Zend Framework View Path

Not for 'how-to' coding questions but PHP theory instead, this forum is here for those of us who wish to learn about design aspects of programming with PHP.

Moderator: General Moderators

Post Reply
User avatar
John Cartwright
Site Admin
Posts: 11470
Joined: Tue Dec 23, 2003 2:10 am
Location: Toronto
Contact:

Zend Framework View Path

Post 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?
User avatar
Maugrim_The_Reaper
DevNet Master
Posts: 2704
Joined: Tue Nov 02, 2004 5:43 am
Location: Ireland

Post 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.
User avatar
John Cartwright
Site Admin
Posts: 11470
Joined: Tue Dec 23, 2003 2:10 am
Location: Toronto
Contact:

Post by John Cartwright »

Yea I was hoping that wasn't the case. I wanted to avoid littering the the bootstrap, grr..

Thanks Maugrim. 8)
User avatar
Christopher
Site Administrator
Posts: 13596
Joined: Wed Aug 25, 2004 7:54 pm
Location: New York, NY, US

Post 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.
(#10850)
User avatar
John Cartwright
Site Admin
Posts: 11470
Joined: Tue Dec 23, 2003 2:10 am
Location: Toronto
Contact:

Post 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 ;)
Post Reply