Thursday 4 October 2012

Zend Framework 2 : Instalation and Make a Module

On 3 March 2012, Zend has released zend framework version 2.0. In zend framework 2.0, you will feel the different from previous version (Zend Framework version 1). Some of the different are zend framework 2 is more solid coding and  better performance. Btw, if you wanna try it you should use PHP version 5.3.For installing Zend Framework 2, we could clone GIT version of ZendSkeletonApplication as the basic foundation from our application.
Ok here we go.

First, clone the skeleton by using terminal or everything that could clone it :
git clone --recursive git://github.com/zendframework/ZendSkeletonApplication.git
After we clone, there must be a structure directory like this :


If in the first process of cloning success, we could try to by access : http://localhost/ZendSkeletonApplication/public on your browser.If there is no error, the view of our browser must be like this :




Umm well, if it works properly we could learn to make a simple module. Btw, what is "module" on Zend Framework 2 ? If you don't know what is module, I'll explain simple definition.

Module is a collection of program codes and other files that support to solve specific problem in an application.

Ok, let's we start it by  clone ZendSkeletonModule module and see the structure of the module :

git clone https://github.com/zendframework/ZendSkeletonModule.git

Then we could see our module like this :


Note : 
  1. src folder is used for put your PHP class files.
  2. view folder is used to put your view files (html).
From the Skeleton module, we try to rename the module with the other name, for example Test module, for more details the module that has renamed should be like this : 



Then we could start to setting :

  1. Register the module on ZendSkeletonApplication\config\application.config.php
  2.  array(
            'Application',
            'Test',
        ),
        'module_listener_options' => array(
            'config_glob_paths'    => array(
                'config/autoload/{,*.}{global,local}.php',
            ),
            'module_paths' => array(
                './module',
                './vendor',
            ),
        ),
    );
    
  3. Setting autoload_classmap.php
  4.  __DIR__ . '/Module.php',
        'Test\Controller\TestController' => __DIR__ . '/src/Test/Controller/TestController.php',
    );
    
  5. Setting module
  6.  namespace Test;
      
     use Zend\Module\Consumer\AutoloaderProvider;
      
     class Module implements AutoloaderProviderInterface
    {
        public function getAutoloaderConfig()
        {
            return array(
                'Zend\Loader\ClassMapAutoloader' => array(
                    __DIR__ . '/autoload_classmap.php',
                ),
                'Zend\Loader\StandardAutoloader' => array(
                    'namespaces' => array(
          // if we're in a namespace deeper than one level we need to fix the \ in the path
                        __NAMESPACE__ => __DIR__ . '/src/' . str_replace('\\', '/' , __NAMESPACE__),
                    ),
                ),
            );
        }
    
        public function getConfig()
        {
            return include __DIR__ . '/config/module.config.php';
        }
    
    }
    
  7. Setting module configuration
  8.  array(
            'invokables' => array(
                'Test\Controller\Test' => 'Test\Controller\TestController',
            ),
        ),
        'router' => array(
            'routes' => array(
                'test' => array(
                    'type'    => 'Literal',
                    'options' => array(
                        // Change this to something specific to your module
                        'route'    => '/test',
                        'defaults' => array(
                            // Change this value to reflect the namespace in which
                            // the controllers for your module are found
                            //'__NAMESPACE__' => 'Test\Controller',
                            'controller'    => 'Test\Controller\Test',
                            'action'        => 'index',
                        ),
                    ),
                ),
            ),
        ),
        'view_manager' => array(
            'template_path_stack' => array(
                'test' => __DIR__ . '/../view',
            ),
        ),
    );
    
  9. Then make our controller in folder src\Test\Controller :
  10. namespace Test\Controller;
    
    use Zend\Mvc\Controller\AbstractActionController;
    use Zend\View\Model\ViewModel;
    
    class TestController extends AbstractActionController
    {
        public function indexAction()
        {
            $view = new ViewModel();
            $view->greeting = "Hallo everybody !!!";
            
            return $view;
        }
    
    }
    
  11. Let’s show in the view ( Test\view\test\index.phtml )
  12. echo "Greeting : ".$greeting; 
    


source :
  1. http://samsonasik.wordpress.com/2012/03/06/zend-framework-2-instalasi-dan-membuat-module/
  2. http://www.framework.zend.com/manual/2.0/en/user-guide/overview.html 
Copyright © 2012 Clighter | Powered by Blogger