Passing variables in mvc template

I created a system that matches the mvc pattern in PHP. Controllers and actions are part of the URLs in my application. Therefore, I have:

www.example.com/controller/action/

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

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

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

Is this the best way? This is actually the only way I know this. I am looking for a simple but still good way to implement it.

Any suggestions are welcome.

+4
source share
7 answers

Structures like CodeIgniter allow you to pass variables to the controller without revealing the variable name -

/controller/action/foo/bar/ 

will be 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 because it gives you control over the number of parameters that your controller will take, and if it is important that certain variables are never used, you can make logic or redirect accordingly.

+4
source

var1/value1/var2/value2 really not the best approach, and it abuses the MVC controller/action/id structure. Or you need to rethink your design, use the POST or request parameters. Most likely, you can remake the design, for example, if you have

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

You can repeat it like

 /movie/divx?year=2000 

So, your movie controller will look for divx format movies, and then maybe use the helper or filter or client side of the script to show only movies corresponding to year == 2000.

+3
source

You have the option of passing variables through GET or POST.

GET will add variables to the URL, or in a nice format

  /controller/action/var1/value1/var2/value2 

or traditional way

  /controller/action/?var1=value1&var2=value2 

In the first case, you still need to write an Apache rewrite rule to extract the variables from the URL and the route to the controller / action with the correct set of variables as part of your $ _REQUEST.

If you use POST, you can add hidden values ​​to each form as a way of passing variables.

For variables that you would not like to publish, you can use Sessions to store them on the server side.

+1
source

You can also make GET requests as follows:

www.site.com/controller/action?var1=value1&var2=value2

They will appear in the superglobal $ _GET.

+1
source

Depending on your project, you might also try setting and reading values ​​using PHP sessions. These values ​​should not be passed from script to script as GET or POST values. One side (or side up) is that variables cannot simply be passed as part of a URL (or bookmark).

+1
source

I would avoid the Zend key / value approach mentioned above. The problem is that creating three different URLs becomes trivial.

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

pointing to the same resource. While this works, part of the success of MVC style frameworks for web applications is that they make it easy to provide a clean URL structure and link one URL on your site to each resource. The var1 / value1 / var2 / value2 method has few advantages over key / value query strings.

The approach I'll take here is to drop the keys and just use the values

 http://www.example.com/example/list/value1/value2/value3 

which will be passed to the action method as an array

 class example extends Controller{ public function list($args){ //$args[0] = 'value1'; //$args[1] = 'value2'; //$args[2] = 'value3'; } } 

By doing this in this way, you leave it to the end users of your system to decide how they want to handle $ GET style variables while encouraging a design that results in cleaner, more stable single-resource URLs.

+1
source

What I like to use in my CakePHP projects is "named params". Basically, my urls look like this:

 http://example.com/controller/action/param1:value1/param2:value2 

You can see some more examples in Cake manual . I don’t know if your infrastructure supports this (or Zend), but if bakers did this, it can be done. :-)

+1
source

All Articles