Page 1 of 1

Zend Framework View Path

Posted: Fri Apr 13, 2007 8:53 pm
by John Cartwright
We'll I've been closely following the PetShop demo thread, and have begun to implement some changes on my own build. However, I've run into a slight problem when using the directory structure proposed in the thread.

Right now I have

Code: Select all

/Application 
   bootstrap.php
   /Default
   /Library
      /Zend
      /Northern
         View.php
My bootstrap sets the filepaths, as suggested in the thread

Code: Select all

// we locate the application dir via this bootstrap file
$appDir = dirname(__FILE__);

/**
 * Include path setup (assuming no facility to edit php.ini)
 * Also assumed only Core ZF. Includes the Zps parent directory.
 * Left out Models for now.
 */
set_include_path(
    $appDir . PATH_SEPARATOR
    . $appDir . '/Library' . PATH_SEPARATOR
    . get_include_path()
);
My view file sets the filepath in the constructor,

Code: Select all

public function __construct(Zend_Controller_Request_Http $request, Zend_Session_Namespace $session)
	{
		$this->setEncoding('utf-8');
		$this->setScriptPath($request->getModuleName().'/Views/');

		$this->session = $session;			
		$this->baseurl = $request->getRequestUri();
	}
However I've gotten an exception saying the view cannot be found, fatal error. When I prepend '/Application/ to the filepath it works fine. Does anyone have any idea why the include path isn't working with my views?

Posted: Sat Apr 14, 2007 4:33 am
by Maugrim_The_Reaper
I'm guessing it's just that relative paths aren't all that reliable without a fixed base. I'll add it to the bootstrap later, but you can add the $AppDir value to the Registry and use from within your constructor. So:

Code: Select all

public function __construct(Zend_Controller_Request_Http $request, Zend_Session_Namespace $session)
        {
                $appDir = Zend_Registry::getInstance()->get('applicationDirectory');

                $this->setEncoding('utf-8');
                $this->setScriptPath($appDir . DIRECTORY_SEPARATOR . $request->getModuleName() . '/Views');

                $this->session = $session;                 
                $this->baseurl = $request->getRequestUri();
        }
Just amend the bootstrap to register the application path. Also if you use the scripts/filters/helpers convention you can use setBasePath() instead of setScriptPath to set up the standard convention locations for everything a View needs - not needed though if you prefer putting the templates into the main View directory.

Posted: Sat Apr 14, 2007 12:04 pm
by John Cartwright
Yea I was hoping that wasn't the case. I wanted to avoid littering the the bootstrap, grr..

Thanks Maugrim. 8)

Posted: Sat Apr 14, 2007 1:12 pm
by Christopher
Perhaps we can find a way, in extending the Action class, to deal with this problem cleanly. I is one of several problems with their cobbled together architecture.

Posted: Sat Apr 14, 2007 2:03 pm
by John Cartwright
Indeed, I am waiting until the example shop thread to get to that point before I jump in there. So don't count me out just yet ;)