php - 指定的 Controller 无效(错误)- Zend Framework

标签 php zend-framework

我总是遇到这个错误:

exception 'Zend_Controller_Dispatcher_Exception' with message 'Invalid controller specified (error)' in blub\libraries\Zend\Controller\Dispatcher\Standard.php:242

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

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

我的 Bootstrap 看起来像这样:

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

最佳答案

你应该依靠资源加载器/ Bootstrap 来获取你的 frontController,删除 _initController()

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

就错误而言,我认为您的问题可能是缺少斜杠:APPLICATION_PATH。 '/controllers/' 您在 Bootstrap 中手动设置的。您的 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"

这留下了您的 Bootstrap 中唯一需要 Controller run() 函数的 try{}catch{} 的部分,它可以移动到您的 index .php 代替。

关于php - 指定的 Controller 无效(错误)- Zend Framework,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2276094/

相关文章:

javascript - 使用隐藏形式将 JavaScript 分数从游戏发送到 PHP - 我没有得到任何值?

php - Symfony2数组访问twig错误

php - SQL 好友列表创建

php - 如何在没有 session_destroy 的情况下销毁 Zend_Session_Namespace

php - 与 Google Chrome 和 PHP 5.3 的 session

php - 如何根据php中的字段过滤数组?

php - 调用数据库的子集,如何在数据库末尾循环记录 1

php - 定制 Zend_Form

zend-framework - Zend 框架 : Post to different action then return to original action if fails validation AND keep form fields

php - Bisna Doctrine 2.1 和 2.2 从未导入注释 "@Table"