Zend Framework 2 and Symfony2: The Perfect Team · PHP frameworks Symfony and Zend Framework are...

41
Zend Framework 2 and Symfony2: Zend Framework 2 and Symfony2: The Perfect Team The Perfect Team Stefan Koopmanschap, Enrico Zimuel 18th May – Verona (Italy)

Transcript of Zend Framework 2 and Symfony2: The Perfect Team · PHP frameworks Symfony and Zend Framework are...

Page 1: Zend Framework 2 and Symfony2: The Perfect Team · PHP frameworks Symfony and Zend Framework are the most used PHP frameworks worldwide Both offer a tons of features that can improve

Zend Framework 2 and Symfony2:Zend Framework 2 and Symfony2:The Perfect TeamThe Perfect Team

Stefan Koopmanschap, Enrico Zimuel

18th May – Verona (Italy)

Page 2: Zend Framework 2 and Symfony2: The Perfect Team · PHP frameworks Symfony and Zend Framework are the most used PHP frameworks worldwide Both offer a tons of features that can improve

About us

● Stefan Koopmanschap

● Enterpreneur: Ingewikkeld and Techademy

● Member of Dutch Web Alliance

● Symfony Community Manager

● Co-Founder PHPBenelux, now PFZ.nl

[email protected]

● Enrico Zimuel

● Software Engineer at Zend Technologies

● Zend Framework Core Team

● Co-founder PUG Turin

[email protected]

Page 3: Zend Framework 2 and Symfony2: The Perfect Team · PHP frameworks Symfony and Zend Framework are the most used PHP frameworks worldwide Both offer a tons of features that can improve

PHP frameworks

● Symfony and Zend Framework are the most used PHP frameworks worldwide

● Both offer a tons of features that can improve the development of web applications

● Modular architectures and high quality PHP code (SoC + SOLID principles)

● Why don't use it together to get the best features of both?

Page 4: Zend Framework 2 and Symfony2: The Perfect Team · PHP frameworks Symfony and Zend Framework are the most used PHP frameworks worldwide Both offer a tons of features that can improve

Religion war?

ZF sucks!Symfony?Oh My!

Page 5: Zend Framework 2 and Symfony2: The Perfect Team · PHP frameworks Symfony and Zend Framework are the most used PHP frameworks worldwide Both offer a tons of features that can improve

Instead of think aboutZF2 || Symfony 2

think about

ZF2 && Symfony 2

Page 6: Zend Framework 2 and Symfony2: The Perfect Team · PHP frameworks Symfony and Zend Framework are the most used PHP frameworks worldwide Both offer a tons of features that can improve

PSR-*

● Framework interoperability standards● Meant to make it easier to work with multiple

frameworks● Naming standards and coding standards● For instance: Only one autoloader!● http://www.php-fig.org/

Page 7: Zend Framework 2 and Symfony2: The Perfect Team · PHP frameworks Symfony and Zend Framework are the most used PHP frameworks worldwide Both offer a tons of features that can improve

PSR-0

● Autoloading standard:

\<Vendor Name>\(<Namespace>\)*<Class Name>

Each namespace separator is converted to a DIRECTORY_SEPARATOR when loading from the file system

Page 8: Zend Framework 2 and Symfony2: The Perfect Team · PHP frameworks Symfony and Zend Framework are the most used PHP frameworks worldwide Both offer a tons of features that can improve

PSR-1

● Basic coding standard:

Files MUST use only <?php and <?= tagsNamespaces and classes MUST follow PSR-0Class constants MUST be declared in all upper case with underscore separatorsMethod names MUST be declared in camelCase...

Page 9: Zend Framework 2 and Symfony2: The Perfect Team · PHP frameworks Symfony and Zend Framework are the most used PHP frameworks worldwide Both offer a tons of features that can improve

PSR-2

● Coding style guide:

Code MUST follow PSR-1Code MUST use 4 spaces for indenting, not tabsThere MUST be one blank line after the namespace declaration, and there MUST be one blank line after the block of use declarationsOpening braces for classes MUST go on the next line, and closing braces MUST go on the next line after the body...

Page 10: Zend Framework 2 and Symfony2: The Perfect Team · PHP frameworks Symfony and Zend Framework are the most used PHP frameworks worldwide Both offer a tons of features that can improve

PSR-3

● Logger interface:

The LoggerInterface exposes eight methods to write logs to the eight RFC 5424 levels (debug, info, notice, warning, error, critical, alert, emergency)A ninth method, log, accepts a log level as first argument. Calling this method with one of the log level constants MUST have the same result as calling the level-specific method...

Page 11: Zend Framework 2 and Symfony2: The Perfect Team · PHP frameworks Symfony and Zend Framework are the most used PHP frameworks worldwide Both offer a tons of features that can improve
Page 12: Zend Framework 2 and Symfony2: The Perfect Team · PHP frameworks Symfony and Zend Framework are the most used PHP frameworks worldwide Both offer a tons of features that can improve

