Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

passing variables in a mvc pattern

I have built a system that conforms to the mvc pattern in PHP. The controllers and actions are part of the urls in my application. So I have:

www.example.com/controller/action/

So now I am looking for a way to pass on variables. For forms I just use the post method, but sometimes I would just like to link to a different page and then pass on some variables.

I would like some suggestions on how to do this. I know the Zend Framework has the variables as key/value pairs after the action in the controller using the "/" character as a separator, like this:

www.example.com/controller/action/var1/value1/var2/value2

Is this the best way? This is actually the only way I know it's done. I am looking for an easy but yet good way to implement it.

Any suggestions are welcome.

like image 950
sanders Avatar asked Apr 13 '09 20:04

sanders


2 Answers

Frameworks like CodeIgniter let you pass the variables into the controller without disclosing the variable name -

/controller/action/foo/bar/

would get processed as:

function action( $id, $value){
  echo $id; // outputs 'foo'
  echo $value; // outputs 'bar'
}

http://codeigniter.com/user_guide/general/controllers.html

I like this as it gives you control of how many parameters your controller will accept, and if it is important that certain variables are never used, you can do logic or redirect appropriately.

like image 167
postpostmodern Avatar answered Sep 28 '22 00:09

postpostmodern


var1/value1/var2/value2 is really not the best approach and it abuses the MVC controller/action/id structure. Either you need to rethink your design, use POST or query params. Most likely you could redo the design, for example if you have

/search/type/movie/format/divx/year/2000

You could redo that as

/movie/divx?year=2000

So your movie controller would search for divx format movies and then maybe use a helper or filter or client-side script to show only movies that match year == 2000.

like image 40
aleemb Avatar answered Sep 28 '22 02:09

aleemb