Zend Framework: how to change the default layout of a script to something other than layout.phtml?

I would like to call my default layout file something other than layout.phtml, as it does not actually describe the layout type. How can i do this? Thanks!

+4
source share
2 answers

From the Bootstrap.php file, you can do something like this:

protected function _initLayoutName() { // use sitelayout.phtml as the main layout file Zend_Layout::getMvcInstance()->setLayout('sitelayout'); } 

If you want to use a different layout for another module, you need to register the plugin in Bootstrap and include the plugin in the following code:

 class Application_Plugin_LayoutSwitcher extends Zend_Controller_Plugin_Abstract { public function dispatchLoopStartup(Zend_Controller_Request_Abstract $request) { $module = $request->getModuleName(); // get the name of the current module if ('admin' == $module) { // set the layout to admin.phtml if we are in admin module Zend_Layout::getMvcInstance()->setLayout('admin'); } else if ('somethingelse' == $module) { Zend_Layout::getMvcInstance()->setLayout('somethingelse'); } } } 

Inside your .ini application, you can do this to install a script layout:

 resources.layout.layout = "layoutname" 

However, this will not work for each layout. If you need to change the layout based on the module, you will have to use the plugin, but you can use the parameter in application.ini to set the default layout name.

+6
source

If you want to have a specific layout depending on your modules, you can create a plugin and register it in your booklet:

 <?php class Plugin_LayoutModule extends Zend_Controller_Plugin_Abstract { /** * preDispatch function. * * Define layout path based on what module is being used. */ public function preDispatch(Zend_Controller_Request_Abstract $request) { $module = strtolower($request->getModuleName()); $layout = Zend_Layout::getMvcInstance(); if ($layout->getMvcEnabled()) { $layout->setLayoutPath(APPLICATION_PATH . '/modules/' . $module . '/layouts/'); $layout->setLayout($module); } } } //Register it in your bootstrap.php <?php defined('APPLICATION_PATH') or define('APPLICATION_PATH', dirname(__FILE__)); ... Zend_Layout::startMvc(); $frontController->registerPlugin(new Plugin_LayoutModule()); ?> 

EDIT:

to set the layout to another file using the .ini file:

create a layout.ini file and paste it:

 [layout] layout = "foo" layoutPath = "/path/to/layouts" contentKey = "CONTENT" 

in your bootstrap file:

 $config = new Zend_Config_Ini('/path/to/layout.ini', 'layout'); $layout = Zend_Layout::startMvc($config); 
+3
source

Source: https://habr.com/ru/post/1394711/


All Articles