PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!
It's been a good 2 months or so since I last touched the Zend Framework, and there has been some rather large changes since then. I am trying to add a header and a footer to the response via a controller plugin. For the life of me I can't get it to render the stupid templates. It keeps giving me this error:
Fatal error: Uncaught exception 'Zend_View_Exception' with message 'script 'elements/header.phtml' not found in path' in C:\phplib\Zend\library\Zend\View\Abstract.php:853 Stack trace: #0 C:\phplib\Zend\library\Zend\View\Abstract.php(764): Zend_View_Abstract->_script('elements/header...') #1 C:\htdocs\dealer\dealer\extends\MC2\Controller\Plugin\HeadFoot.php(11): Zend_View_Abstract->render('elements/header...') #2 C:\phplib\Zend\library\Zend\Controller\Plugin\Broker.php(245): MC2_Controller_Plugin_HeadFoot->preDispatch(Object(Zend_Controller_Request_Http)) #3 C:\phplib\Zend\library\Zend\Controller\Front.php(876): Zend_Controller_Plugin_Broker->preDispatch(Object(Zend_Controller_Request_Http)) #4 C:\htdocs\dealer\dealer\bootstrap.php(74): Zend_Controller_Front->dispatch() #5 C:\htdocs\dealer\index.php(7): include('C:\htdocs\deale...') #6 {main} thrown in C:\phplib\Zend\library\Zend\View\Abstract.php on line 853
I know it's because the basePath I'm setting is incorrect, but I've tried just about everything I can think of. I've tried setting the path relatively as well as absolutely. Nothing seems to work. What am I doing wrong?
<?php
require_once 'Zend/Controller/Plugin/Abstract.php';
class MC2_Controller_Plugin_HeadFoot extends Zend_Controller_Plugin_Abstract
{
public function preDispatch(Zend_Controller_Request_Abstract $request)
{
$view = new Zend_View();
$view->setBasePath('views/scripts/');
$this->getResponse()->prepend('header', $view->render('elements/header.phtml'));
}
public function postDispatch(Zend_Controller_Request_Abstract $request)
{
$view = new Zend_View();
$view->setBasePath('views/scripts/');
$this->getResponse()->append('footer', $view->render('elements/footer.phtml'));
}
}
Whenever I use the Zend Framework, I find myself wanting to use Controller Plugins for just about everything. Is what I'm trying to do even something I should be doing with a controller plugin or does this sound like something that should go into an application controller? I finally got this to work, but then ran into the issue of all of the variables in my header being rendered before they are set. Where is the best place to put this kind of thing?