Hello all, in my last blog I explained about how to configure Zend2 project, in this blog will see some more setting in Module of project with code writing in Controller and View.

Let’s start….

Check for module/Application/Module.php. If not exist create it.
Open terminal(ctrl+alt+t)

sudo vim module/Application/Module.php

And write following code :-

namespace Application;
use Zend\Mvc\MvcEvent;
 
class Module
{
    public function onBootstrap(MvcEvent $event)
    {
        $app = $event->getApplication();
        $eventManager = $app->getEventManager();
        $moduleRouteListener = $app->getServiceManager()
            ->get('ModuleRouteListener');
        $moduleRouteListener->attach($eventManager);
    }
 
    public function getConfig()
    {
        return include __DIR__ . '/config/module.config.php';
    }
 
    public function getControllerConfig()
    {
        return [
            'invokables' => [
                'Application\Controller\Index' => 'Application\Controller\IndexController'
            ],
        ];
    }
 
    public function getAutoloaderConfig()
    {
        return [
            'Zend\Loader\ClassMapAutoloader' => [
                __DIR__ . '/autoload_classmap.php'
            ]
        ];
    }
}

check for module/Application/autoload_classmap.php. If not exist
create it. Open terminal(ctrl+alt+t)

sudo vim module/Application/autoload_classmap.php

And write following code :-

<?php
return [
    'Application\Module'                        => __DIR__ . '/Module.php',
    'Application\Controller\IndexController'    => __DIR__ . '/src/Application/Controller/IndexController.php',
];

We have only two classes in this module our ‘Module.php’ and ‘IndexController.php’ and now these files will autoload.

Now, last file we need to modify is config/application.config.php

sudo vim config/application.config.php

And write following code :-

<?php

return [
    'modules' => [
        'Application',
    ],
    'module_listener_options' => [
        'module_paths' => [
            './vendor',
            './module',
        ],
        'config_glob_paths' => [
            'config/autoload/{,*.}{global,local}.php',
        ],
    ],
    'service_manager' => [
        'invokables' => [
            'ModuleRouteListener' => 'Zend\Mvc\ModuleRouteListener',
        ],
    ],
];

Now, all setting are done so we can start code to Controller.
now we need to write action in it
open it in editing mode.

sudo vim module/Application/src/Application/Controller/IndexController.php

And write following code in it :-

<?php
namespace Application\Controller;
use Zend\Mvc\Controller\AbstractActionController;
class IndexController extends AbstractActionController
{
    public function indexAction()
    {
        return new ViewModel();
    }
}

Now, we need to create view for this, So open terminal and create a view.

mkdir -p module/Application/view/application/index/index.phtml

Open it in editor

sudo vim module/Application/view/application/index/index.phtml

And now,write following code:-

<?php 
echo "Hello World"
?>

Now, we are all done. To check everything is working open browser and type zf2.localhost.com will get “Hello World” on webpage.

Leave a comment