在插件中添加zend视图的路径



我正试图在preDispatch插件中添加一个新的路径到我的视图脚本。

我的最终目标是在同一个Zend框架应用程序中运行几个站点,每个站点都有自己的布局和视图脚本。然而,我希望使用默认的布局/视图,如果一个网站没有自己的视图/布局。

我已经得到了正确的布局工作,但我不能得到视图脚本加载。

我的视图文件夹布局如下

/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

我想要实现的是在domingod/index/index索引视图脚本。如果这个文件没有找到我想要的默认索引。

我已经试过了,但是没有运气

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

这似乎添加了路径,但ZF仍然呈现默认索引。而不是站点(domingod)之一。

我希望这一切都是有意义的,提前感谢。

加里

我想到的解决方案是检查网站是否有视图脚本,如果它已经设置为渲染。

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);
        }
}

实际上它总是查找index/index. php。这是基本动作和基本控制器。如果你想达到你的目标,我认为你应该在那里设置重定向,并进行一些检查。

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

但在我看来,你应该知道你的网站上有什么,你没有什么。如果网站不存在,设置一个重定向链接,你的网站就会像瑞士时钟一样工作。

最新更新