CakePhp Routing

1 Jan

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]'

)

);

2 Responses to “CakePhp Routing”

  1. JuiCe December 10, 2010 at 11:25 am #

    Hello, thanks for this post , i am novice with zend and phpcake.
    I followed your steps but i get an error:
    [Fri Dec 10 13:06:49 2010] [error] [client IP] File does not exist: /var/www/html/projects/cakephp/post
    I installed succesfully cakePHP and i made settings in the following way:
    – in file config/routers.php :
    Router::connect(‘post/view’,array(‘controller’=>’post’,’action’=>’view’));
    – in folder controllers i make a file PostController.php with content :

    – and in folder views/pages i have post/view.thtml or view.ctp – i have tryed with both.
    And after these, i access url http://link.tld/cakephp/post/view/age/26
    and i get error File does not exist: /var/www/html/projects/cakephp/post
    Where i am wrong?

  2. JuiCe December 10, 2010 at 12:30 pm #

    I configured phpcake like in this post but i get error : post page dont exist.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: