[英]Invalid controller specified (error) - Zend Framework
I get all the time this error: 我总是得到这个错误:
exception 'Zend_Controller_Dispatcher_Exception' with message 'Invalid controller specified (error)' in blub\\libraries\\Zend\\Controller\\Dispatcher\\Standard.php:242
异常'Zend_Controller_Dispatcher_Exception',在blub \\ libraries \\ Zend \\ Controller \\ Dispatcher \\ Standard.php中显示消息'指定了无效的控制器(错误)':242
I have a file "ErrorController.php" in the "controllers" directory looking like this: 我在“controllers”目录中有一个文件“ErrorController.php”,如下所示:
class ErrorController extends Zend_Controller_Action
{
public function errorAction()
{
// blub
}
}
My bootstrap looks like this: 我的引导程序看起来像这样:
protected function _initController()
{
$this->_frontcontroller = Zend_Controller_Front::getInstance();
$this->_frontcontroller->setControllerDirectory(APPLICATION_PATH . 'controllers/');
}
protected function _initRoute()
{
$this->_route = $this->_frontcontroller->getRouter();
$this->_route->addRoute('default', new Zend_Controller_Router_Route(
':controller/:action/*', array(
'module' => 'default',
'controller' => 'index',
'action' => 'index'
)
));
}
public function run()
{
try {
$this->_frontcontroller->dispatch();
}
catch (Exception $e) {
print nl2br($e->__toString());
}
}
application.ini 的application.ini
[bootstrap]
autoloadernamespaces[] = "Zend_"
autoloadernamespaces[] = "ZendX_"
[production]
includePaths.library = APPLICATION_PATH "/libraries"
bootstrap.path = APPLICATION_PATH "/Bootstrap.php"
bootstrap.class = "Bootstrap"
resources.frontController.controllerDirectory = APPLICATION_PATH "/controllers"
resources.frontController.params.throwexceptions = 0
resources.frontController.params.displayExceptions = 0
[development : production]
resources.frontcontroller.params.throwexceptions = 1
resources.frontController.params.displayExceptions = 1
You should rely on the resource loader/bootstrap to get your frontController, drop the _initController()
你应该依赖资源加载器/ bootstrap来获取你的frontController,删除
_initController()
To get your controller from the boostrap, you can do $this->bootstrap('frontController');
要从boostrap获取控制器,可以执行
$this->bootstrap('frontController');
and $frontController = $this->getResource('frontController');
和
$frontController = $this->getResource('frontController');
. 。 This way it will use the configuration in your application.ini.
这样它将使用application.ini中的配置。
As far as the error goes, I think your problem might be the missing slash on: APPLICATION_PATH . '/controllers/'
就错误而言,我认为您的问题可能是缺少的斜杠:
APPLICATION_PATH . '/controllers/'
APPLICATION_PATH . '/controllers/'
that you are manually setting in the bootstrap. 您在引导程序中手动设置的
APPLICATION_PATH . '/controllers/'
。 Your APPLICATION_PATH
might not end with a /
, therefore it can't find applicationcontrollers/ErrorController.php
您的
APPLICATION_PATH
可能不以/
结尾,因此无法找到applicationcontrollers/ErrorController.php
Also, your _initRoute()
function could be replaced with the following application.ini: 此外,您的
_initRoute()
函数可以替换为以下application.ini:
resources.router.routes.default.type = "Zend_Controller_Router_Route"
resources.router.routes.default.route = ":controller/:action/*"
resources.router.routes.default.defaults.controller = "index"
resources.router.routes.default.defaults.action = "index"
resources.router.routes.default.defaults.module = "default"
This leaves the only part of your bootstrap that wants the controller the run()
function's try{}catch{}
which could be moved to your index.php
instead. 这使得引导程序的唯一部分需要控制器
run()
函数的try{}catch{}
,而不是可以将其移动到index.php
。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.