无法使用模块解析 Yii 中的请求
Unable to resolve the request in Yii using modules
这是我的控制器
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 我收到此消息: Unable to resolve the request 。如果我创建一个新函数并像这样访问:localhost/cars/myfunction,还是一样。我正在研究 Windows。有人可以帮我解决这个问题吗?
通常,模块的默认 url 规则是 module/controller/actin
,因此对于访问 actionIndex
inside CarsController
inside CarsModule
,您的 url应该是 localhost/cars/cars/index,而不是 localhost/cars/index。如果你不喜欢 url 像 localhost/cars/cars/index,你可以写一个 url 管理器规则来映射 localhost/cars/cars/index变成localhost/cars/index。像这样:
'cars/index' => 'cars/cars/index'
这是我的控制器
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 我收到此消息: Unable to resolve the request 。如果我创建一个新函数并像这样访问:localhost/cars/myfunction,还是一样。我正在研究 Windows。有人可以帮我解决这个问题吗?
通常,模块的默认 url 规则是 module/controller/actin
,因此对于访问 actionIndex
inside CarsController
inside CarsModule
,您的 url应该是 localhost/cars/cars/index,而不是 localhost/cars/index。如果你不喜欢 url 像 localhost/cars/cars/index,你可以写一个 url 管理器规则来映射 localhost/cars/cars/index变成localhost/cars/index。像这样:
'cars/index' => 'cars/cars/index'