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


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:

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:

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

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

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

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.


Leave A Reply

Your email address will not be published.