覆盖zend视图目录?

时间:2010-06-22 12:45:37

标签: zend-framework zend-view

在我的引导程序中,我检测到用户所在的站点,然后发出布局更改和帮助程序更改。基本上任何默认情况下,如果/ 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并使用它。

3 个答案:

答案 0 :(得分:1)

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

可能有一个更整洁的方法,但对于布局,在Bootstrap:

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

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

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

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

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

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

答案 1 :(得分:0)

在尝试加载viewscript时抛出异常之前,您需要扩展Zend_View并实现类似回退脚本目录的内容。

答案 2 :(得分:0)

好的,这就是我如何运作:

自举:

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) {

    }

}