Apply for Zend Framework Certification Training

Zend Framework 2




1.1.  Create the module directory structure and necessary preliminary files
At first we are going to create the Album module structure, it’s similar to the ‘Helloworld’ module. 
The directory structure is shown below.
 
Now first of all we know that the module manager will first look for the Module.php file and look 
for the class Module, so we will now open Module.php and insert these below codes.
<?php

namespace Album;

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';
  }
}
?>
I already discussed these in previous, though a little discussion here for those directly comes here.
We define the namespace name of the module and then we create the class Module and create two 
common methods which are automatically got by the ZF2 when Module Manager is initiates. We 
describe this into the ‘Hello world’ module already in details. If you didn’t read or watch the video 
of the ‘Hello World’ module application, I recommended you to read or watch that first (link). In
 short, I can say that when ZF2 starts then if the module namespace is configured into the 
application.config.php then the module manager automatically activates all the modules. When 
a module initialize then it checks for the Module.php file and see the Module class, if there is any
 methods named getAutoloaderConfig() or getConfig() then these two methods also initialize 
automatically. These two methods basically say’s about the module configuration. Into the getConfig()
 method we returning the module config array that has defined into the module.config.php file, ZF2 
merge these module configuration with the whole application configuration. And into the 
getAutoloaderConfig() method we shows the path of our module class files where we will write our all 
the controllers and models, we want these classes initializes when the module initiates.
Details visit this blog if you want to know more details.
1.2. Define view_manager in module.config.php 
So now we had written some codes into the Module.php file, now we will configure the module
 configuration file. Open the  /config/module.config.php file, and write these codes.

<?php
  return array('view_manager' => array('template_path_stack' => array(__DIR__ . '/../view'),),);
?>
We here just define the view manager and shows where is our template paths for the views for this 
module.
 
We will add more configurations keys into this file later as we go forward. Normally the controllers,
 routes, service managers, factories are all configured into this file.
 
1.3. Create the Controller named ‘AlbumController’ 
Now we need to create a controller. We are naming it Album. So create a file named
 AlbumController.php under the /Controller/
 
As you see each controller are named by append of Controller into it, it’s the naming convention 
of the ZF2.
Add these codes into it.
<?php

namespace Album\Controller;
use Zend\Mvc\Controller\AbstractActionController;
use Zend\View\Model\ViewModel;
class AlbumController extends AbstractActionController
{
	public function indexAction()
	{
    		return new ViewModel(array('album' => 'Album Home Page'));
	}
}
So here we define the namespace name; then we create the AlbumController class which
 extends the AbstractionActionController of the Zend\Mvc\Controller. Also we create an 
action method named indexAction. We want that our controller and view works good at
 first like the hello world program, so we will create the view and route first after that we 
will add the functionality into the controller later. So we just provide a view model array 
with a key name album and give it a value Album Home Page for now.
 
Now that we have a controller so we will immediately add  and configure it to the 
module.config.php file.
Open the /config/module.config.php file and add these codes into it.
'controllers' => array(
     'invokables' => array(
         'Album\Controller\Album' => 'Album\Controller\AlbumController',
          // <----- Module Controller
      ),
 ),
So here we create a controllers key and into the invokables we name this controller as
 ’Album\Controller\Album’ and gives the path of the controller.
For your information, this controller is now can be accessible from any other modules in 
the application. That is why we named our controller as ’Album\Controller\Album’ so 
that it will be unique.
So up to now we create the controller and we defined it into the module configuration 
and now we will create the index view as we create a indexAction method under the 
Album controller. 

So let’s create a view first.
Create a file named index.phtml under /view/album/album/
See here we create the view file under the view/then the module name/ then the controller 
name, this is the convention of the ZF2.
And into the index.phtml file write these codes
<?php
echo $this->album;
?>
Here we are just printing the album key value from the AlbumController of indexAction 
methods, see there we defined the album key a value ‘Album Home Page’.
 
1.4. Create routes for Album Module
We will now create the route for this view.
Again open the module.config.php file and write these lines of codes at the end.
'router' => array(
   'routes' => array(
     'album' => array(
        'type'    => 'segment',
        'options' => array(
           'route'    => '/album/album[/:action][/:id]',
            // <---- url format module/action/id
           'constraints' => array(
              'action' => '[a-zA-Z][a-zA-Z0-9_-]*',
              'id'     => '[0-9]+',
            ),
            'defaults' => array(
               'controller' => 'Album\Controller\Album',
                // <--- Defined as the module controller
               'action'     => 'index',
                // <---- Default action
            ),
        ),
     ),
  ),
),
So here we create a route name album which is a segment type route. And we defined 
some of the options here. We are checking when this route will be activated? We write it
 like this ‘/album/album/[:/action][:/id]’ I like to active my controller like the module name / 
then the controller name/ then the action name and / later the id. I also defined some
 regular expression for the action name so that action and id follows these validation rules.
Then we defined the default controller name as our AlbumController and action as
 indexAction() method for it.
1.5. Now active the module.
Open the application.config.php file and add this module namespace name at the last 
after comma.
 
Here we inform our application that a new module has been added to the system.
Now try to visit: http://www.zf2-tutorial-video.loc/album/album
You supposed to see the message text “Album Home Page”
If you are having problem please write it into the comment section, write what it is showing 
on the error.We can also see that what controller, action and id are requesting and working 
behind the requested url.Open the AlbumController.php file and changed the codes of the
 indexAction method
return new ViewModel(array(
      'namespace'        => __NAMESPACE__,
      'controller'    => $this->params('controller'),
      'action'        => $this->params('action'),
      'id'            => $this->params('id'),
   ));
So here we defined the name of the module.
Then we get the controller name by calling the params(‘controller’) and action and id
So now print this values into the index.phtml file.
echo "Module Name: ".$this->namespace;
echo "<br />";
echo "Controller Name: ".$this->controller;
echo "<br />";
echo "Action Name: ".$this->action;
echo "<br />";
echo "ID: ".$this->id;
Now if we type into the url this : http://localhost/album/album/index/5
It will show us this informations:
Module Name: Album Module
Controller Name: Album\Controller\Album
Action Name: index
ID: 5

                                    

< Listing of Data From Database and models In ZendFramework2 How to create Form in Zend Framework 2 and Insert Data in Database >



Ask a question



  • Question:
    {{questionlistdata.blog_question_description}}
    • Answer:
      {{answer.blog_answer_description  }}
    Replay to Question


Back to Top