Routing specify which Contorller/Action to be called on specific request.
If you successfully install cake and browse
http://localhost/cake/post/view
this will call PostController’s “view” action. If don’t have the specified controller or action cake will route your request to default controller that display missing controller error.
Similarly /cake/post/viewOrders will route your request to PostController’s “view_orders” action and /cake/post/view/5 to PostController’s “view($key)” action.
If you define following controller
<?php
class PostController extends AppController
{
function view($key,$value)
{
}
}
?>
and browse
http://localhost/cake/post/view/age/26. this will call Post controller “view” action. The parameter $key will be mapped to “age” and $value to “26″.
If you want to pass variables to specific action simply write in browser
http://localhost/cake/post/view/name:fahim/address:my address/
Now in your action simply write
<?php
class PostController extends AppController
{
function view()
{
$name = $this->passedArgs['name'];
$address = $this->passedArgs['address'];
}
}
?>
Before wrapping my article, I would like to tell about a very useful file cakePhp provides for defining your routing.
Open “/app/config/route.php” and you will find some routing mechanism already defined.
If you want that request to certain controller should be route some specific pages, you can define your criteria in this file and cake will do the magic for you.
If you want that all request in your application to /blog/view should be route to post/view, simple define
Router::connect(
'blog/view',
array('controller'=>'post','action'=>'view')
);
in this file.
A more complex example would be
Router::connect(
'/:controller/:year/:month/:day',
array('action'=>'index','day'=>null),
array(
'year' => '[12][0-9]{3}',
'month' => '0[1-9]1[012]',
'day' => '0(1-9)[12][0-9]/3[01]'
)
);
Posted in CakePhp