আগামী ১৫ ডিসেম্বর -২০১৭ তারিখ থেকে শুরু হচ্ছে পাঁচ মাস ব্যাপী Professional Web Design and Development with HTML, CSS PHP,MySQL,JavaScript, AJAX, JQUERY, Bootstrap and Live Project কোর্সের ৮৭ তম ব্যাচ এবং ২৬ ডিসেম্বর-২০১৭ তারিখ থেকে শুরু হচ্ছে চার মাস ব্যাপী Zend PHP-7 Certified PHP Engineering (Advance PHP) কোর্সের ৩৫ তম ব্যাচ। প্রত্যেকটি কোর্স এর ফী নির্ধারণ করা হয়েছে ৩০,০০০/= আগ্রহীদেরকে অতিসত্বর মাসুদ আলম স্যার এর সাথে যোগাযোগ করতে অনুরোধ করা যাচ্ছে। স্যার এর মোবাইল: 01722 81 75 91

Getting Started with Zend Framework 2 Part-2: Create a Zend Framework 2 Module

Create a Module with ZEND Framesork

Create a Module

Well,. This version made ​​it clear (and practical) the use of modules.  Now in this post I’ll  show you how to create a new module, which we’ll call “Hello”. Let’s first create the configuration files of the module, and later we will create two static pages to exemplify the MVC architecture (without the “M” for now), implemented by Zend Framework 2.

Let’s Go …

Creating the Structure Module

  1. First Create a new folder called “Hello” within the directory “zf2/module . Inside the “Hello“, create 3 folders: “config“, “src” and “view“. Also create a file called ” Module.php “, and leave it blank for now.
  2. Inside the “src” folder, create another folder called “Hello” (Capitalized same, because the namespace)
  3. Inside the “src/Hello” Create a Folder called “Controller” (Capitalized same, because the namespace), and also create “HelloController.php” file within the “src/Hello/Controller” folder.
  4. Inside the “view” folder, create another folder called “hello” (this lowercase).
  5. Finally, create a file called “module.config.php ” in the folder “config“, blank for now.

Note that the structure will look like this:

Zend Framework 2 Module Structure

For now these are the directories and files that need. Come to the most important part of this process: The configuration of this module.

Configuring the module

The first step is to enable the module . In Zend Framework is all explicit, so if we create a module, we configure it properly and do not set this module is active, it will not run. To activate it, open the file ” zf2/config/application.config.php “and add the module name in this array there.See:

<?php

return array (

'Modules' => array (

'Application' ,

'Hello' , // This line was added to activate the module

)

'Module_listener_options' => array (

'Module_paths' => array (

'. / Module' ,

'. / Vendor' ,

)

'Config_glob_paths' => array (

'Config / autoload / {*.} {Global, local}. Php' ,

)

)

);

Now we will set its internal settings. Open “/ zf2/module/Hello/config/module.config.php” file and add the following code:

<?php

return array(

'router' => array(

'routes' => array(

'hello' => array(

'type' => 'Literal',

'options' => array(

'route' => '/hello',

'defaults' => array(

'controller' => 'Hello\Controller\Hello',

'action' => 'index',

),

),

'may_terminate' => true,

'child_routes' => array(

'default' => array(

'type' => 'Segment',

'options' => array(

'route' => '/[:action]',

'constraints' => array(

'action' => '[a-zA-Z][a-zA-Z0-9_-]*',

),

),

),

),

),

),

),

'controllers' => array(

'invokables' => array(

'Hello\Controller\Hello' => 'Hello\Controller\HelloController',

),

),

'view_manager' => array(

'template_path_stack' => array(

__DIR__ . '/../view',

),

),

);

Now open the File “Module.php” in “zf2/module/Hello/ “and add the following code:

<?php

namespace Hello;

use Zend\Mvc\ModuleRouteListener;

use Zend\Mvc\MvcEvent;

class Module

{

public function onBootstrap(MvcEvent $e)

{

$eventManager = $e->getApplication()->getEventManager();

$moduleRouteListener = new ModuleRouteListener();

$moduleRouteListener->attach($eventManager);

}

public function getConfig()

{

return include __DIR__ . '/config/module.config.php';

}

public function getAutoloaderConfig()

{

return array(

'Zend\Loader\StandardAutoloader' => array(

'namespaces' => array(

__NAMESPACE__ => __DIR__ . '/src/' . __NAMESPACE__,

),

),

);

}

}

