开发者

Overwrite zend view directory?

开发者 https://www.devze.com 2023-01-04 13:37 出处:网络
In my bootstrap, I detect the site the users on, then issue the layout change and helper change. Essentially anything that is there by default is what the site falls back on if there is no per site ve

In my bootstrap, I detect the site the users on, then issue the layout change and helper change. Essentially anything that is there by default is what the site falls back on if there is no per site version in /public/site/

However I cannot get the actual view to rely on a path a give it or a path in /public/site.

Here is my code so far:

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 = $q开发者_JS百科uery->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;
    }

Essentially when landing on the site, Zend_View should check to see if /public/site/1/layouts/view/scripts/default/index/index.phtml exists and use it, if it doesn't then use /application/modules/default/views/scripts/index/index.phtml and use it.


Zend_View can have multiple script paths, and it will check them in (reverse) order until it finds the template it's looking for.

There maybe a tidier way to do this but for the layout, in the Bootstrap:

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

replace path1 and path2 with the paths to use.

For view scripts, assuming you are using MVC you need to manipulate the script paths on the view renderer. E.g.:

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

personally I do this in a controller plugin - you could try doing it in the Bootstrap but I seem to remember having some weird issues with this.

Remember that the paths are checked in reverse order, so for your case path2 should be the domain one, and path1 the default.


you will need to extend Zend_View and implement something like a fallback script directory before throwing an exception when trying to load a viewscript.


Okay so heres how I got it working:

Bootstrap:

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

Plugin:

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

    }

}
0

精彩评论

暂无评论...
验证码 换一张
取 消