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 :
After we clone, there must be a structure directory like this :
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
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.gitThen we could see our module like this :
Note :
- src folder is used for put your PHP class files.
- view folder is used to put your view files (html).
Then we could start to setting :
- Register the module on ZendSkeletonApplication\config\application.config.php
- Setting autoload_classmap.php
- Setting module
- Setting module configuration
- Then make our controller in folder src\Test\Controller :
- Let’s show in the view ( Test\view\test\index.phtml )
array( 'Application', 'Test', ), 'module_listener_options' => array( 'config_glob_paths' => array( 'config/autoload/{,*.}{global,local}.php', ), 'module_paths' => array( './module', './vendor', ), ), );
__DIR__ . '/Module.php', 'Test\Controller\TestController' => __DIR__ . '/src/Test/Controller/TestController.php', );
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'; } }
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', ), ), );
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; } }
echo "Greeting : ".$greeting;