user2014
user2014

Reputation: 388

How to remove controller and action name from URL in CakePHP?

How can I write working router:connect to have SEO friendly links?

In my site, I have articles and category. In right sidebar article's category is listed with below link.

<?php echo $this->Html->link(ucwords($data['Category']['name']),array('controller'=>'Articles','action'=>'displayArticles','cat'=>$data['Category']['name'])) ?>

This gives me link like - http://example.com/Articles/displayArticles/category-name , now I want the link as http://example.com/category-name. So for that I have tried below code but its not working.

Router::connect(
    '/:query/*', array('controller' => 'Articles', 'action' => 'displayArticles'), array(
        'params' => array('query', 'cat'),
        'named' => array(
            'query', 'cat'
        )
    )
);

So please someone let me know, how to achieve just category name(parameter) in URL.

Thanks in advance!

Upvotes: 1

Views: 1482

Answers (1)

Momin
Momin

Reputation: 868

Router::connect(
    '/:query',
   array('controller' => 'Articles', 'action' => 'displayArticles',1)
    array('query' => '[a-zA-Z]+')
);

Here id is numeric with regex. please see this

You will also have to give parameter count in router.

Upvotes: 2

Related Questions