1
votes

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

  1. Is the rule only application to mentioned controller and action i.e. product and view resp. ?

  2. Why my url (on hover) is showing as controller/action/slug:slug_name instead of controller/action/slug_name ?

  3. In same controller I wish to use different url rewriting such as action/id/slug_name or somewhere I sent /action or somewhere I use only slug action/slug_name. Is only single rule is written for all? Or I have to define separate rule for each controller and action.

  4. 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

1 Answers

3
votes

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

echo $this->Html->link($record['Category']['name'], array('controller' => 'categories', 'action' => 'index',  $record['Category']['slug']), array('escape' => false));

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

# only for action/slug_name case
Router::connect('/products/:slug', array('controller' => 'products', 'action' => 'view'), array('pass' => array('slug'), 'slug'=>'[a-z_]+'));
# for action/id/slug_name case
Router::connect('/products/:id/:slug', array('controller' => 'products', 'action' => 'view'), array('pass' => array('id','slug'), 'id' => '[0-9]+','slug'=>'[a-z_]+'));

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:

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));

much better to do this:

controller/action/param_val1/param_val2

 echo 'controller/action/'.$param_val1.DS.$param_val2;

Also is better for SEO, and url-friendly