Dish rookie Zend Framework 2 not fully learning Graffiti (iv)--module
This is the fourth piece of graffiti.
Module (Modules)
ZF2 is a modular system, and you need to organize your main application code in each module. The application modules provided by the template (skeleton) are used throughout the application as bootstrap (bootstrapping), errors (Error), and routing settings (routing configuration). It is often used to provide application-level control, such as the first page of an application. However, in this tutorial we do not use the default module, we will use the album list as the first page of the application.
We put the code into the Record module (Album module), which contains the console (Controller), the mode (Models), the form (form) and the view, and of course the configuration.
Let's start by setting the necessary directory structure
First, set up the recording module (Album module)
First, in the module directory to establish a subdirectory of the Ablum to store modules files, the construction of pre-and post-directory for example
The entire Album directory is as follows
zf2-tutorial/ /module /album /config /src /album /controller/form/model /view /album /album
We keep different types of files in different directories under the album Module (Album module). The classes defined in the PHP file in the Src/ablum directory are in the namespace named Album, so that we can define multiple namespaces as needed. There is also a album subdirectory under the View directory to hold the view file.
The ZF2 has a module manager (Modulemanager) that is used to invoke and configure modules. The module manager looks for module.php in the module start directory (module/album) and finds a class called Album\module in the future. This means that the directory name is the module name, and a class under a module has a namespace named after the module name.
In the Zf2-tutorial/module/album directory, create a module.php file with the following code
<?phpNamespaceAlbum;Classmodule{PublicfunctionGetautoloaderconfig() {ReturnArray' Zend\loader\classmapautoloader ' =array (__dir__. '/autoload_classmap.php ', " Zend\loader\standardautoloader ' = = array ( ' namespaces ' = __namespace__ = __dir__. '/src/'. __namespace__,),); } public function getconfig () {return Include __dir__. '/config/module.config.php ';}}
The module Manager (Modulemanager) automatically calls Getautoloaderconfig () and GetConfig () for us.
Second, automatic call file
Getautoloaderconfig () returns an array that is compatible with the ZF2 autoloaderfactory. We add a class map file in Classmapautoloader and add the module's namespace to Standardautoloader. The standard auto-caller requires a namespace and a path where the namespace file can be found. Such structures conform to the PSR-0 rules.
In the course of the development of this tutorial, we do not need to invoke the file through Classmap, so we set an empty array in the CLASSMAP auto-caller, the steps are as follows:
1. Create a new PHP file under the Zf2-tutorial/module/album directory and name it: autoload_classmap.php
2. Enter the following code
<?phpArray ();
When the Automatic caller (autoloader) looks for a class that corresponds to the Ablum namespace, the empty array is returned to Standardautoloader.
Attention:
If you are using composer, you can create an empty function Getautoloaderconfig () {} in Composer.json instead of the above steps
"AutoLoad": { "module/album/src/"}},
If you use this method, you need to perform
PHP Composer.phar Update
command to update the composer auto-call file.
Third, the configuration
After registering for the Automatic Caller (autoloader), let's take a quick look at the GetConfig () method under Album\module, which simply calls the config/module.config.php file.
Create a new PHP file under the Zf2-tutorial/module/album/config directory and name it: module.config.php, with the following code:
<?phpArray ( '/.. /view ',),);
The configuration information above will be passed to the related component via ServiceManager. We need two initial fields: Controllers and View_manager.
The Controller field contains all the controllers provided by the module, where we only need a controller in Album\controller\album: Albumcontroller. The controller primary key must be unique, so we added a prefix to the module name
In the View_manager field we specify the directory path where the view is located in Templatepathstack so that the Ablum module can find the code for the view in the view/directory.
Iv. information about the new application module
The module Manager (Modulemanager) is described below. The template provides a config/application.config.php file that adds the "Ablum" module to the Modules field of this file, with the following code:
<?phpArray ( './vendor ',) ,);
We added "Album" in the "Modules" field immediately following "Application"
Now we are ready to enter our own code.
-------------------------------------------------------------------------------------
2013-07-26 Supplement
V. Information about the application.config.php
The application.config.php file is a template (Skeleton) that comes with an application configuration file, and the save path is zf2-tutorial\module\album\config\. This configuration file returns an array directly, with the default of three keywords (key), namely: Modules,module_listener_options and Service_manager.
1, modules
Modules is also an array that holds the namespaces used by the application. For example, Album
Array ( ' application ', ' Album '),
2, Module_listener_options
Module_listener_options is also an array that holds many options for the listener of the module manager (Modulemanager). Let's take a look at the meaning of each keyword (key)
(1) module_paths
This keyword points to an array that contains the path to which the module is located. If we set the path to a module, the Listener (listener) will notice the namespace of the module. The path to this setting is the module class that points to the module
Array ( './module ', './vendor ',),
In the example above, "./module" is the path saved by the module we created, "./vendor" is the path where the ZF2 class library resides.
(2) Config_glob_paths
This keyword refers to an array that contains the path to the global configuration file after the module call. This configuration file can be easily overwritten by the module's own configuration. The path can use the glob_brace symbol
Array ( ' config/autoload/{,*.} {global,local}.php ',),
The path in the example above is a PHP file in the config/autoload/directory that ends with global or local. In the default template (S K e l e t o N), this file is zf2-tutorial/config/autoload/global.php
(3) config_cache_enabled
Whether the cache is allowed to be configured. If allowed, the merged configuration information is cached and used in future requests. Caching is not allowed by default
$booleanValue,
(4) Config_cache_key
This keyword is used to create the configuration information cache file name, which is not enabled by default
$stringKey,
(5) module_map_cache_enabled
Whether module-class map caches are allowed. If allowed, a template class map cache is created to be used for future requests in order to reduce the automatic call (autoloading) process, which is not allowed by default
$booleanValue,
(6) Module_map_cache_key
This keyword is used to create a template class map cache file name, which is not enabled by default
$stringKey,
(7) Cache_dir
Cache saved paths After merging configuration information, not using default
$stringPath,
(8) Check_dependencies
Whether module dependency checking is allowed. By default, prevents the use of dependency amount modules (module) but is not called
True
3. Create your own service Manager before. May contain one or more sub-arrays
' service_listener_options ' = Array (// Array (// $stringServiceManagerName,// ' Config_key ' $stringConfigKey,// $stringRequiredMethodName,//),/ /)
4. Initialize the Service Manager (ServiceManager), which will be consistent with Zend\servicemanager\config
' Service_manager ' = = Array (),
Not to be continued ... Thank you
Dish rookie Zend Framework 2 not fully learning Graffiti (iv)--module