Yii将所有操作路由到控制器中的一种方法

问题描述 投票:2回答:5

我正在尝试使用Yii制作类似cms的应用程序。该功能将类似于:

http://www.example.com/article/some-article-name

[我正在尝试做的是让actionIndex()中的方法ArticleController.php都拥有所有内容,并让一个方法确定如何处理该动作。所以我的问题是如何将所有动作路由到Yii控制器中的一个方法?

php routing yii
5个回答
2
投票
就您而言,我认为最好使用filterbeforeAction method


过滤方式:

过滤器是一段代码,配置为在执行控制器动作之前和/或之后执行。

示例:

beforeAction


class SomeController extends Controller { // ... other code ... public function filters() { return array( // .. other filters ... 'mysimple', // our filter will be applied to all actions in this controller // ... other filters ... ); } public function filterMysimple($filterChain) { // this is the filter code // ... do stuff ... $filterChain->run(); // this bit is important to let the action run } // ... other code ... } 方式:

将在执行动作之前(在所有可能的过滤器之后立即调用此方法。)您可以重写此方法以为动作做最后准备。

示例:

beforeAction


作为旁注,您还可以通过以下方式访问控制器内的当前操作:class SomeController extends Controller { // ... other code ... protected function beforeAction($action) { if (parent::beforeAction($action)){ // do stuff return true; // this line is important to let the action continue } return false; } // ... other code ... } ,以获取$this->actionid:]的值>

$this->action->id


0
投票
在配置中urlManager规则的开头添加此:

0
投票
您的规则必须类似于以下内容:-

0
投票
[我猜您只是想在“视图”文件夹中放入一堆静态页面,并自动选择并渲染它们,而无需在控制器中为每个页面添加操作。

0
投票
情况如何,当我真的想将对控制器的每个请求重定向到单个操作时。不会有静态内容-动作将自行处理路由。范例:
© www.soinside.com 2019 - 2024. All rights reserved.