Zend_View - Why can't I set the basePath properly?

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!

Moderator: General Moderators

Post Reply
User avatar
Luke
The Ninja Space Mod
Posts: 6424
Joined: Fri Aug 05, 2005 1:53 pm
Location: Paradise, CA

Zend_View - Why can't I set the basePath properly?

Post by Luke »

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?

Code: Select all

<?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'));
    }
}
User avatar
volka
DevNet Evangelist
Posts: 8391
Joined: Tue May 07, 2002 9:48 am
Location: Berlin, ger

Post by volka »

try

Code: Select all

<?php
function my_debug_log($prefix, $message) {
	/*
	static $logfile = null;
	if( is_null($logfile) ) {
		$logfile = $_SERVER['DOCUMENT_ROOT'] . 'Debug.txt';
	}
	
	error_log('[' . $prefix.'] '. $message."\n", 3, $logfile);
	*/
	
	echo '<div>[' , $prefix, '] ', $message, "</div>>\n";
}

function my_stat($path) {
	$r = is_dir($path) ? 'd':'-';
	$r .= is_readable($path) ? 'r':'-';
	$r .= is_writeable($path) ? 'w':'-';
	$r .= is_executable($path) ? 'x':'-';
	$r .= ' ' . $path;
	return $r;
}

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();
		my_debug_log('preDispatch', 'cwd: '.getcwd());

		$path = realpath('.');
		foreach( array('views','scripts', 'elements', 'header.phtml') as $x ) {
			$path .= '/'. $x;
			echo my_debug_log('preDispatch', my_stat($path));
		}

		$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'));
	}
}
User avatar
Luke
The Ninja Space Mod
Posts: 6424
Joined: Fri Aug 05, 2005 1:53 pm
Location: Paradise, CA

Post by Luke »

Does anybody know what in the world I'm doing wrong? Am I going about this wrong? Everything I've tried has failed. I am ripping my hair out. Image

volka, I ran this:

Code: Select all

<?php

function my_debug_log($prefix, $message) {
        /*
        static $logfile = null;
        if( is_null($logfile) ) {
                $logfile = $_SERVER['DOCUMENT_ROOT'] . 'Debug.txt';
        }
       
        error_log('[' . $prefix.'] '. $message."\n", 3, $logfile);
        */
       
        echo '<div>[' , $prefix, '] ', $message, "</div>>\n";
}

function my_stat($path) {
        $r = is_dir($path) ? 'd':'-';
        $r .= is_readable($path) ? 'r':'-';
        $r .= is_writeable($path) ? 'w':'-';
        $r .= is_executable($path) ? 'x':'-';
        $r .= ' ' . $path;
        return $r;
} 

require_once 'Zend/Controller/Plugin/Abstract.php';

class MC2_Controller_Plugin_HeadFoot extends Zend_Controller_Plugin_Abstract
{
    private $_view = null;

    public function __construct(Zend_View_Abstract $view)
    {
        $this->_view = $view;
    }
    
    public function preDispatch(Zend_Controller_Request_Abstract $request)
    {
                my_debug_log('preDispatch', 'cwd: '.getcwd());

                $path = realpath('.');
                foreach( array('dealer','default','views','scripts', 'elements', 'header.phtml') as $x ) {
                        $path .= '/'. $x;
                        echo my_debug_log('preDispatch', my_stat($path));
                }
                $basepath = realpath('.');
        $this->_view->setBasePath($basepath . '/dealer/default/views');
        d($this->_view); // shortcut for Zend_Debug::dump();
        $this->getResponse()->prepend('header', $this->_view->render('elements/header.phtml'));
    }
    
    public function postDispatch(Zend_Controller_Request_Abstract $request)
    {
        $this->_view->setBasePath('/dealer/default/views');
        $this->getResponse()->append('footer', $this->_view->render('elements/footer.phtml'));
    }
}
Got this:

Code: Select all

[preDispatch] cwd: C:\htdocs\dealer
>
[preDispatch] drw- C:\htdocs\dealer/dealer
>
[preDispatch] drw- C:\htdocs\dealer/dealer/default
>
[preDispatch] drw- C:\htdocs\dealer/dealer/default/views
>
[preDispatch] drw- C:\htdocs\dealer/dealer/default/views/scripts
>
[preDispatch] drw- C:\htdocs\dealer/dealer/default/views/scripts/elements
>
[preDispatch] -rw- C:\htdocs\dealer/dealer/default/views/scripts/elements/header.phtml
>

object(Zend_View)#5 (12) {
  ["_path:private"] => array(3) {
    ["script"] => array(1) {
      [0] => string(46) "C:\htdocs\dealer\dealer\default\views\scripts\"
    }
    ["helper"] => array(2) {
      [0] => array(2) {
        ["prefix"] => string(17) "Zend_View_Helper_"
        ["dir"] => string(46) "C:\htdocs\dealer\dealer\default\views\helpers\"
      }
      [1] => array(2) {
        ["prefix"] => string(17) "Zend_View_Helper_"
        ["dir"] => string(40) "C:\phplib\Zend\library\Zend\View\Helper\"
      }
    }
    ["filter"] => array(2) {
      [0] => array(2) {
        ["prefix"] => string(17) "Zend_View_Filter_"
        ["dir"] => string(46) "C:\htdocs\dealer\dealer\default\views\filters\"
      }
      [1] => array(2) {
        ["prefix"] => string(17) "Zend_View_Filter_"
        ["dir"] => string(40) "C:\phplib\Zend\library\Zend\View\Filter\"
      }
    }
  }
  ["_file:private"] => NULL
  ["_helper:private"] => array(0) {
  }
  ["_helperLoaded:private"] => array(0) {
  }
  ["_helperLoadedDir:private"] => array(0) {
  }
  ["_filter:private"] => array(0) {
  }
  ["_filterClass:private"] => array(0) {
  }
  ["_filterLoaded:private"] => array(0) {
  }
  ["_filterLoadedDir:private"] => array(0) {
  }
  ["_escape:private"] => string(12) "htmlentities"
  ["_encoding:private"] => string(5) "UTF-8"
  ["_strictVars:private"] => bool(true)
}


Fatal error: Uncaught exception 'Zend_View_Exception' with message 'script 'elements/footer.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/footer...') #1 C:\htdocs\dealer\dealer\extends\MC2\Controller\Plugin\HeadFoot.php(54): Zend_View_Abstract->render('elements/footer...') #2 C:\phplib\Zend\library\Zend\Controller\Plugin\Broker.php(259): MC2_Controller_Plugin_HeadFoot->postDispatch(Object(Zend_Controller_Request_Http)) #3 C:\phplib\Zend\library\Zend\Controller\Front.php(900): Zend_Controller_Plugin_Broker->postDispatch(Object(Zend_Controller_Request_Http)) #4 C:\htdocs\dealer\dealer\bootstrap.php(82): 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
User avatar
Luke
The Ninja Space Mod
Posts: 6424
Joined: Fri Aug 05, 2005 1:53 pm
Location: Paradise, CA

Post by Luke »

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?
Post Reply