


zend framework multi-module multi-layout configuration_PHP tutorial
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:
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:
resources.frontController.controllerDirectory = APPLICATION_PATH"/controllers"
Add the following lines:
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:
class Admin_Bootstrap extends Zend_Application_Module_Bootstrap
{
}
And add the following method to the application/Bootstrap.php file:
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 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:
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:
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:
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
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:
$layout = Zend_Layout::getMvcInstance();
$layout->setLayout('layout_special');

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

AI Hentai Generator
Generate AI Hentai for free.

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics



CrystalDiskMark is a small HDD benchmark tool for hard drives that quickly measures sequential and random read/write speeds. Next, let the editor introduce CrystalDiskMark to you and how to use crystaldiskmark~ 1. Introduction to CrystalDiskMark CrystalDiskMark is a widely used disk performance testing tool used to evaluate the read and write speed and performance of mechanical hard drives and solid-state drives (SSD). Random I/O performance. It is a free Windows application and provides a user-friendly interface and various test modes to evaluate different aspects of hard drive performance and is widely used in hardware reviews

Title: The working principle and configuration method of GDM in Linux systems In Linux operating systems, GDM (GNOMEDisplayManager) is a common display manager used to control graphical user interface (GUI) login and user session management. This article will introduce the working principle and configuration method of GDM, as well as provide specific code examples. 1. Working principle of GDM GDM is the display manager in the GNOME desktop environment. It is responsible for starting the X server and providing the login interface. The user enters

foobar2000 is a software that can listen to music resources at any time. It brings you all kinds of music with lossless sound quality. The enhanced version of the music player allows you to get a more comprehensive and comfortable music experience. Its design concept is to play the advanced audio on the computer The device is transplanted to mobile phones to provide a more convenient and efficient music playback experience. The interface design is simple, clear and easy to use. It adopts a minimalist design style without too many decorations and cumbersome operations to get started quickly. It also supports a variety of skins and Theme, personalize settings according to your own preferences, and create an exclusive music player that supports the playback of multiple audio formats. It also supports the audio gain function to adjust the volume according to your own hearing conditions to avoid hearing damage caused by excessive volume. Next, let me help you

NetEase Mailbox, as an email address widely used by Chinese netizens, has always won the trust of users with its stable and efficient services. NetEase Mailbox Master is an email software specially created for mobile phone users. It greatly simplifies the process of sending and receiving emails and makes our email processing more convenient. So how to use NetEase Mailbox Master, and what specific functions it has. Below, the editor of this site will give you a detailed introduction, hoping to help you! First, you can search and download the NetEase Mailbox Master app in the mobile app store. Search for "NetEase Mailbox Master" in App Store or Baidu Mobile Assistant, and then follow the prompts to install it. After the download and installation is completed, we open the NetEase email account and log in. The login interface is as shown below

Cloud storage has become an indispensable part of our daily life and work nowadays. As one of the leading cloud storage services in China, Baidu Netdisk has won the favor of a large number of users with its powerful storage functions, efficient transmission speed and convenient operation experience. And whether you want to back up important files, share information, watch videos online, or listen to music, Baidu Cloud Disk can meet your needs. However, many users may not understand the specific use method of Baidu Netdisk app, so this tutorial will introduce in detail how to use Baidu Netdisk app. Users who are still confused can follow this article to learn more. ! How to use Baidu Cloud Network Disk: 1. Installation First, when downloading and installing Baidu Cloud software, please select the custom installation option.

MetaMask (also called Little Fox Wallet in Chinese) is a free and well-received encryption wallet software. Currently, BTCC supports binding to the MetaMask wallet. After binding, you can use the MetaMask wallet to quickly log in, store value, buy coins, etc., and you can also get 20 USDT trial bonus for the first time binding. In the BTCCMetaMask wallet tutorial, we will introduce in detail how to register and use MetaMask, and how to bind and use the Little Fox wallet in BTCC. What is MetaMask wallet? With over 30 million users, MetaMask Little Fox Wallet is one of the most popular cryptocurrency wallets today. It is free to use and can be installed on the network as an extension

Understanding Linux Bashrc: Function, Configuration and Usage In Linux systems, Bashrc (BourneAgainShellruncommands) is a very important configuration file, which contains various commands and settings that are automatically run when the system starts. The Bashrc file is usually located in the user's home directory and is a hidden file. Its function is to customize the Bashshell environment for the user. 1. Bashrc function setting environment

Apple rolled out the iOS 17.4 update on Tuesday, bringing a slew of new features and fixes to iPhones. The update includes new emojis, and EU users will also be able to download them from other app stores. In addition, the update also strengthens the control of iPhone security and introduces more "Stolen Device Protection" setting options to provide users with more choices and protection. "iOS17.3 introduces the "Stolen Device Protection" function for the first time, adding extra security to users' sensitive information. When the user is away from home and other familiar places, this function requires the user to enter biometric information for the first time, and after one hour You must enter information again to access and change certain data, such as changing your Apple ID password or turning off stolen device protection.
