无法使用模块解决Yii中的请求

时间:2015-03-25 14:34:47

标签: php yii

这是我的控制器

class CarsController extends Controller {
    public function actionIndex() {
       echo 1; exit();
    }

}

这是模块文件:

<?php

class CarsModule extends CWebModule {

    public $defaultController = "cars";

    public function init() {
        // this method is called when the module is being created
        // you may place code here to customize the module or the application
        // import the module-level models and components
        $this->setImport(array(
            'cars.models.*',
            'cars.components.*',
        ));
    }

    public function beforeControllerAction($controller, $action) {
        if (parent::beforeControllerAction($controller, $action)) {
            // this method is called before any module controller action is performed
            // you may place customized code here
            return true;
        }
        else
            return false;
    }

}

我的问题是,如果我访问我的项目,如:localhost / cars它的工作原理。如果我访问localhost / cars / index,我收到此消息:无法解析请求。如果我创建一个新函数,我这样访问:localhost / cars / myfunction,仍然是相同的。我在Windows上工作。有人可以帮我这个吗?

1 个答案:

答案 0 :(得分:0)

通常模块的默认网址规则为module/controller/actin,因此对于actionIndex内的CarsController内的访问CarsModule,您的网址应为 localhost / cars / cars / index ,而不是 localhost / cars / index 。如果您不喜欢网址 localhost / cars / cars / index ,您可以编写一个网址管理器规则来映射 localhost / cars / cars / index 进入 localhost / cars / index 。像这样:

'cars/index' => 'cars/cars/index'