-
Notifications
You must be signed in to change notification settings - Fork 3
Auto Routing
Auto routing is a real time saver. To use auto routing you would just need to enable it in common.conf.php.
$config['AUTOROUTE'] = true;
Let's say you have a Controller class like this:
class BlogController extends DooController{
public function all(){
//Display all blog posts
}
}
It can be accessed at http://localhost/blog/all
If you have your controller name in a camelCase style:
class CamelCaseController extends DooController{
public function walking(){
//Display all photos on camel walking
}
}
It can be accessed at http://localhost/camel-case/walking
AUTOROUTE will make all controllers accessible through the structured uri. To exclude your one of the controller, reject autoroute by defining autoroute property to false.
class CamelCaseController extends DooController{
public $autoroute = false;
public function youCannotAccessThisWithAutoRoute(){
}
}
##Parameters
And how to get parameters from the URI if autoroute is used? Use getKeyParam()
method
URI is http://localhost/user/find/id/11
//Example controller
class UserCaseController extends DooController {
public function find(){
$id = $this->getKeyParam('id'); //returns 11
}
}
To retrieve multiple parameters at once for: URI is http://localhost/user/find/status/1/type/admin
$this->getKeyParams( ['status', 'type'] );
//it will returns ['status' => 1, 'type' => 'admin']
You can still get parameters by key like what is used in a custom defined route. However the key will be integer indexes.
$this->params[0]; //status
$this->params[1]; //1
$this->params[2]; //type
$this->params[3]; //admin