4. Zend Framework 2
Zend Framework 1 is great! Why do we need
new version?
● ZF1 is inflexible
● performance sucks
● difficult to learn
● doesn't use PHP 5.3 goodies
5. Zend Framework 2
● development started in 2010
● latest release is BETA3
● release cycle is following the "Gmail" style of
betas
● developed on GitHub
● no CLA needed anymore!
● aims to provide modern, fast web
framework...
● ...that solves all problems with its
predecessor
6. ZF1 ZF2
application config
configs module
controllers Application
modules config
views src
library Application
public Controller
view
public
vendor
7. New module system
module
Application
config "A Module is a
public collection of code and
src
Application
other files that solves
Controller
a more specific
Form atomic problem of the
Model larger business
Service problem"
view
8. Module class
class Module implements AutoloaderProvider {
public function init(Manager $moduleManager) // module initialization
{}
public function getAutoloaderConfig() // configure PSR-0 autoloader
{
return array(
'ZendLoaderStandardAutoloader' => array(
'namespaces' => array(
'Application' => __DIR__ . '/src/Application ',
)));
}
public function getConfig() // provide module configuration
{
return include __DIR__ . '/config/module.config.php';
}
}
13. Example: blog engine
class BlogEngine {
public $events;
public $blogPost;
public function __construct() {
$this->events = new EventManager(__CLASS__);
}
public function showBlogPost() {
$this->events->trigger('render', $this);
echo $this->blogPost;
}
}
14. Example: blog engine
class BlogEngine {
public $events;
public $blogPost;
public function __construct() {
$this->events = new EventManager(__CLASS__);
}
public function showBlogPost() {
$this->events->trigger('render', $this);
echo $this->blogPost;
}
}
// ...
$blogEngine->events->attach('render', function($event) {
$engine = $event->getTarget();
$engine->blogPost = strip_tags($engine->blogPost);
});
15. Dependency Injection
How do you manage your dependencies?
● globals, singletons, registry
public function indexAction() {
global $application;
$user = Zend_Auth::getInstance()->getIdentity();
$db = Zend_Registry::get('db');
}
16. Dependency Injection
How do you manage your dependencies?
● Zend_Application_Bootstrap
public function _initPdo() {
$pdo = new PDO(...);
return $pdo;
}
public function _initTranslations() {
$this->bootstrap('pdo');
$pdo = $this->getResource('pdo'); // dependency!
$stmt = $pdo->prepare('SELECT * FROM translations');
// ...
}
17. Solution: ZendDi
● First, let's consider simple service class:
class UserService {
protected $pdo;
public function __construct($pdo) {
$this->pdo = $pdo;
}
public function fetchAll() {
$stmt = $this->pdo->prepare('SELECT * FROM users');
$stmt->execute();
return $stmt->fetchAll();
}
}
19. Solution: ZendDi
● Use it from controllers:
public function indexAction() {
$sUsers = $this->locator->get('UserService');
$listOfUsers = $sUsers->fetchAll();
}
20. Definitions can be complex.
return array(
'di' => array(
'instance' => array(
'ZendViewRendererPhpRenderer' => array(
'parameters' => array(
'resolver' => 'ZendViewResolverAggregateResolver',
),
),
'ZendViewResolverAggregateResolver' => array(
'injections' => array(
'ZendViewResolverTemplateMapResolver',
'ZendViewResolverTemplatePathStack',
),
),
// Defining where the layout/layout view should be located
'ZendViewResolverTemplateMapResolver' => array(
'parameters' => array(
'map' => array(
'layout/layout' => __DIR__ . '/../view/layout/layout.phtml',
),
),
// ...
This could go on and on...