Lets play with Symfony2

23
Let’s play with Symfony2 Noël GUILBERT

Transcript of Lets play with Symfony2

Page 1: Lets play with Symfony2

Let’s play with Symfony2Noël GUILBERT

Page 2: Lets play with Symfony2

Symfony2 introduced a lot a new features, let’s discover some of them:

• Dependency Injection Container• The form framework• Doctrine2 integration

Page 3: Lets play with Symfony2

Dependency Injection Container

• Dependency Injection Container–a way to organize dependencies–Define services

services:zend.translator:class: Userarguments: [@user.storage]

user.storage:class: SessionStorage

Page 4: Lets play with Symfony2

• How to use it in a Symfony2 project ?

Page 5: Lets play with Symfony2

• Create your own service–Symfony2 offer a simple and effective way to create

services

– It’s called an extension

–An extension can receive some configuration parameter from the application config.yml file (i.e. databases connections, passwords, etc.)

Page 6: Lets play with Symfony2

• Create your own service–Define your services:

# src/Bundle/TwitterBundle/Resources/config/user.ymlparameters:

twitter.username: ~twitter.password: ~

services:twitter:class: Twitterarguments: [%twitter.username%, %twitter.password%]

Page 7: Lets play with Symfony2

# src/Bundle/TwitterBundle/DependencyInjectionContainer/TwitterExtension.php

• Create the extension class

Page 8: Lets play with Symfony2

# src/Bundle/TwitterBundle/DependencyInjectionContainer/TwitterExtension.php

• Load your service

Page 9: Lets play with Symfony2

• Enable the extension–Add these lines in the application config.yml file:

# hello/config/config.ymltwitter.twitter:username: noelguilbertpassword: mysecretpassword

Page 10: Lets play with Symfony2

• That’s all, the service is now available:

Page 11: Lets play with Symfony2

The Form Framework

• A brand new piece of code• A layer on top of the domain models• Allows you to easily create HTML forms

Page 12: Lets play with Symfony2

Your first form

Page 13: Lets play with Symfony2

Write a simple Request class

Page 14: Lets play with Symfony2

Let’s add some validators

Page 15: Lets play with Symfony2

And you are ready to use it!

Page 16: Lets play with Symfony2

Doctrine 2 integration

• Fully integrated:– DBAL– ORM– MongoDb mapper– Migrations

Page 17: Lets play with Symfony2

DBAL: configure your database connection

Page 18: Lets play with Symfony2

DBAL command lines

• Create configured database$ php console doctrine:database:create

• Drop configured database$ php console doctrine:database:drop

• Query the database$ php console doctrine:sql:query ‘SELECT * FROM User’

Page 19: Lets play with Symfony2

ORM: persisting and retrieving entities

• An Entity is a regularPHP class that has been mapped to Doctrine 2

Page 20: Lets play with Symfony2

ORM: Defining entities

• Entities can be mapped with:

• Regular PHP class• XML Definition schemas• YML Definition schemas

Page 21: Lets play with Symfony2

ORM: command lines• Convert Doctrine1 schemas to Doctrine 2:

$ php console doctrine:mapping:convert-d1-schema

• Generate entities:$ php console doctrine:generate:entities

• Create or update the database schema$ php console doctrine:schema:create$ php console doctrine:schema:update

• Load fixtures$ php console doctrine:schema:update

Page 22: Lets play with Symfony2

What’s next ?

Page 23: Lets play with Symfony2

Questions?Noël GUILBERT

[email protected]

Sensio S.A.92-98, boulevard Victor Hugo

92 115 Clichy CedexFRANCE

Tél. : +33 1 40 99 80 80

www.sensiolabs.com - www.symfony-project.org - www.noelguilbert.com