vendredi 23 janvier 2015

MVC URL structure with URI parameters


When creating routes for your MVC web application I have seen two possible ways to pass variables to a controller method:


With the first approach the variable is passed to a parameter of the controller method and utilized as such:



1a. http://ift.tt/15adv6v

1b. class controller1 extends main_controller(){

function method1($var1){

echo $var1; //prints "variable1"

}

}


The next approach uses URI parameters and allows for the name of the variables being passed to appear in the URL:



2a. http://ift.tt/1zwHpiT

2b. class controller1 extends main_controller(){

function method1(){

//split the uri into an array using framework function
$uri = $this->uri_to_assoc()

//call the uri variables as array indexes
echo $uri['variable1']; //prints "34"

echo $uri['variable2']; //prints "56"

}

}


My question is concerning when to use one case vs the other? My guess would be that approach #2 would be more for RESTful web services while approach #1 would be for a web application that will be serving html and crawled by search engines?





Aucun commentaire:

Enregistrer un commentaire