Home  >  Article  >  Backend Development  >  zend framework multi-module multi-layout configuration_PHP tutorial

zend framework multi-module multi-layout configuration_PHP tutorial

WBOY
WBOYOriginal
2016-07-21 15:32:01731browse

Many people will encounter problems of this kind during use, and zend framework has now reached version 1.11. A lot of information on the Internet is still on the old version, so I will take the latest version 1.11 as an example here. Let’s briefly introduce how to use zend framework to create modular applications. Due to future framework version upgrades, some content may be outdated, so please refer to the latest user manual in a timely manner.

1. Preparation

First, assume that you have deployed the web server and PHP, downloaded the latest version of zend framework, created an original zend framework project, and can access the default actions. You can use the zend framework tool to create a project. For details, see Creating a Project Using the zend Framework. Of course, you can also manually create folders and files yourself. See the project directory structure recommended by zend framework.

Briefly take a look at several important default directories.
The first is public, which not only stores the program’s entry point index.php, but can also store images, css, javascript files, etc.
The second is library, which is used to store some class libraries, including your own defined or third-party class libraries.
Then there is test, which is used to store test files such as unit tests.
Finally, it is also the directory most closely related to what we are going to talk about here - application. Enter the application directory, and there will be the following directories:
configs: to store configuration files, usually there will be a main configuration file application.ini;
controllers: controller, such as the default IndexController.php;
models: stores business logic, data models and other files;
views: scripts of the view layer, usually with .phtml as the suffix;
modules: module directory, which is automatically generated using the tool's default options and does not have this directory. Need to be added manually. Modules can contain multiple folders named after the module name, such as admin. The default is default. One folder represents a module. The directory structure under it is similar to the application directory, and can also include directories such as controllers, models, views, etc. It should be noted that the class name of the file under the controllers under the module must be prefixed with the module name. For example, the class name of application/modules/admin/controllers/IndexController.php is Admin_IndexController.

If you need to conveniently use some class libraries you wrote yourself (for example, the namespace is Rockux), or third-party class libraries, you can modify the application.ini file and add the following lines:

Copy code The code is as follows:

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

Of course you can add a few more as needed, but please pay attention to the underscore at the end.

2. Create 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 name Admin_IndexController)
application/modules/admin/views/scripts/index/index.phtml

Except New In addition to the module file, you also need to change the configuration file application.ini and delete the following lines, if any:
Copy the code The code is as follows:

resources.frontController.controllerDirectory = APPLICATION_PATH"/controllers"

Add the following lines:
Copy code The code is as follows:

resources.frontController.moduleDirectory = APPLICATION_PATH "/modules"
resources.frontController.moduleControllerDirectoryName = "controllers"
resources.frontController.defaultModule = "default"
resources.modules[]

In this way, when you visit http://localhost/admin, you should be able to see the content output by the admin module.
If we want to give full play to the powerful functions of the module, we also need to add a startup file for the module - Bootstrap.php. It allows you to conveniently use class resources, models, filters, helpers, etc. in each module. Create a new Bootstrap.php under admin with the following code:
Copy the code The code is as follows:

class Admin_Bootstrap extends Zend_Application_Module_Bootstrap
{
}

And add the following method to the application/Bootstrap.php file:
Copy the code The code is as follows:

protected function _initAppAutoload()
{
$autoloader = new Zend_Application_Module_Autoloader(array(
'namespace' => 'App',
'basePath' => dirname( __FILE__),
));
return $autoloader;
}

Copy code The code is as follows:

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

Second, the layout script files of different modules are stored in their own 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 in the configuration file application.ini:
Copy code The code is as follows:

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

Whether it is the first or the second type, if you visit http://localhost/admin at this time, you will find that the system does not The expected admin.phtml was used as the layout file, and the default layout.phtml was used instead. This is because the admin line of configuration is not a valid configuration that the system can handle by default, so we have to handle it ourselves.

We create a new file: library/Rockux/Controller/Action/Helper/LayoutLoader.php,

The code for the first case is as follows:
Copy the code 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);
}
}

}

The code for the second case is as follows:
Copy code The code is as follows:

class Rockux_Controller_Action_Helper_LayoutLoader extends Zend_Controller_Action_Helper_Abstract
{

public function preDispatch()
{
$bootstrap = $this->get ActionController( )
->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 need to add it to application/Bootstrap.php
Copy code The code is as follows:

protected function _initLayoutHelper()
{
$this->bootstrap('frontController');
$layout = Zend_Controller_Action_HelperBroker::addHelper (
new Rockux_Controller_Action_Helper_LayoutLoader());
}

Visit http://localhost/admin again, you should be able to see the specified layout file being used.
If you want to use a specific layout for a specific controller, you can add the following code in the init() method of the controller:
Copy code Code As follows:

$layout = Zend_Layout::getMvcInstance();
$layout->setLayout('layout_special');

www.bkjia.comtruehttp: //www.bkjia.com/PHPjc/322914.htmlTechArticleMany people will encounter problems of one kind or another during use, and zend framework has now reached version 1.11. A lot of information on the Internet is still stuck in the old version, so here I am...
Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn