Hi all, in my last blog I explained how to “Install Zend framework 2 in Linux”.
Now, I am try to explaining how we can write code in Zend Framework 2.

After installing Zend Framework 2, Project folder structure will look like below

zf2-demo
|--config
|  |--aplication.config.php
|  |--autoload
|     |--global.php
|--module
|  |--Application
|     |--config
|     |  |--module.config.php
|     |--src
|     |  |--Application
|     |     |--Controller
|     |        |--IndexController.php
|     |--view
|     |  |--application
|     |     |--index
|     |     |  |-index.phtml
|     |     |--error
|     |     |  |--404.phtml
|     |     |  |--index.phtml
|     |     |--layout
|     |     |  |--layout.phtml
|     |--autoload_classmap.php
|     |--Module.php
|--public
|  |--.htaccess
|  |--index.php
|  |--css
|  |--js
|  |--img
|--vendor
|  |--autoload.php
|  |--bin
|  |--composer
|  |--zendframework
|--composer.json
|--composer.lock 

Now, we need to modify 3 files in setting.
1. zf2-demo/public/.htaccess (if not present create it)
2. zf2-demo/public/.index.php
3. zf2-demo/config/application.config.php

Open first file i.e .htaccess in terminal (ctrl+alt+t)

  sudo vim zf2-demo/public/.htaccess
 

Now, overwrite .htaccess file with following code :-

RewriteEngine On
RewriteCond %{REQUEST_FILENAME} -s [OR]
RewriteCond %{REQUEST_FILENAME} -l [OR]
RewriteCond %{REQUEST_FILENAME} -d
RewriteRule ^.*$ - [NC,L]
RewriteRule ^.*$ index.php [NC,L]

Save it and open index.php and overwrite with following code:-

<?php
chdir(dirname(__DIR__));

// Setup autoloading
require 'init_autoloader.php';

// Run the application!
Zend\Mvc\Application::init(require 'config/application.config.php')->run();

Save it and open config/application.config.php and overwrite following code :-

<?php
return array(
    'modules' => array(
        'Application'
    ),
    'module_listener_options' => array(
        'config_glob_paths'    => array(
            'config/autoload/{,*.}{global,local}.php',
        ),
        'module_paths' => array(
            './module',
            './vendor',
        ),
    ),
);

Check for local.php and global.php
if they don’t exist create them as shown above in folder structure with
following code.

<?php
return [
];

That’s all for setting up, now we need to write code for Module.
Open module/Application/config/module.config.php and write the following code.

<?php
return [
    'router'    => [
        'routes' => [
            'home' => [
                'type'      => 'Literal',
                'options'   => [
                    'route'     => '/',
                    'defaults'  => [
                        '__NAMESPACE__' => 'Application\Controller',
                        'controller'    => 'Index',
                        'action'        => 'index',
                    ],
                ],
            ],
        ],
    ],
    'navigation' => [
        'default' => [
            'home' => [
                'label' => 'Home',
                'route' => 'home'
            ],
        ],
    ],
    'view_manager'  => [
        'doctype'                   => 'HTML5',
        'display_exceptions'        => true,
        'exception_template'        => 'error/index',
        'display_not_found_reason'  => true,
        'not_found_template'        => 'error/404',
        'template_map'              => [
            'layout/layout'           => __DIR__ . '/../view/layout/layout.phtml',
            'error/404'               => __DIR__ . '/../view/error/404.phtml',
            'error/index'             => __DIR__ . '/../view/error/index.phtml',
            'application/index/index' => __DIR__ . '/../view/application/index/index.phtml',
        ],
    ],
];

Now, we are done with the initial set-up of project using Zend Framework 2. In my next blog we will go through the actual code which will print “Hello World” for us, the reason we are reading this post.

Comments on: "Getting started with Zend 2 – “Hello World” Part-1" (2)

  1. Nice 🙂

    Like

Leave a comment