[英]cakePHP route element directs to missing controller action?
我正在嘗試在cakePHP 2.3中設置以下路由:
域/新聞//
我遵循了有關路由的烹飪指南,並且創建的路由是正確的。 我遇到的問題是,當選擇鏈接時,出現“ NewsController中缺少方法”錯誤消息。
這是我配置的:
Router::connect(
'/news/:slug/',
array('controller' => 'news', 'action' => 'view'),
array(
'pass' => array('slug'),
'slug' => '[^_]+'
)
);
我通過一個正則表達式(任何不包含下划線的字符串)來傳遞該段代碼。
這是我在索引頁面中的鏈接:
<?php echo $this->Html->link(
$news['News']['title'],
array(
'controller' => 'news',
'action' => 'view',
'slug' => $news['News']['slug']
)
); ?>
如前所述,URL是正確構建的,看起來像這樣:/ news / test-slug-news-story
但是,當我單擊它時,會收到“ NewsController中的缺少方法”錯誤消息
很明顯我所缺少的東西,因為我看了這么久才能夠看到它。
謝謝保羅
您可以嘗試以下一種方法:
<?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']
)
);
?>
如果對您不起作用,請告訴我:)
謝謝
如上所述,我發現通過在路由設置中的“ slug”后加上反斜杠,控制器將“:slug /”解釋為控制器動作。
那些“ doh”時刻之一。
代碼應如下所示:
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.