Okay, our module configuration is set. Now let’s create our first Controller.

 Creating the first controller

Create a file called “HelloController.php ”  within the directory “zf2/module/Hello/src/Hello/Controller  and add the following code :

<?php

namespace Hello\Controller;

use Zend\Mvc\Controller\AbstractActionController;

use Zend\View\Model\ViewModel;

class HelloController extends AbstractActionController

{

public function indexAction()

{

$text = "I was set on the controller, but will appear in the view";

return new ViewModel(array(

'example' => $text,

));

}

}

Understand it line by line:

Line 3: define the namespace. As everything is on the first folder inside the “src /” directory, simply define “Album \ Controller”.

Line 5 and 6: Two classes that we will use in the controller, so give a “use” here.

Line 8: I create a class (must must finish the class name with “Controller”, and set the name of the file the same way), and I extend the same class with Zend responsible for controllers (just type the name of the class, since included this in line 5 of file)

Line 10: For every action of our controller, a new function must be defined. When followed by the “Action” at the end.

Line 12: define some string in $text variable.

Line 14 to 16: we’re pushing for layer display the string created on line 12.

Let’s see how it will look in the view layer?

Creating the view layer

Now it’s even easier. We have already defined everything we needed to make the system run, just want to see how to print the view layer that string created there in the controller layer … That’s pacifier in honey. Come on.

Create within the directory ” / zf2/module/Hello/view/hello / “a new directory called” hello “.This refers to the folder Controller “HelloController” . That has to be clear to you, dear reader: if we had a controller called “TesteController” would create within the directory “hello” or directory called “test” in lower case letters. Now inside the directory ” / zf2/module/Hello/view/hello/hello “, create a file named” file index.phtml “

In turn, the “index.phtml” file references the action ” indexAction ” within the controller ” HelloController “. Did you see how the structure of Zend Framework 2 is well organized? Now copy the code below into the “index.phtml” file.

<h2> Welcome to Hello Module, I'm the action INDEX. </h2>

<!-- The below line will print the variable $ instance -->

<?= $example ?>

<!-- This was defined in the controller HelloController -->

Now go into your browser application with URI ” / hello “or” /hello/index ” (in my end its: http://zf2/hello) and see his first work with Zend Framework 2 running !

ZEND Framework 2 Module

That’s it, my friend … The first time we ran this whole process can seem a little complicated, but practice makes perfect, so train. Follow this tutorial two or three times and see that it’s not as complicated as it sounds. Note also that we keep the same layout, so there is no need to configure a new one. But you can define different layouts for modules, including more than one layout per module. It’s all about setup and configuration.

Challenge: Try yourself now create a new action, and make it appear on the screen. If you will try also create a new controller ( do not forget to configure the routes and everything else ) and run it. And feel free to also comment on what you thought of this tutorial.

Thank you for accompanying me on this journey more. Maybe next post I will show how to work with sql queries to object-oriented with Zend Framework 2 .

Warmest regards and until next time.

Getting Started with Zend Framework 2 Part-2- Create a Zend Framework 2 Module

Hi, My name is Masud Alam, love to work with Open Source Technologies, living in Dhaka, Bangladesh. I’m a Certified Engineer on ZEND PHP 5.3, I served my first five years a number of leadership positions at Winux Soft Ltd, SSL Wireless Ltd, Canadian International Development Agency (CIDA), World Vision, Care Bangladesh, Helen Keller, US AID and MAX Group where I worked on ERP software and web development., but now i’m a founder and CEO of TechBeeo Software Company Ltd. I’m also a Course Instructor of ZCPE PHP 7 Certification and professional web development course at w3programmers Training Institute – a leading Training Institute in the country.

5 comments on “Getting Started with Zend Framework 2 Part-2: Create a Zend Framework 2 Module
  1. To read and practice this article, I have strongly posered with the zend technology. Really it is very very secrete, which is nicely connected one another, if you understand and well known to use netbeens. I think next and next tutorials, I will enjoy….

    • Please share tutorial if you have so it will be great full for me to have zend knowledge….!!! looking forword

Leave a Reply

Your email address will not be published. Required fields are marked *