Zend Framework2: Tutorial Part2: Hello World Module Application

The reason I start to write this post, because I read saw a lot video on youtube.com take hours to tell you how do it. And I also follow it but it is waste my time. Didn’t work for me. And I write this as my study record also hope that could help someone to start Zend Framework2.

Actually it is very simple,

First,copy the entire “Application file” and rename it to “Hello” (You could just rename the “Application” to “Hello” or any name you like, but under this file those three name must match )

HelloModuleZendFrameWork

Second, open the “Module.php”, the namespace Hello (this namespace should match your module name).

see below

HelloModuleZendFrameWork1

Third, Write your control, go to the src file open the IndexController.php

here is my function. here is the time you could write your function here if you like it.

public function indexAction()
{
$views = new ViewModel(array(‘text’=>’Hello World, My first application’));
return $views;
}

Hello world ModuleZendFrameWork2
Hello world ModuleZendFrameWork2

4. go to you view file, open the index.phtml

clear the entire page , write this:

<h2>Hello world</h2>
<p><?php echo $this->text; ?></p>

Hello World ModuleZendFrameWork2
Hello World
ModuleZendFrameWork2

5. go to config/module.config.php

there have some line need change, just change the module name “Application” to “Hello”, you file should look like this:

<?php

return array(
‘router’ => array(
‘routes’ => array(
hello‘ => array(
‘type’ => ‘Zend\Mvc\Router\Http\Literal’,
‘options’ => array(
‘route’    => ‘/hello‘,
‘defaults’ => array(
‘controller’ => ‘Hello\Controller\Index’,
‘action’     => ‘index’,
),
),
),

‘hello’ => array(
‘type’    => ‘Literal’,
‘options’ => array(
‘route’    => ‘/hello’,
‘defaults’ => array(
‘__NAMESPACE__’ => ‘Hello\Controller’,
‘controller’    => ‘Index’,
‘action’        => ‘index’,
),
),
‘may_terminate’ => true,
‘child_routes’ => array(
‘default’ => array(
‘type’    => ‘Segment’,
‘options’ => array(
‘route’    => ‘/[:controller[/:action]]’,
‘constraints’ => array(
‘controller’ => ‘[a-zA-Z][a-zA-Z0-9_-]*’,
‘action’     => ‘[a-zA-Z][a-zA-Z0-9_-]*’,
),
‘defaults’ => array(
),
),
),
),
),
),
),
‘service_manager’ => array(
‘abstract_factories’ => array(
‘Zend\Cache\Service\StorageCacheAbstractServiceFactory’,
‘Zend\Log\LoggerAbstractServiceFactory’,
),
‘aliases’ => array(
‘translator’ => ‘MvcTranslator’,
),
),
‘translator’ => array(
‘locale’ => ‘en_US’,
‘translation_file_patterns’ => array(
array(
‘type’     => ‘gettext’,
‘base_dir’ => __DIR__ . ‘/../language’,
‘pattern’  => ‘%s.mo’,
),
),
),
‘controllers’ => array(
‘invokables’ => array(
Hello\Controller\Index’ => ‘Hello\Controller\IndexController’
),
),
‘view_manager’ => array(
‘display_not_found_reason’ => true,
‘display_exceptions’       => true,
‘doctype’                  => ‘HTML5’,
‘not_found_template’       => ‘error/404’,
‘exception_template’       => ‘error/index’,
‘template_map’ => array(
‘layout/layout’           => __DIR__ . ‘/../view/layout/layout.phtml’,
‘hello/index/index’ => __DIR__ . ‘/../view/hello/index/index.phtml’,
‘error/404’               => __DIR__ . ‘/../view/error/404.phtml’,
‘error/index’             => __DIR__ . ‘/../view/error/index.phtml’,
),
‘template_path_stack’ => array(
__DIR__ . ‘/../view’,
),
),
// Placeholder for console routes
‘console’ => array(
‘router’ => array(
‘routes’ => array(
),
),
),
);

Now time to ‘http://zf2.local/hello&#8217; to test your module.

After succeed should look like this

HelloModuleZendFrameWork0
Hello world ModuleZendFrameWork2
Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s