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

Php Yii将所有操作路由到控制器中的一个方法,php,routing,yii,Php,Routing,Yii,我正在尝试使用Yii制作一个类似cms的应用程序。功能将类似于: 我想做的是让所有东西都进入ArticleController.php中的方法actionIndex(),并让该方法确定如何处理该操作。因此,我的问题是如何将所有操作路由到Yii中控制器中的一个方法?在配置中的urlManager规则开头添加以下内容: 'article/*' => 'article', 在你的情况下,我认为最好使用a或a 过滤方式: 过滤器是一段代码,配置为在控制器操作执行之前和/或之后执行 样本: c

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


我想做的是让所有东西都进入
ArticleController.php
中的方法
actionIndex()
,并让该方法确定如何处理该操作。因此,我的问题是如何将所有操作路由到Yii中控制器中的一个方法?

在配置中的urlManager规则开头添加以下内容:

'article/*' => 'article',

在你的情况下,我认为最好使用a或a


过滤方式:

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

样本:

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 ...
}
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 ...
}

行动前
方式:

在执行操作之前(在所有可能的筛选器之后)调用此方法。您可以重写此方法以在最后一分钟为操作做准备

样本:

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 ...
}
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

if($this->action->id == 'view') { // say you want to detect actionView
    $this->layout = 'path/to/layout'; // say you want to set a different layout for actionView 
}

您的规则必须与以下类似:-

        'rules' => array(
            '<controller:\w+>/<action:\w+>' => '<controller>/<action>',
            '<controller:\w+>/<action:\w+>' => 'article/index',
        ),
“规则”=>数组(
'/' => '/',
“/”=>“文章/索引”,
),

如果控制器和/或操作不存在,这将把所有请求传递给
ArticleController
PHP类中的
actionIndex
函数。

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

上述建议的筛选器()和beforeAction()以及甚至_构造()都不适用于此目的(如果该操作不存在,则筛选器和beforeAction根本不会启动,并且_构造非常混乱,因为如果将功能放在_构造中,此时我甚至不知道它应该调用哪个控制器/操作/视图)

然而,有一个简单的解决方法涉及URL管理器

在配置中的URL管理器规则中,添加以下行之一(取决于路径设置)


然后,您可以调用/articles/myarticle或/articles/yourarticle这样的页面,而无需在控制器中设置任何函数。您只需在views/articles文件夹中添加一个名为myarticle.php或yourarticle.php的文件,然后在这些文件中键入html内容。

catchAllRequest
param in config file。将其设置为您想要的任何方法。但我只希望使用此控制器,而此catchAllRequest文档似乎是一个站点范围的参数。可能有一种方法可以做到这一点,但我需要知道“处理操作”的含义,以及您计划如何确定?处理操作…我在哪里通过Yii获得操作,类似Yii::app()->控制器->操作(或类似的方法)并基于此确定一个视图。看起来像是使用beforeAction或filter的好选择,请选中answerNo,否则不执行。-'规则'=>array('articles/*'=>'articles','/'=>'/view','//'=>'/','/'=>'/','//'=>'/',),不是
文章
,而是
文章
。你有一个
ArticleController
,对吗?这篇文章只是一个容易识别的例子,真正的控制器是文档,结果是一样的。在我的本地计算机上测试,它可以工作。所有路径,如
article/foo
article/bar
都会转到
ArticleController::actionIndex
。。。不确定你那边发生了什么,这会给你一个错误吗?
public function actionIndex() {
    $name = Yii::app()->request->getParam('action');
    $this->render($name);
}