Tuesday, 8 January 2019

Zend Framework modules questions and answers

Zend Framework modules questions and answers

Question: What are the basic steps for create new module?
Suppose you are creating module with name of Album.
  1. Attach module with application.
    Open file i.e /config/application.config.php, Add Blog in array like below:
    return array(
         'modules' => array(
             'Application',
             'Blog'
         ),
         
     );
    
  2. create Album  folder inside the module folder.
  3. After creating the Album folder inside modules, Create below folder structure.
  4. Album/
        Module.php
        autoload_classmap.php
        autoload_function.php
        autoload_register.php
        config/
            module.config.php
        public/
            images/
            css/
            js/
        src/
            Album/
                Controller/
                    AlbumController.php
                Form/
                    AlbumForm.php
                Model/
                    Album.php 
        view/
            album/
                album/
                    index.phtml
            layout/
                layout.phtml
            error
                index.phtml
    



Question: How to attach a module in main application?
Go to File config/application.config.php, and add your module name in modules array.
See example below (We have add a module Album).
return array(
     'modules' => array(
         'Application',
         'Album'
     ),
     
 );



Question: What is Service?
A Service is an object in the module that executes complex logic of the application.
Here we do all difficult logic together and gives you easy to understand results.


Question: What are the basic steps for creating the Service?
  1. Create a folder service in-side the module and path will be similar to below:
    /module/{moduleName}/src/{moduleName}/Service/{Album}Service.php
  2. Create a model in-side the module and path will be similar to below:
    /module/{moduleName}/src/{moduleName}/Model/Post.php
  3. Add Following code in controller where you want to use.
    use {moduleName}\Service\PostServiceInterface;

    Add following in controller (you can use another variable)
    protected $postService;
     public function __construct(PostServiceInterface $postService)
         {
             $this->postService = $postService;
         }

  4. Create the Factory class (path: /module/{moduleName}/src/{moduleName}/Factory/{factoryClass}.php)
  5. Register your service in module.config.php. (Add following code)
            'service_manager' => array(
             'invokables' => array(
                 '{moduleName}\Service\{Service}' => '{moduleName}\Service\{Service}'
             )
             
  6. Register your service from module.config
    (Path: /module/Blog/config/module.config.php)
        'controllers'  => array(
             'factories' => array(
                 '{moduleName}\Controller\List' => '{moduleName}\Factory\{factoryClass}'
             )
          

Question: How to set different Db Connections for each module?
There are different module.config.php file inside each module (module/{moduleName/config/module.config.php}.
In this you you can set the database credentials.
 'db' => array(
         'driver'         => 'Pdo',
         'username'       => 'SECRET_USERNAME',  //edit this
         'password'       => 'SECRET_PASSWORD',  //edit this
         'dsn'            => 'mysql:dbname=blog;host=localhost',
         'driver_options' => array(
             \PDO::MYSQL_ATTR_INIT_COMMAND => 'SET NAMES \'UTF8\''
         )
     ),