zend-frameworkzend-viewzend-layout

Zend default view script name switch


I want to display a mobile view instead of the normal view script.

Let's take for example this action /my_module/my_controller/my_action.

When I call it, based on some headers processing I know that user agent is a mobile device. Then I set the main layout to be a mobile-specific layout. I do that like this and it works fine: Zend_Layout::getMvcInstance()->setLayout('mobile');

But in my controller or somewhere else I want to override the default view file my_action.phtml on something like my_action_mobile.phtml.

How that could be done considering that in some of my views I am using the action helper as this: <?php echo $this->action("index", "index", "items")?> or is some of my action I have a _forward like this: $this->_forward("index", "auth", "default");

I have tried with setViewSuffix('mobile') and with several file name variants as: my_action.mobile.phtml, or setViewSuffix('mobile.phtml') and the file called my_action.mobile.phtml and others, resulting in a blank page - no error, nothing. ( i have no ideea why because it is set to display the thrown errors )

Also I have tried with contextSwitching but it disables my main layout - i do not want this - and it feels like it does not handles properly the action view helper. ( a view rendered in another )

I would appreciate any suddgestion. Thank you!


Solution

  • There is an awful lot of information on the subject. For starters get youself throught http://static.zend.com/topics/Mobile-web-app-with-ZF.pdf

    In a nutshell, you should use ContextSwitch.

    1. You create a plugin that defines available contexts and format parameter derived from Zend_Http_UserAgent.
    2. In controller you describe which actions are eligible for those contexts.
    3. Create different views for those actions.