【问题标题】:Uncaught exception 'Zend_Controller_Dispatcher_Exception' error when using Zend framework使用 Zend 框架时出现未捕获的异常“Zend_Controller_Dispatcher_Exception”错误
【发布时间】:2011-05-16 15:37:48
【问题描述】:

我在运行Xampp 1.7.1包的Windows机器上使用Zend Framework v1.11.0。我的项目目录结构如下。

/
|- /data
| |- /logs
| |- /uploaded-files
| |- /tmp
|- /htdocs
|- /include
| |- /Controllers
| |- /Zend
|- /templates

我在 htdocs 中的 index.php 中有以下代码:

<?php

    require_once('Zend/Loader.php');
    Zend_Loader::registerAutoload();

    $controller = Zend_Controller_Front::getInstance();
    $controller->setControllerDirectory('../include/Controllers');
    $controller->dispatch();

?>  

我得到的错误如下:

注意:Zend_Loader::Zend_Loader::registerAutoload 自 1.8.0 起已弃用,将在 2.0.0 中删除;在第 266 行的 C:\xampp\htdocs\myproject\include\Zend\Loader.php 中使用 Zend_Loader_Autoloader

致命错误:在 C:\xampp\htdocs\myproject\include\Zend\Controller\Dispatcher\Standard.php:248 中未捕获异常“Zend_Controller_Dispatcher_Exception”并带有消息“指定的控制器无效(错误)” 堆栈跟踪:

#0 C:\xampp\htdocs\myproject\include\Zend\Controller\Front.php(954): Zend_Controller_Dispatcher_Standard->dispatch(Object(Zend_Controller_Request_Http), Object(Zend_Controller_Response_Http))

#1 C:\xampp\htdocs\myproject\htdocs\index.php(8): Zend_Controller_Front->dispatch()

#2 {main} 在第 248 行的 C:\xampp\htdocs\myproject\include\Zend\Controller\Dispatcher\Standard.php 中抛出

我的 .htaccess 文件位于 myproject/htdocs 中:

RewriteEngine on
RewriteCond %{SCRIPT_FILENAME} !-f
RewriteCond %{SCRIPT_FILENAME} !-d
RewriteRule ^(.*)$ index.php/$1

在我的 apache httpd.conf 中,我定义了以下 VirtualHost:

<VirtualHost myproject:80>
    ServerName myproject
    DocumentRoot "c:/xampp/htdocs/myproject/htdocs"
    <Directory "c:/xampp/htdocs/myproject/htdocs">
        AllowOverride None
        Options All
    </Directory>
    php_value include_path ".;c:/xampp/htdocs/myproject/include;c:/xampp/php/PEAR"
    php_value magic_quotes_gpc off
    php_value register_globals off
</VirtualHost>

这里可能出了什么问题?

请帮忙 谢谢你

【问题讨论】:

  • 为什么不用zend工具自带的常规index.php文件

标签: php zend-framework


【解决方案1】:

我知道这为时已晚,这可能对像我这样遇到此错误的其他人有用。

对于第一个错误:替换此代码。

require_once('Zend/Loader.php');
Zend_Loader::registerAutoload();

这个

require_once 'Zend/Loader/Autoloader.php';
$autoloader = Zend_Loader_Autoloader::getInstance();
$autoloader->setFallbackAutoloader(true);

并稍微改变你的目录结构并使用以下代码 在index文件夹中创建一个文件IndexController.php

$controller = Zend_Controller_Front::getInstance();
$controller->setDefaultModule('index');
$controller->addModuleDirectory('PATH_TO_MODULES')->dispatch();

而不是使用这个

$controller = Zend_Controller_Front::getInstance();
$controller->setControllerDirectory('../include/Controllers');
$controller->dispatch();

你的主管结构是这样的

application
   modules
       index
          controllers
              IndexController.php
          views

【讨论】:

    【解决方案2】:

    消息Invalid controller specified (error),表示调度程序正在寻找一个名为ErrorController的控制器类,但找不到。

    ErrorController 通常在 Zend_Controller_Front 中 throwExceptions 标志设置为 false 时调用。在这种情况下,抛出的异常将被聚合并转发到 ErrorController 类。这是 ErrorHandler 插件提供的一个功能:一个名为 Zend_Controller_Plugin_ErrorHandler 的类。

    简而言之,您的代码中可能存在引发异常的内容,但由于您没有错误控制器,因此调度程序会失败。更改前端控制器中的throwExceptions 标志、创建错误控制器或禁用插件应该可以让您看到实际错误的来源。

    【讨论】:

      【解决方案3】:

      首先;

      Zend_Loader::registerAutoload();
      

      已弃用,这就是为什么您会收到第一条通知

      Notice: Zend_Loader::Zend_Loader::registerAutoload is deprecated as of 1.8.0 and will be removed with 2.0.0; use Zend_Loader_Autoloader instead in C:\xampp\htdocs\myproject\include\Zend\Loader.php on line 266
      

      在您的情况下,您的应用程序不知道默认控制器名称及其默认操作是什么 它抛出一个错误 并且错误处理程序取而代之,您的前端控制器再次尝试定位错误控制器,但它也没有找到它 所以它会告诉你这个错误

      Fatal error: Uncaught exception 'Zend_Controller_Dispatcher_Exception' with message 'Invalid controller specified (error)' in C:\xampp\htdocs\myproject\include\Zend\Controller\Dispatcher\Standard.php:248 Stack trace:
      

      好的最后: 如果我负责,我该如何解决:

      1-将应用程序结构重新管理为默认的ZF文件结构 2-您可以尝试设置默认控制器名称和动作名称,查阅文档或 导航到 Zend/Controller/Front.php 你会发现像

      这样的函数
      public function setDefaultAction($action)
          {
              $dispatcher = $this->getDispatcher();
              $dispatcher->setDefaultAction($action);
              return $this;
          }
      
       public function setDefaultAction($action)
          {
              $dispatcher = $this->getDispatcher();
              $dispatcher->setDefaultAction($action);
              return $this;
          }
      

      还有很多其他的setter函数

      别忘了深入了解@ZF flowchart http://devzone.zend.com/article/4601

      希望对你有帮助

      【讨论】:

        猜你喜欢
        • 2011-01-08
        • 1970-01-01
        • 2014-01-23
        • 2023-01-29
        • 1970-01-01
        • 2011-01-12
        • 1970-01-01
        • 2011-02-14
        • 1970-01-01
        相关资源
        最近更新 更多