Zend Framework changes view location in plugin

I can’t find a solution to my problem anywhere, whether on SO or in real zend documentation.

I basically have this setting:

  • I created a plugin that uses Zend_Http_UserAgent and WURFL to determine if the user is using a mobile phone. This is done in advance. It works great

  • Now I want to change the catalog of view scripts if the user is using mobile.

  • In an ideal world, I would like to try and download the mobile version of the view, if it exists, if not download the default view. But if I can get 2, I work as a minimum, I will be happy.

I'm more than dumb about how I do it. I saw that I can use:

$ view = Zend_Controller_Action_HelperBroker :: getStaticHelper ('viewRenderer') → view; $ view-> setBasePath (APPLICATION_PATH. '/ mobile_views /');

But that doesn't seem to do what I expect, plus does this happen in postDispatch when I think such a thing should happen in preDispatch?

Here is my current plugin:

<?php

class SQ_Plugins_Mobile extends Zend_Controller_Plugin_Abstract {

public function dispatchLoopStartup(Zend_Controller_Request_Abstract $flag) {

    $bootstrap  = Zend_Controller_Front::getInstance()->getParam("bootstrap");
    $useragent  = $bootstrap->getResource("useragent");
    $device     = $useragent->getDevice();

    Zend_Registry::set("useragent", $useragent);
    Zend_Registry::set("device", $device);

    echo $device->getType() . " is the type of device";

    if($device->getType() != "mobile") {
        /**
         * Set the layout to be mobile, here we make sure we streamline what is loaded
         * so as to not load things that arent needed.
         */
        Zend_Layout::getMvcInstance()->setLayout("mobile");


        /**
         * Here i wish to change the view path to be APPLICATION_PATH . "/mobile_views/"
         * @todo Change the view script path 
         */

    }
}

public function postDispatch(Zend_Controller_Request_Abstract $request) {

    /**
     * Maybe i have to change the view script path here?
     * @todo change the viewscript path if we're using a mobile
     */

    // Get the current view
    $view = Zend_Controller_Action_HelperBroker::getStaticHelper('viewRenderer')->view;
    $view->setBasePath(APPLICATION_PATH . '/mobile_views/');
    echo "kl;kl;kjhkl;jhlkjhjklhkl;h k;hi";
}

}

+3
source share
2 answers

In your action, you can do this if it is a mobile version of the page.

$this->view->addBasePath('../application/views/mobile'); // Or whatever your path may be.

Just a thought.

+1
source

OK, so I fixed it, although I'm not sure if this is the best "zend" solution, but I think it is quite elegant and works, which is the best.

, , , . , , , , .

class SQ_Plugins_Mobile extends Zend_Controller_Plugin_Abstract {

public function dispatchLoopStartup(Zend_Controller_Request_Abstract $request) {

    $bootstrap  = Zend_Controller_Front::getInstance()->getParam("bootstrap");
    $useragent  = $bootstrap->getResource("useragent");
    $device     = $useragent->getDevice();

    Zend_Registry::set("useragent", $useragent);
    Zend_Registry::set("device", $device);

    /**
     * @todo change this to be Mobile 
     */
    if($device->getType() != "mobile") {

        /**
         * Set the layout to be mobile, here we make sure we streamline what is loaded
         * so as to not load things that arent needed.
         */
        Zend_Layout::getMvcInstance()->setLayout("mobile_layout");

        /**
         * Here we check to see if a mobile version of the template exists.  if it does then we change the view suffix
         * this allows us to load the mobile view if it exists and the defgault view if it doesnt. 
         */
        $base       = APPLICATION_PATH . "/views/scripts/";
        $mobile     = $base .  $request->getControllerName() . "/" . $request->getActionName() . ".mobile.phtml";

        if(is_readable($mobile)) {
            Zend_Controller_Action_HelperBroker::getExistingHelper('ViewRenderer')->setViewSuffix('mobile.phtml');      
        } 

    }
}
}
0

All Articles