指定的控制器无效(错误) - Zend Framework

时间:2010-02-16 20:28:03

标签: php zend-framework

我总是得到这个错误:

  

异常'Zend_Controller_Dispatcher_Exception',并在blub \ libraries \ Zend \ Controller \ Dispatcher \ Standard.php中显示消息'指定了无效的控制器(错误)':242

我在“controllers”目录中有一个文件“ErrorController.php”,如下所示:

class ErrorController extends Zend_Controller_Action
{
    public function errorAction()
    {
        // blub
    }
}

我的引导程序如下所示:

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

[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

1 个答案:

答案 0 :(得分:7)

你应该依赖资源加载器/ bootstrap来获取你的frontController,放弃_initController()

要从boostrap获取控制器,您可以执行$this->bootstrap('frontController');$frontController = $this->getResource('frontController');。这样它将使用application.ini中的配置。

就错误而言,我认为您的问题可能是您在引导程序中手动设置的APPLICATION_PATH . '/controllers/'上缺少的斜杠。您的APPLICATION_PATH可能不会以/结尾,因此无法找到applicationcontrollers/ErrorController.php

此外,您的_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"

这使得引导程序的唯一部分需要控制器run()函数的try{}catch{},而index.php可以移动到{{1}}。