Click here to Skip to main content
15,887,585 members
Articles / Zend

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

Rate me:
Please Sign up or sign in to vote.
5.00/5 (2 votes)
14 Oct 2014CPOL 5.6K   1   2
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).

C#
sudo vim module/Application/Module.php

And write the following code:

C#
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
<?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.

C#
sudo vim config/application.config.php

And write the following code:

PHP
<?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
<?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
<?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.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Software Developer
India India
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.

Comments and Discussions

 
QuestionGetting Error on the hello world example ViewModel Class not found... Pin
Member 1259089718-Jun-16 0:03
Member 1259089718-Jun-16 0:03 
GeneralMy Vote 5 Pin
Shemeemsha (ഷെമീംഷ)30-Oct-14 19:52
Shemeemsha (ഷെമീംഷ)30-Oct-14 19:52 
Good Blog Smile | :) Smile | :) Thumbs Up | :thumbsup: Thumbs Up | :thumbsup: Thumbs Up | :thumbsup: Thumbs Up | :thumbsup: Thumbs Up | :thumbsup:

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.