Zend framework multi module multi layout configuration

  • 2020-03-31 21:36:22
  • OfStack

Many people encounter this and that in the process of using, and zend framework has now reached version 1.11, a lot of information on the network is still on the old version, so I take the current version of 1.11 as an example, a simple introduction to how to use the zend framework to create modular applications. Due to the framework version upgrades in the future, some content may be out of date, please refer to the latest (link: http://framework.zend.com/manual/zh/).

1. Preparation

Let's say you've deployed the web server and PHP, downloaded the latest version of the zend framework, created a primitive zend framework project, and now have access to the default actions. You can use the zend framework tools to create projects, specific operation see (link: http://framework.zend.com/manual/zh/learning.quickstart.create-project.html). Of course can also manually folders and files, see (link: http://framework.zend.com/manual/zh/project-structure.html).

Take a quick look at a few important directories by default.
The first is public, which not only stores the entry point of the program, index.php, but also can store images, CSS, javascript files, etc.
The second is library, which is used to store some class libraries, including those you define yourself or those of third parties.
Then there's test, which is for unit tests and other test files.
Finally, application is the directory that is most relevant to what we are going to talk about here. When you go into the application directory, you will find the following directory:
Configs: to store configuration files, there is usually a main configuration file application.ini;
Controllers: controllers, such as the default indexcontroller.php;
Models: store business logic, data models, etc.
Views: view layer script, usually with. PHTML suffix;
Modules: a directory of modules that is not automatically generated using the default options of the tool. A folder represents a module. The directory structure under it is similar to the application directory. It can also contain directories such as controllers, models, views, etc. It is important to note under the module of controllers below please add module name prefix, the class name of the file such as application/modules/admin/controllers/IndexController PHP class called Admin_IndexController.

If you need to easily use your own libraries (such as the namespace Rockux) or third-party libraries in your project, you can modify the application.ini file by adding the following lines:

 
autoloaderNamespaces.rockux = "Rockux_" 
autoloaderNamespaces.thirdParty = "ThirdPartyLibrary_" 

Of course, you can add as many as you like, but notice the underscore at the end.

2. Build a module
Now let's create an admin module with the following directory:
Application/modules/admin/controllers
Application/modules/admin/models
Application/modules/admin/views
Application/modules/admin/views/scripts
Application/modules/admin/views/helpers
Application/modules/admin/views/filters
And create the following files:
Application/modules/admin/controllers/IndexController PHP (class called Admin_IndexController)
Application/modules/admin/views/scripts/index/index. The PHTML

In addition to creating a new module file, you also need to change the configuration file application.ini by deleting the following lines, if any:
 
resources.frontController.controllerDirectory = APPLICATION_PATH"/controllers" 

Add the following line:
 
resources.frontController.moduleDirectory = APPLICATION_PATH "/modules" 
resources.frontController.moduleControllerDirectoryName = "controllers" 
resources.frontController.defaultModule = "default" 
resources.modules[] 

Now, if you go to http://localhost/admin, you should see the output of the admin module.
If we want to make full use of the module's power, we also need to add a boot file for the module -- bootstrap.php. It allows you to easily use class resources, models, filters, helpers, etc. Create new bootstrap.php under admin, the code is as follows:
 
class Admin_Bootstrap extends Zend_Application_Module_Bootstrap 
{ 
} 

And in the application/Bootstrap. PHP file to join the following methods:
 
protected function _initAppAutoload() 
{ 
$autoloader = new Zend_Application_Module_Autoloader(array( 
'namespace' => 'App', 
'basePath' => dirname(__FILE__), 
)); 
return $autoloader; 
} 

 
resources.layout.layoutPath = APPLICATION_PATH "/layouts/scripts" 
resources.layout.layout = "layout" 
admin.resources.layout.layout = "admin" 

Second, the layout script files for different modules are stored in their respective module folders
You can create the following directories and files under application:
Application/layouts/scripts/layout. PHTML
Application/modules/admin/layouts/scripts/layout PHTML

Add the following lines to the configuration file application.ini:
 
resources.layout.layoutPath = APPLICATION_PATH "/layouts/scripts" 
resources.layout.layout = "layout" 
admin.resources.layout.layoutPath = APPLICATION_PATH "/modules/admin/layouts/scripts" 

Either way, if you visit http://localhost/admin, you will find that the system does not use the expected admin.phtml as the layout file, but the default layout.phtml. This is because the admin row configuration is not a valid configuration that the system can handle by default, so we'll handle it ourselves.

Our new file: library/Rockux/Controller/Action/Helper/LayoutLoader. PHP,

For the first case, the code is as follows:
 
class Rockux_Controller_Action_Helper_LayoutLoader extends Zend_Controller_Action_Helper_Abstract 
{ 

public function preDispatch() 
{ 
$bootstrap = $this->getActionController() 
->getInvokeArg('bootstrap'); 
$config = $bootstrap->getOptions(); 
$module = $this->getRequest()->getModuleName(); 
if (isset($config[$module]['resources']['layout']['layout'])) { 
$layoutScript = $config[$module]['resources']['layout']['layout']; 
$this->getActionController() 
->getHelper('layout') 
->setLayout($layoutScript); 
} 
} 

} 

For the second case, the code is as follows:
 
class Rockux_Controller_Action_Helper_LayoutLoader extends Zend_Controller_Action_Helper_Abstract 
{ 

public function preDispatch() 
{ 
$bootstrap = $this->getActionController() 
->getInvokeArg('bootstrap'); 
$config = $bootstrap->getOptions(); 
$module = $this->getRequest()->getModuleName(); 
if (isset($config[$module]['resources']['layout']['layoutPath'])) { 
$layoutPath = 
$config[$module]['resources']['layout']['layoutPath']; 
$this->getActionController() 
->getHelper('layout') 
->setLayoutPath($layoutPath); 
} 
} 
} 

Next, we also need to add it to the application/Bootstrap. PHP
 
protected function _initLayoutHelper() 
{ 
$this->bootstrap('frontController'); 
$layout = Zend_Controller_Action_HelperBroker::addHelper( 
new Rockux_Controller_Action_Helper_LayoutLoader()); 
} 

If you visit http://localhost/admin again, you should see that the specified layout file is used.
If you want to use a particular layout for a particular controller, you can add the following code to the controller's init() method:
 
$layout = Zend_Layout::getMvcInstance(); 
$layout->setLayout('layout_special'); 


Related articles: