Many people encounter such problems in their use, and the Zend Framework is now up to 1.11, and much of the data on the web is still on the old version, so I'm here to take the current version of 1.11 as an example to briefly describe how to use the Zend The framework creates a modular application. Some of the content may become obsolete due to a future version upgrade of the framework, please refer to the latest usage manual in time.
1. Preparation work
Let's say you've already deployed your Web server and PHP, downloaded the latest version of the Zend Framework, created an original Zend Framework project, and accessed the default action. You can use the Zend Framework tool to create your project, and see How to create a project using the Zend Framework. Of course, you can manually create folders and files yourself, see the project directory structure recommended by the Zend Framework.
Simply look at a few of the default key directories.
First is public, it not only holds the entry point of the program index.php, but also can store pictures, css,javascript files and so on.
The second is the library, which is used to store libraries of classes, including your own or third party libraries.
Then test, which is used to store test files such as unit tests.
Finally, it is also the most closely related directory--application that we are going to talk about here. In the application directory, the following directories are available:
Configs: Storage configuration file, there will generally be a main configuration file Application.ini;
Controllers: Operating device, such as the default indexcontroller.php;
Models: Store business logic, data model and other documents;
Views: The script of the view layer, generally with the. phtml as the suffix name;
Modules: The module directory, which is automatically generated using the tool default option, needs to be manually added. Modules can contain multiple folders named after the module name, such as admin, default defaults, a folder representing a module, and a directory structure that is similar to the application directory, and can contain controllers,models. Views and other directories. Note that the class name of the file below the module controllers the module name prefix, such as the application/modules/admin/controllers/indexcontroller.php class name Admin_ Indexcontroller.
If you need to use some of your own class libraries (such as namespaces that are Rockux) in your project, or a third party class library, you can modify the Application.ini file and add the following line:
Copy CodeThe code is as follows:
Autoloadernamespaces.rockux = "Rockux_"
Autoloadernamespaces.thirdparty = "Thirdpartylibrary_"
Of course, you can also add as many as you need, but note the underscore at the end of the line.
2, the establishment of 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 name is Admin_indexcontroller)
Application/modules/admin/views/scripts/index/index.phtml
In addition to creating a new module file, you need to change the profile Application.ini, delete 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, access to http://localhost/admin, you should be able to see the content of the admin module output.
If we want to give full play to the power of the module, we also need to add a startup file--bootstrap.php for the module. It can make you in the matter of the module convenient use of class resources, models, filters, helpers and so on. Under admin new bootstrap.php, the code is as follows:
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 ' => ' 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 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:
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's the first or the second, if you visit Http://localhost/admin, you'll find that the system doesn't use the desired admin.phtml as a layout file, but instead uses 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 have to handle it ourselves.
We create a new file: library/rockux/controller/action/helper/layoutloader.php,
For the first scenario, the code 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);
}
}
}
For the second scenario, the code 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 ());
}
To access Http://localhost/admin again, you should be able to see using the specified layout file.
If you want to use a specific layout for a particular controller, you can add the following code in the Controller init () method:
Copy CodeThe code is as follows:
$layout = Zend_layout::getmvcinstance ();
$layout->setlayout (' layout_special ');