用的是APACHE服务器,
在原理上在.htaccess设置 RewriteRule !\.(js|ico|gif|jpg|png|css)$ index.php
就会把所有的请求发送到index.php,包括不存在的文件和目录
可是我只有在输入 http://localhost/zend/ 才能正访问,
如果输入 http://localhost/zend/ 和任意不存的目录或文件就会出错,出错信息如下:
- PHP code
Fatal error: Uncaught exception 'Zend_Controller_Dispatcher_Exception' with message 'Invalid controller specified (error)' in C:\php\library\Zend\Controller\Dispatcher\Standard.php:241 Stack trace: #0 C:\php\library\Zend\Controller\Front.php(934): Zend_Controller_Dispatcher_Standard->dispatch(Object(Zend_Controller_Request_Http), Object(Zend_Controller_Response_Http)) #1 G:\WEB\php\Z\index.php(36): Zend_Controller_Front->dispatch() #2 {main} thrown in C:\php\library\Zend\Controller\Dispatcher\Standard.php on line 241
我的index.php是这样写的
- PHP code
<?php /* * *Zend Framework * */ //require_once 'Zend/Controller/Front.php'; // $rootPath = dirname(dirname(__FILE__)); //Zend_Controller_Front::run($rootPath . '/z/application/controllers'); error_Reporting(E_ALL|E_STRICT); date_default_timezone_set('asia/shanghai'); set_include_path('.' .PATH_SEPARATOR .'./library'.PATH_SEPARATOR .'./application/models/'.PATH_SEPARATOR . get_include_path()); require_once 'Zend/Loader.php'; Zend_Loader::registerAutoload();//设置Zend Framework 自动载入类文件 $registry = Zend_Registry::getInstance(); $view = new Zend_View(); $view->setScriptPath('./application/views/scripts/index/');//设置模板显示路径 $registry['view'] = $view;//注册View //配置数据库参数,并连接数据库 $config=new Zend_Config_Ini('./application/config/config.ini',null, true); Zend_Registry::set('config',$config); $dbAdapter=Zend_Db::factory($config->general->db->adapter,$config->general->db->config->toArray()); $dbAdapter->query('SET NAMES UTF8'); Zend_Db_Table::setDefaultAdapter($dbAdapter); Zend_Registry::set('dbAdapter',$dbAdapter); //设置控制器 $frontController =Zend_Controller_Front::getInstance(); $frontController->setBaseUrl('/z')//设置基本路径 ->setParam('noViewRenderer', true) ->setControllerDirectory('./application/controllers') ->throwExceptions(False) ->dispatch();
------解决方案--------------------
你需要在创建 FrontController 的时候设置
- PHP code
$frontCtrl->throwExceptions(true);// true 为抛出异常,false 为不抛出 $frontCtrl->setParam('useDefaultControllerAlways', true); // true 为 404 总是重定向到默认控制器 $this->_frontCtrl->registerPlugin( new Zend_Controller_Plugin_ErrorHandler(array('module'=>'error', 'controller'=>'error', 'action'=>'error')) );