65.9K
CodeProject is changing. Read more.
Home

Getting Started with Zend 2 – “Hello World” - Part 2

starIconstarIconstarIconstarIconstarIcon

5.00/5 (2 votes)

Oct 14, 2014

CPOL
viewsIcon

5821

Getting started with Zend 2

In my last blog, I explained about how to configure Zend2 project. In this blog, we 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 it does not exist, create it.
Open terminal(ctrl+alt+t).

sudo vim module/Application/Module.php

And write the 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 it does not exist, create it. Open terminal(ctrl+alt+t).

sudo vim module/Application/autoload_classmap.php

And write the 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, the last file we need to modify is config/application.config.php.

sudo vim config/application.config.php

And write the 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 settings 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 the 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 the following code:

<?php 
echo "Hello World"
?>

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