我正在尝试使用Yii制作类似cms的应用程序。该功能将类似于:
http://www.example.com/article/some-article-name
[我正在尝试做的是让actionIndex()
中的方法ArticleController.php
都拥有所有内容,并让一个方法确定如何处理该动作。所以我的问题是如何将所有动作路由到Yii控制器中的一个方法?
beforeAction
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->action
:id
:]的值>
$this->action->id