Skip to content
This repository has been archived by the owner on May 26, 2023. It is now read-only.

Commit

Permalink
Update to support the new ServiceManager and other recent changes to ZF2
Browse files Browse the repository at this point in the history
  • Loading branch information
akrabat committed May 19, 2012
1 parent 909c37b commit d3e9137
Show file tree
Hide file tree
Showing 12 changed files with 177 additions and 242 deletions.
5 changes: 5 additions & 0 deletions config/application.config.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,4 +12,9 @@
'./vendor',
),
),
'service_manager' => array(
'use_defaults' => true,
'factories' => array(
),
),
);
41 changes: 12 additions & 29 deletions module/Album/Module.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,22 +2,10 @@

namespace Album;

use Zend\Module\Manager;
use Zend\Module\Consumer\AutoloaderProvider;
use Zend\Form\View\HelperLoader as FormHelperLoader;


class Module implements AutoloaderProvider
class Module
{

public function init(Manager $moduleManager)
{
$sharedEvents = $moduleManager->events()->getSharedManager();
$sharedEvents->attach('bootstrap', 'bootstrap', array($this, 'onBootstrap'));
}



public function getAutoloaderConfig()
{
return array(
Expand All @@ -39,25 +27,20 @@ public function getConfig()

public function onBootstrap($e)
{
$app = $e->getParam('application');
$locator = $app->getLocator();
$this->helperLoader = $locator->get('Zend\View\HelperLoader');

$app->events()->attach('route', array($this, 'onRouteFinish'), -100);
$application = $e->getParam('application');
$sharedEventManager = $application->events()->getSharedManager();
$sharedEventManager->attach('Album', 'dispatch', array($this, 'onAlbumDispatched'), 2);
// (change 2 to -2 if you want to call the listener before the action is dispatched)
}

public function onRouteFinish($e)
public function onAlbumDispatched($e)
{
$matches = $e->getRouteMatch();
$controller = $matches->getParam('controller');
$namespace = substr($controller, 0, strpos($controller, '\\'));

if ($namespace !== __NAMESPACE__) {
return;
}

// only register form view helpers for this namespace
$this->helperLoader->registerPlugins(new FormHelperLoader());
// This is only called if a controller within our module has been dispatched
$app = $e->getParam('application');
$serviceManager = $app->getServiceManager();
$helperLoader = $serviceManager->get('Zend\View\HelperLoader');

$helperLoader->registerPlugins(new FormHelperLoader());
}

}
113 changes: 53 additions & 60 deletions module/Album/config/module.config.php
Original file line number Diff line number Diff line change
@@ -1,72 +1,65 @@
<?php

return array(
'di' => array(
// 'db' information should probably be in config/autoload/db.global.config.php
'db' => array(
'driver' => 'Pdo',
'dsn' => 'mysql:dbname=zf2tutorial;hostname=localhost',
'username' => 'rob',
'password' => '123456',
'driver_options' => array(
PDO::MYSQL_ATTR_INIT_COMMAND => 'SET NAMES \'UTF8\''
),
),

'instance' => array(
'Album\Controller\AlbumController' => array(
'parameters' => array(
'albumTable' => 'Album\Model\AlbumTable',
),
),
'Album\Model\AlbumTable' => array(
'parameters' => array(
'adapter' => 'Zend\Db\Adapter\Adapter',
)
),
'Zend\Db\Adapter\Adapter' => array(
'parameters' => array(
'driver' => array(
'driver' => 'Pdo',
'dsn' => 'mysql:dbname=zf2tutorial;hostname=localhost',
'username' => 'rob',
'password' => '123456',
'driver_options' => array(
PDO::MYSQL_ATTR_INIT_COMMAND => 'SET NAMES \'UTF8\''
),
),
)
),
'Zend\View\Resolver\TemplatePathStack' => array(
'parameters' => array(
'paths' => array(
'album' => __DIR__ . '/../view',
),
),
),
// Service Manager factories for creating an AlbumTable with a configured
// Db\Adapter.
'service_manager' => array(
'factories' => array (
'db-adapter' => function($sm) {

/**
* View helper(s)
*/
'Zend\View\HelperLoader' => array(
'parameters' => array(
'map' => array(
'zfcUserIdentity' => 'ZfcUser\View\Helper\ZfcUserIdentity',
'zfcUserLoginWidget' => 'ZfcUser\View\Helper\ZfcUserLoginWidget',
),
),
),
$config = $sm->get('config')->db->toArray();
$dbAdapter = new Zend\Db\Adapter\Adapter($config);
return $dbAdapter;
},
'album-table' => function($sm) {
$dbAdapter = $sm->get('db-adapter');
$table = new Album\Model\AlbumTable($dbAdapter);
return $table;
},
),
),

// Setup the router and routes
'Zend\Mvc\Router\RouteStackInterface' => array(
'parameters' => array(
'routes' => array(
'album' => array(
'type' => 'segment',
'options' => array(
'route' => '/album[/:action][/:id]',
'constraints' => array(
'action' => '[a-zA-Z][a-zA-Z0-9_-]*',
),
'defaults' => array(
'controller' => 'Album\Controller\AlbumController',
'action' => 'index',
),
),
),
// Controllers in this module
'controller' => array(
'classes' => array(
'album' => 'Album\Controller\AlbumController'
),
),

// Routes for this module
'router' => array(
'routes' => array(
'album' => array(
'type' => 'segment',
'options' => array(
'route' => '/album[/:action][/:id]',
'constraints' => array(
'action' => '[a-zA-Z][a-zA-Z0-9_-]*',
),
'defaults' => array(
'controller' => 'album',
'action' => 'index',
),
),
),
),
),

// View setup for this module
'view_manager' => array(
'template_path_stack' => array(
'album' => __DIR__ . '/../view',
),
),
);
23 changes: 16 additions & 7 deletions module/Album/src/Album/Controller/AlbumController.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ class AlbumController extends ActionController
public function indexAction()
{
return new ViewModel(array(
'albums' => $this->albumTable->fetchAll(),
'albums' => $this->getAlbumTable()->fetchAll(),
));
}

Expand All @@ -35,7 +35,7 @@ public function addAction()
if ($form->isValid()) {

$album->populate($form->getData());
$this->albumTable->saveAlbum($album);
$this->getAlbumTable()->saveAlbum($album);

// Redirect to list of albums
return $this->redirect()->toRoute('album');
Expand All @@ -52,7 +52,7 @@ public function editAction()
if (!$id) {
return $this->redirect()->toRoute('album', array('action'=>'add'));
}
$album = $this->albumTable->getAlbum($id);
$album = $this->getAlbumTable()->getAlbum($id);

$form = new AlbumForm();
$form->setBindOnValidate(false);
Expand All @@ -64,7 +64,7 @@ public function editAction()
$form->setData($request->post());
if ($form->isValid()) {
$form->bindValues();
$this->albumTable->saveAlbum($album);
$this->getAlbumTable()->saveAlbum($album);

// Redirect to list of albums
return $this->redirect()->toRoute('album');
Expand All @@ -89,7 +89,7 @@ public function deleteAction()
$del = $request->post()->get('del', 'No');
if ($del == 'Yes') {
$id = (int)$request->post()->get('id');
$this->albumTable->deleteAlbum($id);
$this->getAlbumTable()->deleteAlbum($id);
}

// Redirect to list of albums
Expand All @@ -101,13 +101,22 @@ public function deleteAction()

return array(
'id' => $id,
'album' => $this->albumTable->getAlbum($id)
'album' => $this->getAlbumTable()->getAlbum($id)
);
}

public function setAlbumTable(AlbumTable $albumTable)
{
$this->albumTable = $albumTable;
return $this;
}
}

public function getAlbumTable()
{
if (!$this->albumTable) {
$sm = $this->getServiceLocator();
$this->albumTable = $sm->get('album-table');
}
return $this->albumTable;
}
}
File renamed without changes.
22 changes: 2 additions & 20 deletions module/Application/Module.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,18 +2,8 @@

namespace Application;

use Zend\Module\Manager,
Zend\EventManager\StaticEventManager,
Zend\Module\Consumer\AutoloaderProvider;

class Module implements AutoloaderProvider
class Module
{
public function init(Manager $moduleManager)
{
$events = StaticEventManager::getInstance();
$events->attach('bootstrap', 'bootstrap', array($this, 'initializeView'), 100);
}

public function getAutoloaderConfig()
{
return array(
Expand All @@ -32,13 +22,5 @@ public function getConfig()
{
return include __DIR__ . '/config/module.config.php';
}

public function initializeView($e)
{
$app = $e->getParam('application');
$basePath = $app->getRequest()->getBasePath();
$locator = $app->getLocator();
$renderer = $locator->get('Zend\View\Renderer\PhpRenderer');
$renderer->plugin('basePath')->setBasePath($basePath);
}

}
Loading

0 comments on commit d3e9137

Please sign in to comment.