Zendframework 2 tutorial part 2

posted in: Zend Framework | 0

Summary:

Creating Modules:

1. Create a New Module under the module folder with the following structure(By default you will have one Application module)

Project/

            /module

                        /ModuleName

                                    /config

                                    /src

                                                /ModuelName

/Controller

/Form

/Model

                                    /view

/ModuleName

/controllerName               

For my project this is the module structure for User Module

Zendy/

            /module

                        / User

                                    /config

                                    /src     

                                                / User

                                                            /Controller

/Form

/Model

                                    /view

                                                / User

                                            / User (controllerName)

 

In order to load and configure a module, Zend Framework 2 has a ModuleManager. This will look for Module.php in the root of the module directory (module/User) and expect to find a class called User\Module within it. That is, the classes within a given module will have the namespace of the module’s name, which is the directory name of the module.

2.Create Module.php under module/ User with the following code

<?php

namespace User;

class Module

{

    public function getAutoloaderConfig()

    {

        return array(

            ‘Zend\Loader\StandardAutoloader’ => array(

                ‘namespaces’ => array(

                    __NAMESPACE__ => __DIR__ . ‘/src/’ . __NAMESPACE__,

                ),

            ),

        );

    }

    public function getConfig()

    {

        return include __DIR__ . ‘/config/module.config.php’;

    }

}

?>

3.Create a file called module.config.php under module/User/config

<?php

return array(

    ‘controllers‘ => array(

        ‘invokables’ => array(

            ‘User\Controller\User’ => ‘User\Controller\UserController’,

        ),

    ),

    ‘view_manager‘ => array(

        ‘template_path_stack’ => array(

            ‘user’ => __DIR__ . ‘/../view’,

        ),

    ),

);

?>

We need two initial sections: controllers and view_manager. The controllers section provides a list of all the controllers provided by the module. We will need one controller, User Controller, which we’ll reference as User\Controller\User . The controller key must be unique across all modules, so we prefix it with our module name.

Within the view_manager section, we add our view directory to the TemplatePathStack configuration. This will allow it to find the view scripts for the User module that are stored in our view/ directory.

 4. Informing the application about our new module  User

We now need to tell the ModuleManager that this new module exists. Open the config/application.config.php file which is provided by the skeleton application. Update this file

<?php
return array(

    ‘modules’ => array(

        ‘Application’,

        ‘User’,                  // <– Add this line

    ),

    ‘module_listener_options’ => array(

        ‘config_glob_paths’    => array(

            ‘config/autoload/{,*.}{global,local}.php’,

        ),

        ‘module_paths’ => array(

            ‘./module’,

            ‘./vendor’,

        ),

    ),

);

?>

Previous Post
Next Post

Leave a Reply