With Zend Framework, you may have the need to know the directory name of the currently executing view script. For example, I have page nicktest.php, which uses nickTestController.php and view scripts in directory /nick-test/. In my case, I wanted to know the name of this directory from my layout so I could include additional meta data on the page if the file existed within /nick-test/. Unfortunately searching Google did not provide me with the answer and after hours of debugging and stepping through Zend’s MVC, I found the solution.
In your bootstrap, find where the view renderer is instantiated, like this
1 2 3 4 5 6 7 8 9 | /** * setup the view layer */ public static function setupView() { self::$view = new Zend_View(); self::$view->setEncoding('UTF-8'); self::$viewRenderer = new Zend_Controller_Action_Helper_ViewRenderer(self::$view); Zend_Controller_Action_HelperBroker::addHelper(self::$viewRenderer); } |
Be sure to assign the $viewRenderer to self and create the public static variable within the Bootstrap class so we can access it from our layout or a controller. Then, in your controller, the below code will return “/nick-test/index.phtml” or whatever action is being processed.
1 | $nameAndDirectoryOfThisPage = Bootstrap::$viewRenderer->getViewScript(); |
Continue Reading
Hi I'm Nick Bartlett and thanks for visiting my blog. I'm not much of a writer; many of my posts are short and to the point while others are meant to be a reference for myself and other web developers.