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.
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.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With