Composer

● Composer (getcomposer.org) can help the management of projects that use ZF2 and Symfony2– Select your favorite ZF2 and Symfony2

components using composer.json– $ php composer.phar install– Components installed in /vendor – Include the /vendor/autoload.php in your PHP

code, that's it!

Page 13: Zend Framework 2 and Symfony2: The Perfect Team · PHP frameworks Symfony and Zend Framework are the most used PHP frameworks worldwide Both offer a tons of features that can improve

How to integrateHow to integrateZend Framework 2 in Symfony 2Zend Framework 2 in Symfony 2

Page 14: Zend Framework 2 and Symfony2: The Perfect Team · PHP frameworks Symfony and Zend Framework are the most used PHP frameworks worldwide Both offer a tons of features that can improve

composer create-project symfony/framework-standard-edition

project

Page 15: Zend Framework 2 and Symfony2: The Perfect Team · PHP frameworks Symfony and Zend Framework are the most used PHP frameworks worldwide Both offer a tons of features that can improve

Composer.json

"repositories": [ { "type": "composer", "url": "http://packages.zendframework.com/" } ],

Page 16: Zend Framework 2 and Symfony2: The Perfect Team · PHP frameworks Symfony and Zend Framework are the most used PHP frameworks worldwide Both offer a tons of features that can improve

Composer.json

"require": { "php": ">=5.3.3", "symfony/symfony": "2.2.*", (...) "zendframework/zend-crypt": "2.2.*" },

Page 17: Zend Framework 2 and Symfony2: The Perfect Team · PHP frameworks Symfony and Zend Framework are the most used PHP frameworks worldwide Both offer a tons of features that can improve

composer update

Page 18: Zend Framework 2 and Symfony2: The Perfect Team · PHP frameworks Symfony and Zend Framework are the most used PHP frameworks worldwide Both offer a tons of features that can improve
Page 19: Zend Framework 2 and Symfony2: The Perfect Team · PHP frameworks Symfony and Zend Framework are the most used PHP frameworks worldwide Both offer a tons of features that can improve

Now let's get to work

Page 20: Zend Framework 2 and Symfony2: The Perfect Team · PHP frameworks Symfony and Zend Framework are the most used PHP frameworks worldwide Both offer a tons of features that can improve

Register Crypt class as service

Page 21: Zend Framework 2 and Symfony2: The Perfect Team · PHP frameworks Symfony and Zend Framework are the most used PHP frameworks worldwide Both offer a tons of features that can improve

src/ZendCon/CryptBundle/Resources/config/services.xml

Page 22: Zend Framework 2 and Symfony2: The Perfect Team · PHP frameworks Symfony and Zend Framework are the most used PHP frameworks worldwide Both offer a tons of features that can improve

src/ZendCon/CryptBundle/Controller/CryptController.php

src/ZendCon/CryptBundle/Resources/views/Crypt/index.html.twig

Page 23: Zend Framework 2 and Symfony2: The Perfect Team · PHP frameworks Symfony and Zend Framework are the most used PHP frameworks worldwide Both offer a tons of features that can improve

http://zendcon.local/crypt

Page 24: Zend Framework 2 and Symfony2: The Perfect Team · PHP frameworks Symfony and Zend Framework are the most used PHP frameworks worldwide Both offer a tons of features that can improve

src/ZendCon/CryptBundle/Controller/CryptController.php

src/ZendCon/CryptBundle/Resources/views/Crypt/check.html.twig

Page 25: Zend Framework 2 and Symfony2: The Perfect Team · PHP frameworks Symfony and Zend Framework are the most used PHP frameworks worldwide Both offer a tons of features that can improve

http://zendcon.local/crypt

Page 26: Zend Framework 2 and Symfony2: The Perfect Team · PHP frameworks Symfony and Zend Framework are the most used PHP frameworks worldwide Both offer a tons of features that can improve

Other (better?) solutions

● Do validation in Entity● Use Symfony2 Validator

Page 27: Zend Framework 2 and Symfony2: The Perfect Team · PHP frameworks Symfony and Zend Framework are the most used PHP frameworks worldwide Both offer a tons of features that can improve

BlockCipher

Page 28: Zend Framework 2 and Symfony2: The Perfect Team · PHP frameworks Symfony and Zend Framework are the most used PHP frameworks worldwide Both offer a tons of features that can improve

src/ZendCon/CryptBundle/Controller/CryptController.php

src/ZendCon/CryptBundle/Resources/views/Crypt/blockcipher.html.twig

Page 29: Zend Framework 2 and Symfony2: The Perfect Team · PHP frameworks Symfony and Zend Framework are the most used PHP frameworks worldwide Both offer a tons of features that can improve

