Drupal symfony

Post on 25-May-2015

172 views 2 download

Tags:

transcript

Symfony is not scary

Valeriy Tuz Inuits

Is it scary?..

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

Symfony is not scary → Why I love Symfony2

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)

Get Symfony using Composer

Composer — dependency manager for PHP

Composer.json{

'autoload': {

'psr-0': {}

}

'name': 'My Project'

'description': 'Very cool one'

'require': {

'php': '>=5.3.3',

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

}

}

Composer: run your PHP script

Time to look what we've got

Some demo included

Symfony is both:

Full-stack framework

Set of decoupled and standalone components

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

The list of Symfony2 components

Class loader

Config

Console

CssSelector

Dependency Injection

Form

Event Dispatcher

Http Foundation

Http Kernel

Routing

Security

Yaml

What is bundle

Bundle — set of files which implement single feature

Generate bundle skeleton

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

What is bundle

Templates go there

Database mappers

Request → Response

Controller: all together

Controller: route

URL and name of route

Controller: template

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

Controller: get data from DB

Fetch Doctrine data manager

Get data from repository

Twig

Syntax is very simple:

{{ something }} - «Print something»

{% something %} - «Do something»

Twig

Print var:

{{ var }}

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

public function __toString()

{

return $this->name;

}

Twig

Concise:

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

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

{{ var |e }}

Twig: base template example

Twig: inheritance

Twig: documentation

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

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

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

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

Symfony2 components: Console

Http Foundation

Request Response

Http Foundation: Request

Http Foundation: Response

Routing component

Services: Dependency Injection

Benefits:

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

Centralized configuration of Mailer

It can be replaced by another service

Services container (DIC)

Step 1: Create class

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

Step 3 ... n: Use it

Create class

Register as service

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

Create class and inject service

Register new class as service

Use it

Let's improve it. Load services from file

Service.yml become a config file

Http Kernel

Request → Response flow

Events and listeners

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

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

Symfony2 is very kind:

For visitors

For developers

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);

Symfony2 dev mode

Symfony2 dev mode

Symfony2 dev mode

Symfony2 follows standards...

Autoloading standard: PSR-0

Basic coding style: PSR-1

Coding style: PSR-2

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

… 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

I'm almost finished

Yellow: development

Blue: stable

Green: maintenance

Since 3 June 2013 – long time supportfor v. 2.3

Symfony2 bundles

Try Symfony2

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

encounter :)

Questions

valery@inuits.eu

valeriy.tuz@gmail.com

+380 67 505 42 76