Drupal symfony

63
Symfony is not scary Valeriy Tuz Inuits

Transcript of Drupal symfony

Page 1: Drupal symfony

Symfony is not scary

Valeriy Tuz Inuits

Page 2: Drupal symfony

Is it scary?..

Page 3: Drupal symfony

Is it scary?.. No! Let me try it!!!- Interior- Driving- Engine- Assembly- Service- etc

Page 4: Drupal symfony

Symfony is not scary → Why I love Symfony2

Page 5: Drupal symfony

Just try it!

$ curl -s http://getcomposer.org/installer | php

$ php composer.phar create-project symfony/framework-standard-edition /var/www/symfony/

$ cd /var/www/symfony/

$ php app/check.php (PHP >= 5.3.2, etc)

Page 6: Drupal symfony

Get Symfony using Composer

Page 7: Drupal symfony

Composer — dependency manager for PHP

Page 8: Drupal symfony

Composer.json{

'autoload': {

'psr-0': {}

}

'name': 'My Project'

'description': 'Very cool one'

'require': {

'php': '>=5.3.3',

'symfony/class-loader': '2.2.*',

}

}

Page 9: Drupal symfony
Page 10: Drupal symfony

Composer: run your PHP script

Page 11: Drupal symfony

Time to look what we've got

Page 12: Drupal symfony
Page 13: Drupal symfony

Some demo included

Page 14: Drupal symfony
Page 15: Drupal symfony

Symfony is both:

Full-stack framework

Set of decoupled and standalone components

Page 16: Drupal symfony

What are the Symfony components?Symfony components are:

–Set of independent components

–Each can be used separately

–High quality coded, tested and documented

Symfony components are just bricks but you're completely responsible to glue them

Page 17: Drupal symfony

The list of Symfony2 components

Class loader

Config

Console

CssSelector

Dependency Injection

Form

Event Dispatcher

Http Foundation

Http Kernel

Routing

Security

Yaml

Page 18: Drupal symfony

What is bundle

Bundle — set of files which implement single feature

Generate bundle skeleton

$ php app/console generate:bundle --namespace=BundleFolder/NameBundle --format=annotation

Page 19: Drupal symfony

What is bundle

Templates go there

Database mappers

Request → Response

Page 20: Drupal symfony

Controller: all together

Page 21: Drupal symfony

Controller: route

URL and name of route

Page 22: Drupal symfony

Controller: template

Look for template in:./Resources/views/Account/index.html.twig

Page 23: Drupal symfony

Controller: get data from DB

Fetch Doctrine data manager

Get data from repository

Page 24: Drupal symfony

Twig

Syntax is very simple:

{{ something }} - «Print something»

{% something %} - «Do something»

Page 25: Drupal symfony

Twig

Print var:

{{ var }}

«var» can be even an object. With __toString() method

public function __toString()

{

return $this->name;

}

Page 26: Drupal symfony

Twig

Concise:

<?php echo $var ?> → {{ var }}

<?php echo htmlspecialchars($var, ENT_QUOTES, 'UTF-8' ?>

{{ var |e }}

Page 27: Drupal symfony

Twig: base template example

Page 28: Drupal symfony

Twig: inheritance

Page 29: Drupal symfony

Twig: documentation

Page 30: Drupal symfony

Need smth Twig doesn't provide?Just do it!

Page 31: Drupal symfony

Symfony and inspiration

How to rescue your PHP project with Symfony2 components

http://habrahabr.ru/post/146521/

http://www.slideshare.net/xavierlacot/symfony2-components-to-the-rescue-of-your-php-projects

Page 32: Drupal symfony

Create your own framework on top of Symfony components

Recipe from Fabien Potencier

http://fabien.potencier.org/article/50/create-your-own-framework-on-top-of-the-symfony2-components-part-1

Page 33: Drupal symfony

Symfony2 components: Console

Some examples of usage

$ php app/console doctrine:generate:entities Name/MyBundle/Entity/SomeEntity

- generates getters and setters for SomeEntity

$ php app/console container:debug

list of all available services

Page 34: Drupal symfony

Symfony2 components: Console

Page 35: Drupal symfony

Http Foundation

Request Response

Page 36: Drupal symfony

Http Foundation: Request

Page 37: Drupal symfony

Http Foundation: Response

Page 38: Drupal symfony

Routing component

Page 39: Drupal symfony

Services: Dependency Injection

Page 40: Drupal symfony

Benefits:

Only one instance of Mailer which can be used in entire application

Centralized configuration of Mailer

It can be replaced by another service

Page 41: Drupal symfony

Services container (DIC)

Step 1: Create class

Step 2: Register it as service by adding to the service container

Step 3 ... n: Use it

Page 42: Drupal symfony

Create class

Page 43: Drupal symfony

Register as service

Page 44: Drupal symfony

Let's make example more complicated

Step 1: Create one more class

Step 2: Inject service to it

Step 3: Register it as service as well

Page 45: Drupal symfony

Create class and inject service

Page 46: Drupal symfony

Register new class as service

Page 47: Drupal symfony

Use it

Page 48: Drupal symfony

Let's improve it. Load services from file

Page 49: Drupal symfony

Service.yml become a config file

Page 50: Drupal symfony

Http Kernel

Page 51: Drupal symfony

Request → Response flow

Page 52: Drupal symfony

Events and listeners

Event 1 in HttpKernel->handle(): kernel.request

I.e. Security listener can throw Response object with 403 Access denied response

Page 53: Drupal symfony

Symfony2 is very kind:

For visitors

For developers

Page 54: Drupal symfony

Symfony2 is very fast

Response class to manage HTTP cache:

$response = new Response();

$response->setMaxAge(600);

$date = new DateTime();

$date->modify('+600 seconds');

$response->setExpires($date);

Page 55: Drupal symfony

Symfony2 dev mode

Page 56: Drupal symfony

Symfony2 dev mode

Page 57: Drupal symfony

Symfony2 dev mode

Page 58: Drupal symfony

Symfony2 follows standards...

Autoloading standard: PSR-0

Basic coding style: PSR-1

Coding style: PSR-2

http://www.php-fig.org/

Page 59: Drupal symfony

… which means:

It heavily uses namespaces, OOP, SOA, design patterns, etc

http://php.net/manual/en/language.namespaces.php

http://en.wikipedia.org/wiki/Software_design_pattern

http://en.wikipedia.org/wiki/Service-oriented_architecture

Page 60: Drupal symfony

I'm almost finished

Yellow: development

Blue: stable

Green: maintenance

Since 3 June 2013 – long time supportfor v. 2.3

Page 61: Drupal symfony

Symfony2 bundles

Page 62: Drupal symfony

Try Symfony2

But be careful — it can become addictive from the very first

encounter :)