Zend Framework 2 - Basic Components

  • 3,948 views
Uploaded on

My presentation for meetphp #5

My presentation for meetphp #5

More in: Technology
  • Full Name Full Name Comment goes here.
    Are you sure you want to
    Your message goes here
  • What is the difference between zend framework 2.0 packages and zend framewirk 2.0 components

    What is the difference between packages and components.
    How different they are?
    Are you sure you want to
    Your message goes here
No Downloads

Views

Total Views
3,948
On Slideshare
0
From Embeds
0
Number of Embeds
0

Actions

Shares
Downloads
71
Comments
1
Likes
3

Embeds 0

No embeds

Report content

Flagged as inappropriate Flag as inappropriate
Flag as inappropriate

Select your reason for flagging this presentation as inappropriate.

Cancel
    No notes for slide

Transcript

  • 1. Zend Framework 2Basic components
  • 2. Who is this guy?name: Mateusz Tymekage: 26job: Developer at
  • 3. Zend Framework 2Zend Framework 1 is great! Why do we neednew version?
  • 4. Zend Framework 2Zend Framework 1 is great! Why do we neednew version?● ZF1 is inflexible● performance sucks● difficult to learn● doesnt 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 ZF2application config configs module controllers Application modules config views srclibrary Applicationpublic Controller view public vendor
  • 7. New module systemmodule 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 classclass 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; } }
  • 9. Module configurationDefault: User override:modules/Doctrine/config/module.config.php config/autoload/doctrine.local.config.phpreturn array( return array( // connection parameters // connection parameters connection => array( connection => array( driver => pdo_mysql, host => localhost, host => localhost, user => username, port => 3306, password => password, user => username, dbname => database, password => password, ), dbname => database, ); ), // driver settings driver => array( class =>DoctrineORMMappingDriverAnnotationDriver, namespace => ApplicationEntity, ),);
  • 10. ZendEventManagerWhy do we need aspect-orientedprogramming?
  • 11. ZendEventManagerDefine your eventsZendModuleManager ZendMvcApplication● loadModules.pre ● bootstrap● loadModule.resolve ● route● loadModules.post ● dispatch ● render ● finish
  • 12. ZendEventManagerAttach event listenersclass Module implements AutoloaderProvider{ public function init(Manager $moduleManager) { $events = $moduleManager->events(); $sharedEvents = $events->getSharedManager(); $sharedEvents->attach( ZendMvcApplication, finish, function(Event $e) { echo memory_get_usage(); }); }}
  • 13. Example: blog engineclass 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 engineclass 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 InjectionHow do you manage your dependencies?● globals, singletons, registrypublic function indexAction() { global $application; $user = Zend_Auth::getInstance()->getIdentity(); $db = Zend_Registry::get(db);}
  • 16. Dependency InjectionHow do you manage your dependencies?● Zend_Application_Bootstrappublic 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, lets 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(); }}
  • 18. Solution: ZendDi● Wire it with PDO, using DI configuration:return array( di => array( instance => array( PDO => array( parameters => array( dsn => mysql:dbname=test;host=127.0.0.1, username => root, passwd => )), UserService => array( parameters => array( pdo => PDO ))));
  • 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...
  • 21. Solution: ZendServiceLocatorSimplified application config:return array( view_manager => array( doctype => HTML5, not_found_template => error/404, exception_template => error/index, template_path_stack => array( application => __DIR__ . /../view, ), ),);
  • 22. What about performance? ● PSR-0 loader ● cache where possible ● DiC ● accelerator modules: EdpSuperluminal, ZfModuleLazyLoading
  • 23. More info● http://zendframework.com/zf2/● http://zend-framework-community.634137.n4. nabble.com/● https://github.com/zendframework/zf2● http://modules.zendframework.com/● http://mwop.net/blog.html
  • 24. Thank you!