【问题标题】:Overwrite zend view directory?覆盖zend视图目录?
【发布时间】:2010-06-22 12:45:36
【问题描述】:

在我的引导程序中,我检测用户所在的站点,然后发出布局更改和助手更改。基本上,如果 /public/site/ 中没有每个站点的版本,则默认情况下存在的任何内容都是站点所依赖的内容

但是,我无法让实际视图依赖于给定路径或 /public/site 中的路径。

到目前为止,这是我的代码:

protected function _initSite() {

        $this->bootstrap('db');
        $db             = $this->getPluginResource('db')->getDbAdapter();
        $site_domain    = strtolower($_SERVER['HTTP_HOST']);

        //site
        $query = $db->prepare("
            SELECT s.*, so.* 
            FROM Site AS s
            JOIN Site_Domain AS sd ON sd.site_id = s.id
            JOIN Site_Option AS so ON so.site_id = s.id 
            WHERE sd.domain = :site_domain AND s.enabled = '1'
            LIMIT 1
        ");
        $query->bindValue('site_domain', $site_domain);
        $query->execute();
        $site = $query->fetch(PDO::FETCH_OBJ);
        $query->closeCursor();

        if (empty($site)) {
            throw new exception('Unfortunately we were unable to load the site you requested via the domain you came to.');
        }

        //site definitions - we need to get away from defining global variables, so lazy
        define('SITE_ID', $site->id);

        //layout paths
        Zend_Layout::startMvc(array(
            'layout' => 'layout',
            'layoutPath' => array(
                APPLICATION_PATH.'/layouts/scripts/',
                PUBLIC_PATH.'/site/'.$site->id.'/layouts/scripts/'
            )
        ));



        $view = $this->getResource('view');
        //set site to view, we use alot of google anayltics code so
        $view->site = $site;
        //set title seperator
        $view->headTitle($site->title)->setSeparator(' - ');
        //add base path for layout overriding
        $view->addBasePath(APPLICATION_PATH.'/modules/:module/views/');
        //register view helper path
        $view->addHelperPath('My/View/Helper/', 'My_View_Helper_');

        //register partials fallback path
        $view->addScriptPath(APPLICATION_PATH.'/layouts/partials');
        //default partials path
        $view->addScriptPath(PUBLIC_PATH.'/site/'.$site->id.'/layouts/partials/');//the default helpers

        //bind objects to the registry
        Zend_Registry::set('config',    array('meta_description' => $site->meta_description, 'meta_keywords' => $site->meta_keywords));
        Zend_Registry::set('site',      $site);
        return $site;
    }

基本上在登陆站点时,Zend_View 应该检查 /public/site/1/layouts/view/scripts/default/index/index.phtml 是否存在并使用它,如果不存在则使用 /application /modules/default/views/scripts/index/index.phtml 并使用它。

【问题讨论】:

    标签: zend-framework zend-view


    【解决方案1】:

    Zend_View 可以有多个脚本路径,它会以(反向)顺序检查它们,直到找到它正在寻找的模板。

    除了布局,在 Bootstrap 中可能有更简洁的方法:

    protected function _initView()
    {
        Zend_Layout::startMvc(array(
            'layoutPath' => array(path1, path2),
            'layout' => 'default'
        ));
    }
    

    将 path1 和 path2 替换为要使用的路径。

    对于视图脚本,假设您使用的是 MVC,您需要在视图渲染器上操作脚本路径。例如:

    $viewRenderer = Zend_Controller_Action_HelperBroker::getStaticHelper('ViewRenderer');
    $viewRenderer->view->setScriptPath(array(
        path1,
        path2
    ));
    

    我个人在控制器插件中执行此操作 - 您可以尝试在 Bootstrap 中执行此操作,但我似乎记得有一些奇怪的问题。

    请记住,路径是以相反的顺序检查的,因此对于您的情况,path2 应该是域之一,而 path1 是默认值。

    【讨论】:

    • 您好,布局覆盖效果很好,谢谢。但是,viewrenderer 代码段不起作用。 Zend_Controller_Action_Helper_Broker 对我来说不存在吗?愿意分享您的插件吗?
    • 抱歉,我的代码示例中有一个错字(当它应该是 Zend_Controller_Action_HelperBroker 时,我会放置 Zend_Controller_Action_Helper_Broker)。我已经编辑了我的代码来纠正这个问题。只要您使用自动加载器,那里应该可以工作。
    • 当前的默认路径是什么? /application/modules/:module/scripts/views/:controller/:action.phtml ?
    • 控制器/动作会自动附加,所以它会是 /application/modules/:module/views/scripts/
    • 好的,现在已经查看了您的代码,我想您想要: $viewRenderer->view->setScriptPath(array( APPLICATION_PATH.'/modules/:module/scripts/views/', PUBLIC_PATH. '/site/'.$site->id.'/layouts/templates/modules/:module/scripts/views/' ));我假设 :module 会自动替换(我从未尝试过)。此外,默认的 ZF 结构使用 /views/scripts/,但您似乎使用的是 /scripts/views/,因此您可能需要仔细检查您的路径。
    【解决方案2】:

    您需要扩展 Zend_View 并在尝试加载视图脚本时抛出异常之前实现类似后备脚本目录。

    【讨论】:

    • 这是一个选项,但我不确定如何
    【解决方案3】:

    好的,这就是我的工作原理:

    引导程序:

    protected function _initSite() {
    
        $this->bootstrap('db');
        $db             = $this->getPluginResource('db')->getDbAdapter();
        $site_domain    = strtolower($_SERVER['HTTP_HOST']);
    
        //site
        $query = $db->prepare("
            SELECT s.*, so.* 
            FROM Site AS s
            JOIN Site_Domain AS sd ON sd.site_id = s.id
            JOIN Site_Option AS so ON so.site_id = s.id 
            WHERE sd.domain = :site_domain AND s.enabled = '1'
            LIMIT 1
        ");
        $query->bindValue('site_domain', $site_domain);
        $query->execute();
        $site = $query->fetch(PDO::FETCH_OBJ);
        $query->closeCursor();
    
        if (empty($site)) {
            throw new exception('Unfortunately we were unable to load the site you requested via the domain you came to.');
        }
    
        //site definitions - we need to get away from defining global variables, so lazy
        define('SITE_ID', $site->id);
    
        //view helper
        $view = $this->getResource('view');
        $view->addHelperPath('My/View/Helper/', 'My_View_Helper_');
    
        //view plugin
        $front  = $this->getResource('FrontController');
        $plugin = new My_Controller_Plugin_View();
        $front->registerPlugin($plugin);
    
        Zend_Registry::set('config',    array('meta_description' => $site->meta_description, 'meta_keywords' => $site->meta_keywords));
        Zend_Registry::set('site',      $site);
        return $site;
    }
    

    插件:

    class My_Controller_Plugin_View extends Zend_Controller_Plugin_Abstract {
    
    
        public function routeShutdown(Zend_Controller_Request_Abstract $request) {
    
            $this->bootstrap = Zend_Controller_Front::getInstance()->getParam('bootstrap');
            $view = $this->bootstrap->getResource('view');
            $site = Zend_Registry::get('site');
    
            //layout paths
            Zend_Layout::startMvc(array(
                'layout' => 'layout',
                'layoutPath' => array(
                    APPLICATION_PATH.'/layouts/scripts/',
                    PUBLIC_PATH.'/site/'.$site->id.'/layouts/scripts/'
                )
            ));
    
            $front = Zend_Controller_Front::getInstance();
            $request = $front->getRequest();
            $module = $request->getModuleName();
    
            //set site to view, we use alot of google anayltics code so
            $view->site = $site;
    
            //set title seperator
            $view->headTitle($site->title)->setSeparator(' - ');
    
            //add base path for layout overriding
            //$view->addBasePath(APPLICATION_PATH.'/modules/:module/views/');
    
            //register partials fallback path
            $view->setScriptPath(NULL);
            //$view->setScriptPath(array());
            $view->addBasePath(APPLICATION_PATH.'/modules/'.$module.'/views/');
            $view->addBasePath(PUBLIC_PATH.'/site/'.$site->id.'/layouts/templates/'.$module);
            $view->addScriptPath(APPLICATION_PATH.'/layouts/partials');
            $view->addScriptPath(PUBLIC_PATH.'/site/'.$site->id.'/layouts/partials/');
    
            //print_r($view);exit;
    
            $viewRenderer = Zend_Controller_Action_HelperBroker::getStaticHelper('ViewRenderer');
            //$viewRenderer->setViewBasePathSpec(PUBLIC_PATH.'/site/'.$site->id.'/layouts/templates/:module/')->initView();
    
        }
    
    
        public function preDispatch(Zend_Controller_Request_Abstract $request) {
    
        }
    
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-01-19
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多