Reusable ZF2 Modules

 Autoloading

<?php
namespace foo;

use ZendModuleConsumerAutoloaderProvider;

class Module implements AutoloaderProvider
{
  public function getConfig()
  {
    return include __DIR__.’/config/module.config.php’;
  }
  public function getAutoloaderConfig
  {
    return array(
    ‘ZendLoaderClassMapAutoloader’ => array(include __DIR__.’/autoload_classmap.php’),
    ‘ZendLoaderStandardAutoloader’ => array(‘namespaces’=>array(‘Foo’=>__DIR__.’/src/Foo’)),
    );
  }
}

Typical module configuration

 <?php
return array(‘di’=>array(
  ‘instance’=>array(‘ZendMvcRouterRouteStack’=>
    array(‘parameters’=>array(
      ‘routes’=>array(
        ‘foo’=>array(
          ‘type’=>’Literal’,
          ‘options’=>array(
            ‘route’=>’/foo’
            ‘defaults’=>array(
               ‘controller’=>’FooControllerIndexController’
            ),
          ),
          ‘may_terminate’=>true,
          ‘child_routes’=>array( /**/ )
        )
      )
    ))
  )
)); 

Typical event registration

<?php
namespace foo;

use ZendEventManagerStaticEventManager;
        ZendModuleManager as ModuleManager;

class Module
{
  public function init(ModuleManager $manager)
  {
    $events = StaticEventManager::getInstance();
    $events->attach(
      ‘bootstrap’,’bootstrap’,
      array($this, ‘bootstrap’)
    );
  }
  public function bootstrap($e)
  {
    $app = $e->getParam(‘application’);
    //access to service locator to be able to register other listeners and do some stuff
  }
}

Module specific event listeners

<?php

/* ..*/
  public function bootstrap($e)
  {
    $app = $e->getParam(‘application’);
    $app->events()->attach(‘route’ array($this, ‘moduleCheck’), -100); //priority
  }
  public function moduleCheck($e)
  {
    $matches = $e->getRouteMatch();
    $controller = $matches->getParam(‘controller’, ”);
    if(0 !== strpos($controller, __NAMESPACE__)){
      //Not our module
      return;
    }
    //do stuff
  }
}

Example contact form

  • Configure routes in modulePhlyContactconfigmodule.config.php
  • Define the form in modulePhlyContactsrcPhlyContactContactForm.php
  • Code the controller and their actions
  • Add view scripts
  • Definition classes of the module in modulePhlyContactconfigmodule.config.php
  • Inject configurations for used classes like ZendMailMessage and preferences for ZendMailTransport
  • View scripts injection
  • Create autoloader map
  • Create the module class
  • Remember to register your module in config/application.config.php

Why is reusable?

Via configuration…

  • You can change base route
  • You can change preferences of sending method
  • You can change the captcha adapter

More info

    Complete code: https://github.com/weierophinney/PhlyContact

    Presentation: http://www.zend.com/en/webinar/Framework/70170000000bkF8-reusablezf2modules.flv

    Anuncis

    Deixa un comentari

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

    WordPress.com Logo

    Esteu comentant fent servir el compte WordPress.com. Log Out / Canvia )

    Twitter picture

    Esteu comentant fent servir el compte Twitter. Log Out / Canvia )

    Facebook photo

    Esteu comentant fent servir el compte Facebook. Log Out / Canvia )

    Google+ photo

    Esteu comentant fent servir el compte Google+. Log Out / Canvia )

    Connecting to %s


    %d bloggers like this: