I am new with cakePHP, I wish to rewrite my urls in my application.
For some of my Controllers I want to navigate as.
`controller/action/id/slug`.
for this I write rule as,
Router::connect(
'/products/:id/:slug', array('controller' => 'products', 'action' => 'view'), array('pass' => array('id'), 'id' => '[0-9]+')
);
I am writing link as..
echo $this->Html->link($record['Category']['name'], array('controller' => 'categories', 'action' => 'index', 'slug' => $record['Category']['slug']), array('escape' => false));
Questions
-
Is the rule only application to mentioned controller and action i.e. product and view resp. ?
-
Why my url (on hover) is showing as
controller/action/slug:slug_nameinstead ofcontroller/action/slug_name? -
In same controller I wish to use different url rewriting such as
action/id/slug_nameor somewhere I sent/actionor somewhere I use only slugaction/slug_name. Is only single rule is written for all? Or I have to define separate rule for each controller and action. -
Creating common rule for all controllers as well ?
Please describe more. I tried to read the documentation but bit confused with it. How my params would get request for all sort of types I have mentioned.
Write now I am using only simple methods to get params as
controller/action/param1:param_val1/param2:param_val2,
echo $this->Html->link('Val', array('controller' => 'categories',
'action' => 'index', 'param1' => param_val1, 'param2' => param_val2,),
array('escape' => false));
And so on. But I wish to have pretty clear n beautiful url along with better and faster moves. Like passing slug I wish to directly fetch findBySlug or some sort of way making it pretty more good.
1) Yes, When you create a Router::connect rule is only aplicable for one case, namely is applicable for one url or group os urls.
2) You should do it in this way
3) You must create a rule for those urls, only when the structure of the url is different
According with your examples, the rules will be as follows
4) I recommend you to write the urls without using the HTML component of CakePHP. Not because it’s bad, but because it is much faster and easier
instead of doing this:
much better to do this:
Also is better for SEO, and url-friendly