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

Create module in Zend Framework 3

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 3.

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:

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:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
<?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:

READ  Getting Started with Zend Framework 2 Part-1( Zend Framework 2 Project Structure)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
<?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:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
<?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 :

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
<?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:

READ  ZfcUser is a user registration and authentication module for Zend Framework 3

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.

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 !

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 3 .

Leave a Reply

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