学习《PHP Web2.0 开发实战》中ZF的使用,刚创建了简单的IndexController类,想看一下效果,结果出现错误:
问题1:
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 D:\PHPServer\zend\library\Zend\Loader.php on line 266
解决1:
因为从zf2.0开始已经使用 Zend_Loader_Autoloader替代了Zend_Loader,所以需要使用新类,因此index.php中代码修改为
require_once('Zend/Loader/Autoloader.php');
$loader = Zend_Loader_Autoloader::getInstance();
问题2:
Fatal error: Uncaught exception 'Zend_Controller_Dispatcher_Exception' with message 'Invalid controller specified (error)' in D:\PHPServer\zend\library\Zend\Controller\Dispatcher\Standard.php:248 Stack trace: #0 D:\PHPServer\zend\library\Zend\Controller\Front.php(954): Zend_Controller_Dispatcher_Standard->dispatch(Object(Zend_Controller_Request_Http), Object(Zend_Controller_Response_Http)) #1 D:\PHPRoot\phpweb20\htdocs\index.php(10): Zend_Controller_Front->dispatch() #2 {main} thrown in D:\PHPServer\zend\library\Zend\Controller\Dispatcher\Standard.php on line 248
解决2:
这个问题是由于没有默认的错误控制器引起的,代码中增加
$frontController->setParam('useDefaultControllerAlways', true);
控制器会自动寻找与之对应的模型,根据需要的错误中所描述的路径,建立对应的模型。
问题3:
Fatal error: Uncaught exception 'Zend_View_Exception' with message 'script 'index/index.phtml' not found in path (d:/phproot/phpweb20/include/views\scripts/)' in D:\PHPServer\zend\library\Zend\View\Abstract.php:980 Stack trace: #0 D:\PHPServer\zend\library\Zend\View\Abstract.php(876): Zend_View_Abstract->_script('index/index.pht...') #1 D:\PHPServer\zend\library\Zend\Controller\Action\Helper\ViewRenderer.php(897): Zend_View_Abstract->render('index/index.pht...') #2 D:\PHPServer\zend\library\Zend\Controller\Action\Helper\ViewRenderer.php(918): Zend_Controller_Action_Helper_ViewRenderer->renderScript('index/index.pht...', NULL) #3 D:\PHPServer\zend\library\Zend\Controller\Action\Helper\ViewRenderer.php(957): Zend_Controller_Action_Helper_ViewRenderer->render() #4 D:\PHPServer\zend\library\Zend\Controller\Action\HelperBroker.php(277): Zend_Controller_Action_Helper_ViewRenderer->postDispatch() #5 D:\PHPServer\zend\library\Zend\Controller\Action.php(523): Zend_Controller_Action_HelperBroker->notifyPostDispatch() #6 D:\PHPServe in D:\PHPServer\zend\library\Zend\View\Abstract.php on line 980
解决3:使用Zend_Controller时,会自动加载一个名为ViewRenderer的插件,它会根据所请求的控制器和动作名来显示一个视图脚本(模板文件)。而使用Smarty时,必须要扩展Zend_View_Abstrat类从而与Smarty类交互。需要床架一个Templater的类,然后在index.php引导文件中告诉Zend_Controller这个类的信息。
或者根据错误提示创建phpweb20/include/views/scripts/index/index.phtml模板文件,使zf输出时能正常使用。
问题4:
Fatal error: Call to a member function fetch() on a non-object in D:\PHPRoot\phpweb20\include\Templater.php on line 58
解决4:这个错误是因为我根据书中代码录入的时候出现了偏差,类的构造器应该使用两个下划线,而录入时只使用了一个,这就造成templater类没有被正常构造,从而出错。
参考:
1. Uncaught exception 'Zend_Controller_Dispatcher_Exception' http://stackoverflow.com/questions/2130530/uncaught-exception-zend-controller-dispatcher-exception
1. Practical Web 2.0 Applications with PHP – Chapter 2 http://blog.rrosetta.com/2009/10/12/practical-web-2-0-applications-with-php-chapter-2/