cakePHP路由帮助

时间:2010-07-17 09:58:52

标签: php cakephp cakephp-1.3 url-routing

这里我试图路由一个页面而不在URL中显示它的动作,

例如:网址为http://localhost/brands/1/xyz

Router::connect(
    '/brands/:id/:name',
    array(
        'controller' => 'brands',
        'action' => 'index',
        'id' => '[0-9]{1,}',
        'name' => '[a-z]{1,}'
    )
);

它工作正常....

但我需要将idname作为可选项并尝试此操作:

Router::connect(
    '/brands/:id/:name',
    array(
        'controller' => 'brands',
        'action' => 'index',
        'id' => '[0-9]{1,}',
        'name' => '[a-z]{1,}'
    )
);

according to http://book.cakephp.org/view/542/Defining-Routes

但是,当我尝试使用此网址http://localhost/brands/1时,它会搜索操作1,但http://localhost/brands/1/xyz可以正常工作。

我的路由配置是否有任何错误????

2 个答案:

答案 0 :(得分:2)

如果您只想访问http://localhost/brands/1,则需要添加此路线:

Router::connect('/brands/:id',
   array('controller' => 'brands','action' => 'index','id' => '[0-9]{1,}')
);

(并保留原始路线)

Router::connect('/brands/:id/:name',
    array('controller' => 'brands','action' => 'index','id' => '[0-9]{1,}','name' => '[a-z]{1,}')
);

(最后是/品牌的路线)

Router::connect('/brands',
    array('controller' => 'brands','action' => 'index')
);

然后在控制器中检查$ this-> params ['id']和$ this-> params ['name']。如果需要,重定向到正确的URL(如果页面相同,并且您总是希望在URL中使用该名称,这对SEO有用)。

答案 1 :(得分:1)

指定第二条路线,省略可选参数。