【问题标题】:Router cakephp pointing to same action路由器 cakephp 指向相同的动作
【发布时间】:2015-03-06 02:02:18
【问题描述】:

我在cakephp下的路由遇到了一些问题 我的控制器中有两个动作 它们如下:

example.com/posts/show/show-by-day
example.com/posts/view/slug-post

我希望它们为:

example.com/article/show-by-day.html
example.com/article/slug-post.html

所以我在我写的配置文件下路由文件:

Router::connect('/article/:show_by_day', array('controller' => 'posts', 'action' => 'show'), array('pass' => array('show_by_day'))); 

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

当我点击 URL example.com/article/show-by-day.html 时它工作正常

但是当我点击 url example.com/article/slug-post.html 时,它又指向了动作展示。

那我该如何解决呢? 非常感谢!

【问题讨论】:

    标签: php cakephp


    【解决方案1】:

    试试这个

    Router::connect( '/article/show-by-day.html', 
        array(
            'controller' => 'posts',
            'action' => 'show'
        ),
        array(
            'pass' => array('show_by_day')
        )
    );
    
    Router::connect( '/article/slug-post.html', 
        array(
            'controller' => 'posts',
            'action' => 'view'
        ),
        array(
            'pass' => array('slug')
        )
    );
    

    说明: Router::connect 的第一个参数是您要匹配的确切 URL - 在原始代码中,包含 : - 它参数化 URL 而不是使用完全匹配。 Router::connect 中的第二个和第三个参数是实际需要调用的控制器/动作以及所需的参数。

    【讨论】:

    • 你能解释一下吗?我认为您的答案不符合他想要的 URL 结构
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-07-06
    • 2012-09-18
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多