views:

74

answers:

2

Hello folks,

Here I am trying to route a page without showing its action in URL,

Eg: URL is http://localhost/brands/1/xyz

Router::connect(
    '/brands/:id/:name',
    array(
        'controller' => 'brands',
        'action' => 'index',
        'id' => '[0-9]{1,}',
        'name' => '[a-z]{1,}'
    )
);

it works fine....

But I need to make the id and name as optional and tried this:

Router::connect(
    '/brands/:id/:name',
    array(
        'controller' => 'brands',
        'action' => 'index',
        'id' => '[0-9]{1,}',
        'name' => '[a-z]{1,}'
    )
);

according to http://book.cakephp.org/view/542/Defining-Routes

But when I try this URL http://localhost/brands/1 it searches for action 1 but http://localhost/brands/1/xyz works fine.

Is there any mistake in my routing configuration????

+2  A: 

If you just want to be able to access the http://localhost/brands/1, you need to add this route:

Router::connect('/brands/:id',
   array('controller' => 'brands','action' => 'index','id' => '[0-9]{1,}')
);

(and also keep your original route)

Router::connect('/brands/:id/:name',
    array('controller' => 'brands','action' => 'index','id' => '[0-9]{1,}','name' => '[a-z]{1,}')
);

(and finally a route for /brands)

Router::connect('/brands',
    array('controller' => 'brands','action' => 'index')
);

Then check for $this->params['id'] and $this->params['name'] in the controller. If needed, redirect to the correct url (if the page is the same and you always want to have the the name in the url, which is good for SEO).

Oscar
but the should be optional...if i route like this then when i try to access /brands/ will throw error........
RSK
Updated the answer. You need one additional route for accessing /brands.
Oscar
+1  A: 

Specify a second route, omitting the optional parameters.

sibidiba