简体   繁体   English

cakePHP路由元素是否会导致缺少控制器动作?

[英]cakePHP route element directs to missing controller action?

I'm trying to setup the following routing in cakePHP 2.3: 我正在尝试在cakePHP 2.3中设置以下路由:

domain/news/slug 域/新闻//

I've followed the cookbook guidelines on routing and the route that gets created is correct. 我遵循了有关路由烹饪指南,并且创建的路由是正确的。 The problem I run into is that when selecting the link I get the 'Missing Method in NewsController' error message. 我遇到的问题是,当选择链接时,出现“ NewsController中缺少方法”错误消息。

Here's what I've configured: 这是我配置的:

    Router::connect(
    '/news/:slug/', 
    array('controller' => 'news', 'action' => 'view'), 
    array(
        'pass' => array('slug'),
        'slug' => '[^_]+'
        )
    );

I'm passing in the slug with a regular expression (any string that does not include an underscore). 我通过一个正则表达式(任何不包含下划线的字符串)来传递该段代码。

This is my link in the index page: 这是我在索引页面中的链接:

        <?php echo $this->Html->link(
          $news['News']['title'], 
          array(
            'controller' => 'news',
            'action' => 'view',
            'slug' => $news['News']['slug']
            )
          ); ?>

As mentioned, the URL is built correctly, and looks like this: /news/test-slug-news-story 如前所述,URL是正确构建的,看起来像这样:/ news / test-slug-news-story

But when I click on it I get the 'Missing Method in NewsController' error message 但是,当我单击它时,会收到“ NewsController中的缺少方法”错误消息

Is it obvious what I'm missing, cause I've looked at this too long to be able to see it. 很明显我所缺少的东西,因为我看了这么久才能够看到它。

Thanks, Paul 谢谢保罗

You can try this one: 您可以尝试以下一种方法:

<?php
// Routing code
Router::connect('/news/:slug/', 
    array(
        'controller' => 'news', 
        'action' => 'view'
    ), 
    array(
       'slug' => '[a-zA-Z0-9_-]+'
    )
);
?>

<?php 
// HTML Link code.    
echo $this->Html->link(
    $news['News']['title'], 
    array(
        'controller' => 'news',
        'action' => 'view',
        'slug' => $news['News']['slug']
    )
); 
?>

If it is not working for you please let me know :) 如果对您不起作用,请告诉我:)

Thanks 谢谢

As mentioned above, I discovered that by having a backslash after 'slug' in the route setting, the controller interprets the ':slug/' as the controller action. 如上所述,我发现通过在路由设置中的“ slug”后加上反斜杠,控制器将“:slug /”解释为控制器动作。

One of those 'doh' moments. 那些“ doh”时刻之一。

Code should look like this: 代码应如下所示:

    Router::connect(
    '/news/:slug', 
    array('controller' => 'news', 'action' => 'view'), 
    array(
        'pass' => array('slug'),
        'slug' => '[a-zA-Z0-9_-]+'
        )
    );

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM