0
votes

I am trying to add a new path to my view scripts in a preDispatch plugin.

My ultimate goal is to run several sites from the same Zend Framework application each with their own layout and view scripts. However I wish to use the default layout/views where a site does not have its own view/layout.

I have got the layout to work correctly but I cannot get the view scripts to load.

My views folder layout is as follows

/application
    /modules
        /views
            /scripts
                /index
                    index.phtml -> the default index controller view script
                /sites
                    /domingod -> this is a site
                        /index
                            /index.phtml -> the sites index controller view script

What I am trying to achieve is for the index view script in domingod/index/index.phtml to be loaded, if this file is not found the I want the default index.phtml view script to render.

I have tried this so far but with no luck

Zend_Layout::getMvcInstance()->getView()->setScriptPath($viewScriptPath);

This seems to add the path but ZF is still rendering the default index.phtml rather than the sites (domingod) one.

I hope this all makes sense, many thanks in advance.

Garry

2

2 Answers

1
votes

The solution I came up with was to check if the site had a view script, if it had then set that to render.

class FreedomS_Zend_Controller_Plugin_SetView extends Zend_Controller_Plugin_Abstract
{
    public function preDispatch(Zend_Controller_Request_Abstract $request)
        {
            $module = $request->getModuleName();
            $controller = $request->getControllerName();
            $action = $request->getActionName();

            $viewScriptPath = APPLICATION_PATH . '/modules/' . $module . '/views/scripts/sites/' . Zend_Registry::get('siteId') . '/';

            $view = Zend_Controller_Action_HelperBroker::getExistingHelper('ViewRenderer')->view;
            if (file_exists($viewScriptPath . $controller . '/' . $action . '.phtml'))
                $view->addScriptPath($viewScriptPath);
        }
}
0
votes

Actually it always looks for index/index.phtml. This is base action and base controller. If you want to achive your goal, I think you should set redirection there with some checking.

$this->_redirect(   $this
                            ->view
                            ->url(array('controller' => 'domingod',
                                        'action' => 'index')));
            return;

But in my humble opinion, you should know for sure what you have on your site and what you dont. If site doesn't have to exist, set there a redirection link, and your web site would work as a swiss clock.