Zend Framework Multi-module multi-layout configuration _php Tutorial

Source: Internet
Author: User
Tags autoloader php class zend framework
Many people will encounter such problems in the use of the process, and the Zend Framework is now up to version 1.11, the network of a lot of information is still on the old version, so I am here with the current latest version 1.11 For example, to briefly explain how to use the Zend The framework creates modular applications. Due to the future version of the framework, some content may be outdated, please refer to the latest user manual.

1. Preparatory work

First, assume that you have deployed a Web server and PHP, downloaded the latest version of the Zend Framework, created a most original Zend Framework project, and access the default action. You can use the Zend Framework tool to create a project, as described in creating a project with the Zend Framework. Of course, you can also manually create folders and files, see the Zend Framework recommended project directory structure.

Simply take a look at the default number of important directories.
The first is public, it not only stored the program entry point index.php, can also store pictures, css,javascript files and so on.
The second is the library, which holds some class libraries, including your own or third-party class libraries.
Then it is test, which is used for testing files such as unit tests.
Finally, it's also the largest directory--application we're going to talk about here. In the application directory, the following directories will be available:
Configs: Storage configuration file, there will generally be a master configuration file Application.ini;
Controllers: the operator, such as the default indexcontroller.php;
Models: Store business logic, data model and other documents;
Views: The script of the view layer, generally with. phtml as the suffix name;
Modules: module directory, using the tool default options automatically generated is not this directory, you need to manually add. Modules can contain several folders named after the module name, such as admin, default, a folder represents a module, under which the directory structure is similar to the application directory, and can contain controllers,models, Views and other directories. It is important to note that the class name of the following file under module controllers is prefixed with the module name, such as application/modules/admin/controllers/indexcontroller.php class name Admin_ Indexcontroller.

If you need to conveniently use some of your own class libraries (such as namespace Rockux) or a third-party class library in your project, you can modify the Application.ini file to add the following line:
Copy CodeThe code is as follows:
Autoloadernamespaces.rockux = "Rockux_"
Autoloadernamespaces.thirdparty = "Thirdpartylibrary_"

Of course you can add as many as you want, but be aware of the last underline.

2. Building modules
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 named Admin_indexcontroller)
Application/modules/admin/views/scripts/index/index.phtml

In addition to creating a new module file, you need to change the configuration file Application.ini, deleting the following lines, if any:
Copy CodeThe code is as follows:
Resources.frontController.controllerDirectory = Application_path "/controllers"

Add the following line:
Copy CodeThe code is as follows:
Resources.frontController.moduleDirectory = Application_path "/modules"
Resources.frontController.moduleControllerDirectoryName = "Controllers"
Resources.frontController.defaultModule = "Default"
Resources.modules[]

In this way, to access the http://localhost/admin, you should be able to see the contents of the Admin module output.
If we want to take full advantage of the power of the module, we also need to add a boot file--bootstrap.php for the module. It makes it easy to use class resources, models, filters, helpers, etc. in a module. Create a new bootstrap.php under admin with the following code:
Copy CodeThe code is as follows:
Class Admin_bootstrap extends Zend_application_module_bootstrap
{
}

and add the following methods to the application/bootstrap.php file:
Copy CodeThe code is as follows:
protected function _initappautoload ()
{
$autoloader = new Zend_application_module_autoloader (Array (
' Namespace ' and ' App ',
' BasePath ' = dirname (__file__),
));
return $autoloader;
}

Copy CodeThe 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 the respective module folder
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 CodeThe 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, if you visit Http://localhost/admin, you will find that the system does not use the desired admin.phtml as the layout file, but instead uses the default layout.phtml. This is because the admin line configuration is not a valid configuration that the system can handle by default, so we have to deal with it ourselves.

We created a new file: library/rockux/controller/action/helper/layoutloader.php,

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

And then we need to add it to the application/bootstrap.php.
Copy CodeThe code is as follows:
protected function _initlayouthelper ()
{
$this->bootstrap (' Frontcontroller ');
$layout = Zend_controller_action_helperbroker::addhelper (
New Rockux_controller_action_helper_layoutloader ());
}

When you visit Http://localhost/admin again, you should be able to see the use of the specified layout file.
If you want to use a specific layout for a particular controller, you can add the following code to the controller's init () method:
Copy CodeThe code is as follows:
$layout = Zend_layout::getmvcinstance ();
$layout->setlayout (' layout_special ');

http://www.bkjia.com/PHPjc/322914.html www.bkjia.com true http://www.bkjia.com/PHPjc/322914.html techarticle Many people will encounter such problems in the use of the process, and the Zend Framework is now 1.11 version, the network is still a lot of information on the old version, so I am here ...

  • 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.