Zendframework multi-module multi-layout configuration _ PHP Tutorial

Source: Internet
Author: User
Tags autoloader
Zendframework multi-module multi-layout configuration. Many people encounter such problems during use, and zendframework is now in version 1.11, and a lot of information on the network remains on the old version, therefore, many of me have encountered such problems during their use. zend framework is now in version 1.11, and many materials on the network are still on the old version, so here I will take the latest version 1.11 as an example to briefly introduce how to use zend framework to create modular applications. Due to future Framework version upgrades, some content may be outdated. please refer to the latest user manual in a timely manner. 1. preparations

Assume that you have deployed the web server and php, downloaded the latest zend framework version, created the original zend framework project, and accessed the default action. You can use the zend framework tool to create a project. For more information, see create a project using zend framework. Of course, you can also manually create folders and files. For more information, see the project directory structure recommended by zend framework.

Let's take a look at several important default directories.
The first is public. it not only stores index. php, but also images, css, and javascript files.
The second is library, which is used to store some class libraries, including your own or third-party class libraries.
Then test is used to store unit tests and other test files.
Finally, it is the directory with the greatest link we will talk about here-application. The application directory contains the following directories:
Configs: stores the configuration file. Generally, there is a main configuration file application. ini;
Controllers: the operator, such as the default IndexController. php;
Models: Stores business logic, data models, and other files;
Views: View-layer scripts, generally suffixed with. phtml;
Modules: module directory. this directory is automatically generated using the default options of the tool and needs to be manually added. Under modules, multiple folders named after modules can be contained, such as admin. the default value is default. a folder represents a module. the directory structure under it is similar to the application directory and can contain controllers, models, views, and other directories. Note that the class name of the file under 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 easily use some of your own class libraries (for example, the namespace is Rockux) or a third-party class library, you can modify the application. ini file and add the following lines:

The code is as follows:


AutoloaderNamespaces. rockux = "Rockux _"
AutoloaderNamespaces. thirdParty = "ThirdPartyLibrary _"


Of course, you can add a few more as needed, but pay attention to the last underline.

2. create a module
Create an admin module. The directory is as follows:
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. phtml

In addition to creating a module file, you also need to change the configuration file application. ini and delete the following lines, if any:

The code is as follows:


Resources. frontController. controllerDirectory = APPLICATION_PATH "/controllers"


Add the following lines:

The code is as follows:


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


In this way, access http: // localhost/admin to view the output content of the admin module.
If we want to make full use of the powerful functions of the module, we also need to add a startup file -- Bootstrap. php for the module. It makes it easy for you to use class resources, models, filters, and helpers in the case module. Run the following code to create Bootstrap. php under admin:

The code is as follows:


Class Admin_Bootstrap extends Zend_Application_Module_Bootstrap
{
}


Add the following methods to the application/Bootstrap. php file:

The code is as follows:


Protected function _ initAppAutoload ()
{
$ Autoloader = new Zend_Application_Module_Autoloader (array (
'Namespace' => 'app ',
'Basepath' => dirname (_ FILE __),
));
Return $ autoloader;
}


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 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:

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"


If you access http: // localhost/admin, you will find that the system does not use the expected admin. phtml is used as the layout file, but the default layout is used. phtml. This is because the configuration in the admin line is not a valid configuration that the system can handle by default, so we have to handle it ourselves.

Create a File: library/Rockux/Controller/Action/Helper/LayoutLoader. php,

The code for the first case is as follows:

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:

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 need to add it to application/Bootstrap. php.

The code is as follows:


Protected function _ initLayoutHelper ()
{
$ This-> bootstrap ('frontcontroller ');
$ Layout = Zend_Controller_Action_HelperBroker: addHelper (
New Rockux_Controller_Action_Helper_LayoutLoader ());
}


Access http: // localhost/admin again and you will be able to see the specified layout file.
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:

The code is as follows:


$ Layout = Zend_Layout: getMvcInstance ();
$ Layout-> setLayout ('layout _ special ');

Frameworks are now in version 1.11, and a lot of information on the network remains on the old version, so I am here...

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.