How to create a module and a new controller in Zend Framework 2

In this tutorial I will speak again about the Zend Framework. In my next tutorials I will show you how is easy to use Zend Framework 2.5.

Tn this tutorial I will explain how to create a new module and how to create a new controller.

First, I have installed Zend Framework 2.5.1 in Xampp.

Then, I have downloaded the zftool.phar from here(you can install it if you want).

Now, I will open the command line and I will enter this command php zftool.phar create module Newapp to create a new module.

To create a new controller in my module, I will enter in my command line php zftool.phar create controller Newapp Newapp

php zftool.phar create controller "the name of my controller" "the name of my module where I want to create my controller"

The new controller will be created here module\Newapp\src\Newapp\Controller

Now I will register my new controller and I will create a new route in the file module/Newapp/config/module.config.php by adding this code:


array(
'invokables' => array(
'Newapp\Controller\Newapp' => 'Newapp\Controller\NewappController',
),
),

// The following section is new and should be added to your file
'router' => array(
'routes' => array(
'album' => array(
'type' => 'segment',
'options' => array(
'route' => '/newapp',
'constraints' => array(
'action' => '[a-zA-Z][a-zA-Z0-9_-]*'
),
'defaults' => array(
'controller' => 'Newapp\Controller\Newapp',
'action' => 'index',
),
),
),
),
),

'view_manager' => array(
'template_path_stack' => array(
'album' => __DIR__ . '/../view',
),
),
);

Now, I will open my browser and I will enter http://localhost:8888/newapp to see my new module.

Vide tutorial:









Leave a Comment