views:

64

answers:

3

Hello,

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

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.

A: 

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.

zolex
That is an option but I'm unsure how
azz0r
A: 

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.

Tim Fountain
Hello,The layout overwrite works perfectly thanks.However the viewrenderer piece of code doesn't work. Zend_Controller_Action_Helper_Broker doesnt exist for me? Care to share your plugin?
azz0r
Sorry, there was a typo in my code example (I'd put Zend_Controller_Action_Helper_Broker when it should be Zend_Controller_Action_HelperBroker). I've edited my code to correct this. As long as you're using the autoloader what's there should work.
Tim Fountain
What would the current default path be?/application/modules/:module/scripts/views/:controller/:action.phtml ?
azz0r
The controller/action gets appended automatically so it would be /application/modules/:module/views/scripts/
Tim Fountain
Okay, have looked at your code now, I think you want:$viewRenderer->view->setScriptPath(array( APPLICATION_PATH.'/modules/:module/scripts/views/', PUBLIC_PATH.'/site/'.$site->id.'/layouts/templates/modules/:module/scripts/views/'));I'm assuming :module gets replaced automatically (I've never tried this). Also the default ZF structure uses /views/scripts/ but you seem to be using /scripts/views/, so you might want to double check your paths.
Tim Fountain
A: 
azz0r