Zend Framework 2.0 module configuration is spread across a series of files which can be found in the skeleton module. Some of the configuration files are described as follows:
◆ Module.php: The Zend Framework 2 module manager looks for the Module.php file in the module's root folder. The module manager uses the Module.php file to configure the module and invokes thegetAutoloaderConfig() and getConfig() methods.
◆ autoload_classmap.php: The getAutoloaderConfig() method in
the skeleton module loads autoload_classmap.php to include any custom
overrides other than the classes loaded using the standard autoloader format.
Entries can be added or removed to the autoload_classmap.php file to
manage these custom overrides.
◆ config/module.config.php: The getConfig() method loads config/module.config.php; this file is used for configuring various module configuration options including routes, controllers, layouts, and various other configurations.
In this section will make configuration changes to the Users module to enable it to work with the newly created controller and views using the following steps:
1. Autoloader configuration – The default autoloader configuration provided by the ZendSkeletonModule needs to be disabled; this can be done by editing autoload_classmap.php and replacing it with thefollowing content:
<?php
return array();
2. Module configuration – The module configuration file can be found in config/module.config.php; this file needs to be updated to reflect the new controllers and views that have been created, asfollows:
‰ Controllers – The default controller mapping points to the ZendSkeletonModule; this needs to be replaced with the mapping shown in the following snippet:
'controllers' => array( 'invokables' => array(
'Users\Controller\Index' => 'Users\Controller\IndexController',
),
),
‰ Views – The views for the module have to be mapped to the appropriate view location. Make sure that the view uses lowercase names separated by a hyphen (for example, ZendSkeleton will be referred to aszend-skeleton):
'view_manager' => array( 'template_path_stack' => array(
'users' => DIR
),
),
. '/../view',
‰ Routes – The last module configuration is to define a route for accessing this module from the browser; in this case we are defining the route as
/users, which will point to the index action in the Index controller of the Users module:
'router' => array( 'routes' => array(
'users' => array(
'type' => 'Literal', 'options' => array(
'route' => '/users',
'defaults' => array(
' NAMESPACE ' => 'Users\Controller', 'controller' => 'Index',
'action' => 'index',
),
),
3. After making all the configuration changes as detailed in the previous sections, the final configuration file, config/module.config.php, should look like the following:
<?php
return array(
'controllers' => array( 'invokables' => array(
'Users\Controller\Index' => 'Users\Controller\IndexController',
),
),
'router' => array( 'routes' => array(
'users' => array(
'type' => 'Literal', 'options' => array(
// Change this to something specific to your module
'route' => '/users', 'defaults' => array(
// Change this value to reflect the namespace in which
// the controllers for your module are found
' NAMESPACE ' => 'Users\Controller', 'controller' => 'Index',
'action' => 'index',
),
),
'may_terminate' => true, 'child_routes' => array(
// This route is a sane default when developing a module;
// as you solidify the routes for your module, however,
// you may want to remove it and replace it with more
'default' => array(
'type' => 'Segment', 'options' => array(
'route' => '/[:controller[/:action]]', 'constraints' => array(
'controller' =>
'[a-zA-Z][a-zA-Z0-9_-]*', 'action' =>
'[a-zA-Z][a-zA-Z0-9_-]*',
),
'defaults' => array(
),
),
),
),
),
),
),
'view_manager' => array( 'template_path_stack' => array(
'users' => DIR
),
),
);
. '/../view',
4. Application configuration – Enable the module in the application's configuration— this can be done by modifying the application's config/application.config. php file, and adding Users to the list of enabled modules:
'modules' => array( 'Application', 'Users',
),
5. To test the module in a web browser, open http://comm-app.local/users/ in your web browser; you should be able to navigate within the module.
The module home page is shown as follows:
The registration page is shown as follows:
What just happened?
We have modified the configuration of ZendSkeletonModule to work with the new controller and views created for the Users module. Now we have a fully-functional module up and running using the new ZF modulesystem.
Now that we have the knowledge to create and configure own modules, your next task would be to set up a new CurrentTime module. The requirement for this module is to render the current time and date in thefollowing format:
Time: 14:00:00 GMT Date: 12-Oct-2012
Q1. What is the tool used by ZendSkeletonApplication for managing dependencies in PHP?
1. Git
2. Composer
3. PHP Command Line Interface
4. Pyrus
Q2. What is the filename of a module's configuration file?
1. <App>/module/<Module>/config.inc
2. <App>/<Module>/config/config.php
3. <App>/module/<Module>/module.config.php
4. <App>/module/<Module>/config/module.config.php
Summary
We have now learned about setting up a new Zend Framework project using Zend's skeleton application and module. In our next chapters, we will be focusing on further development on this module and extending it into a fully-fledged application.
Không có nhận xét nào:
Đăng nhận xét