开发者

How do I change my cakePHP urls in routes?

I have tried to modify my routes in app/config/routes.php.

I have urls like this: http://example.com/controller/action/params I would like urls like this: http://example.com/controller/params

开发者_JAVA百科

here is my route I have built as I understand it:

Router::connect('/services/:slug', 
array('controller' => 'services', 'action' => 'view'), 
array('pass' => array('slug')));

and here is my link in my index.ctp file for services:

echo $this->Html->link(
     __($service['Service']['title'], true),
    array('controller' => 'services', 'action' => 'view', service['Service']'slug'])
); 

If it helps, here is my view function from the services controller:

    function view($slug) {
        $service = $this->Service->findBySlug($slug);

        $this->set('service', $service);
    }


You are almost there. The view file to the action route is view.ctp(the method name) rather than index.ctp

In your link you can do:

<?php
echo $this->Html->link(
__($service['Service']['title'], true),
array('controller' => 'services', 'action' => $service['Service']['slug'])
?>

Without a method at the 'action' param because your settings will do the routing for you.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