http://zendcon.local/crypt/block

Page 30: Zend Framework 2 and Symfony2: The Perfect Team · PHP frameworks Symfony and Zend Framework are the most used PHP frameworks worldwide Both offer a tons of features that can improve

Some stuff I won't show...

● Zend\Barcode● Zend\Captcha● Zend\Feed● Zend\I18n● ZendService_*

Page 31: Zend Framework 2 and Symfony2: The Perfect Team · PHP frameworks Symfony and Zend Framework are the most used PHP frameworks worldwide Both offer a tons of features that can improve

How to integrateHow to integrateSymfony2 in Zend Framework 2Symfony2 in Zend Framework 2

Page 32: Zend Framework 2 and Symfony2: The Perfect Team · PHP frameworks Symfony and Zend Framework are the most used PHP frameworks worldwide Both offer a tons of features that can improve

How to use Symfony2 in ZF2● Use composer.json to include the Symfony2

components● Execute the update (or install) command of

composer● The Symfony2 composer will be installed in /vendor● MVC approach:

● Register the Symfony2 components in the ServiceManager

● “Standard” approach:● Use the Symfony2 components directly

(instantiate the class and use it)

Page 33: Zend Framework 2 and Symfony2: The Perfect Team · PHP frameworks Symfony and Zend Framework are the most used PHP frameworks worldwide Both offer a tons of features that can improve

composer.json{ "require": { "php": ">=5.3.3", "zendframework/zendframework": "2.0.*", "symfony/yaml": "2.2.*", "symfony/dom-crawler": "2.2.*", }, "autoload": { "psr-0": { "Application": "module/Application/src" } }}

Page 34: Zend Framework 2 and Symfony2: The Perfect Team · PHP frameworks Symfony and Zend Framework are the most used PHP frameworks worldwide Both offer a tons of features that can improve

Symfony component as invokable

// a module config "module/SomeModule/config/module.config.php"return array( 'service_manager' => array( 'invokables' => array( // Keys are the service names // Values are valid class names to instantiate 'YamlParser' => 'Symfony\Component\Yaml\Parser', 'DomCrawler' => 'Symfony\Component\DomCrawler\Crawler',

// ... ), ));

Page 35: Zend Framework 2 and Symfony2: The Perfect Team · PHP frameworks Symfony and Zend Framework are the most used PHP frameworks worldwide Both offer a tons of features that can improve

Symfony component as factory

// a module config "module/SomeModule/config/module.config.php"return array( 'service_manager' => array( 'factories' => array( // Keys are the service names. // Valid values include names of classes implementing // FactoryInterface, instances of classes implementing // FactoryInterface, or any PHP callbacks 'YamlParser' => 'Symfony\Component\Yaml\Parser', 'DomCrawler' => function ($html) {

return new Symfony\Component\DomCrawler\Crawle($html)}

// ... ), ));

Page 36: Zend Framework 2 and Symfony2: The Perfect Team · PHP frameworks Symfony and Zend Framework are the most used PHP frameworks worldwide Both offer a tons of features that can improve

Use a service in a Controller

class AlbumController extends AbstractActionController{ public function indexAction() { $sm = $this->getServiceLocator(); $yamlParser = $sm->get('YamlParser'); $domCrawler = $sm->get('DomCrawler'); } // ...}

Page 37: Zend Framework 2 and Symfony2: The Perfect Team · PHP frameworks Symfony and Zend Framework are the most used PHP frameworks worldwide Both offer a tons of features that can improve

Symfony 2 components

● Some components useful for ZF2 users:– BrowserKit (simulates a web browser)

– CssSelector

– Filesystem

– Finder (file search)

– OptionsResolver

– Process (executes commands in sub-processes)

– Serializer (handle serializing data structures)

– YAML parser

Page 38: Zend Framework 2 and Symfony2: The Perfect Team · PHP frameworks Symfony and Zend Framework are the most used PHP frameworks worldwide Both offer a tons of features that can improve

ConcludingConcluding

Page 39: Zend Framework 2 and Symfony2: The Perfect Team · PHP frameworks Symfony and Zend Framework are the most used PHP frameworks worldwide Both offer a tons of features that can improve

To integrate

● PSR / Framework Interoperability● Composer● Pick your components● Don't stop at Zend Framework/Symfony● Don't Reinvent The Wheel!

Page 40: Zend Framework 2 and Symfony2: The Perfect Team · PHP frameworks Symfony and Zend Framework are the most used PHP frameworks worldwide Both offer a tons of features that can improve

Where to go?

http://packagist.org

Page 41: Zend Framework 2 and Symfony2: The Perfect Team · PHP frameworks Symfony and Zend Framework are the most used PHP frameworks worldwide Both offer a tons of features that can improve

Thanks!

http://framework.zend.com

Give feedback:

https://joind.in/8645

http://www.symfony.com