Reputation: 1546
I have the following code in routes.php
:
Router::connect('/c/details/:id/:slug',
array('controller' => 'cars'),
array('pass' => array('id', 'slug'))
);
If I try to access http://domain.com/c/details/123/abc
, it works. However, if I remove abc
(ie the slug), CakePHP tries to access the action 123
(which is the id, not the action).
Error: The action 123 is not defined in controller CarsController
If I use /c/details/:id/:slug/:action/*
, which was what I had before upgrading from 1.2 (yeah, pretty old) to 2.2.1 and it was working fine, CakePHP also tries to access action 123
whether I have a slug or not.
URLs without slugs always worked before upgrading CakePHP and with the code I have in the controller, if there was no slug in the URL, it would redirect to the right URL.
Edit: I just checked and it seems that when I don't provide with a slug, the whole thing is shifted. c
is ignored, details
becomes the controller and 123
becomes the action.
[request] => CakeRequest Object
(
[params] => Array
(
[plugin] =>
[controller] => details
[action] => 123
[named] => Array()
[pass] => Array()
[isAjax] =>
)
When the correct would be, and which is what I get if I provide with a slug:
[request] => CakeRequest Object
(
[params] => Array
(
[plugin] =>
[controller] => cars
[action] => index
[named] => Array()
[pass] => Array
(
[0] => 123
[1] => abc
)
[id] => 123
[slug] => abc
[isAjax] =>
)
Any idea what may be causing this issue now?
Upvotes: 1
Views: 725
Reputation: 6767
You may need 2 routes if you want the slug to be optional (not sure). In any case, add the action
key to each route as well.
Router::connect('/c/details/:id',
array('controller' => 'cars', 'action' => 'details'),
array('pass' => array('id'))
);
Router::connect('/c/details/:id/:slug',
array('controller' => 'cars', 'action' => 'details'),
array('pass' => array('id', 'slug'))
);
Upvotes: 2