symfony2 y su ecosistema
DESCRIPTION
Desarrolla tus proyectos PHP utilizando los componentes de Symonfy2 (ClassLoader, HttpFoundation, Routing, Dependecy Injection) y las librerías Doctrine 2 y sistema de plantillas Twig. Como apoyo a las transparencias aquí teneis una pequeña aplicación desarrolloada con dichos componentes y librerías. https://github.com/javiacei/codemotion-sf2TRANSCRIPT
![Page 2: Symfony2 y su ecosistema](https://reader034.vdocuments.net/reader034/viewer/2022042714/554bb377b4c9053a298b45ea/html5/thumbnails/2.jpg)
Francisco Javier Aceituno Lapido
• Ingeniero en Informática (UPM) 2010
• Colaborador en symfony-madrid
• Arquitecto de Software en ideup!
• Amante del mundo PHP y Symfony desde 2008
2 Symfony 2 y su ecosistema
![Page 3: Symfony2 y su ecosistema](https://reader034.vdocuments.net/reader034/viewer/2022042714/554bb377b4c9053a298b45ea/html5/thumbnails/3.jpg)
¿ Qué es Symfony 2?
3
8 Bundles
Symfony 2 y su ecosistema
![Page 4: Symfony2 y su ecosistema](https://reader034.vdocuments.net/reader034/viewer/2022042714/554bb377b4c9053a298b45ea/html5/thumbnails/4.jpg)
¿ Qué es Symfony 2?
4
ClassLoader HttpFoundation Routing EventDispatcher
DependencyInjection Console Config
22 componentes
Symfony 2 y su ecosistema
![Page 5: Symfony2 y su ecosistema](https://reader034.vdocuments.net/reader034/viewer/2022042714/554bb377b4c9053a298b45ea/html5/thumbnails/5.jpg)
¿ Qué es Symfony 2?
5
ClassLoader HttpFoundation Routing EventDispatcher
DependencyInjection Console Config
22 componentes
Twig Doctrine Monolog Assetic SwiftMailer
5 Librerías externas
Symfony 2 y su ecosistema
![Page 6: Symfony2 y su ecosistema](https://reader034.vdocuments.net/reader034/viewer/2022042714/554bb377b4c9053a298b45ea/html5/thumbnails/6.jpg)
¿ Qué es Symfony 2?
6
ClassLoader HttpFoundation Routing EventDispatcher
DependencyInjection Console Config
22 componentes
Twig Doctrine Monolog Assetic SwiftMailer
WebProfilerBundle
5 Librerías externas 8 Bundles
TwigBundle
SecurityBundle
FrameworkBundle
Symfony 2 y su ecosistema
![Page 7: Symfony2 y su ecosistema](https://reader034.vdocuments.net/reader034/viewer/2022042714/554bb377b4c9053a298b45ea/html5/thumbnails/7.jpg)
¿ Qué es Symfony 2?
7
ClassLoader HttpFoundation Routing EventDispatcher
DependencyInjection Console Config
22 componentes
Twig Doctrine Monolog Assetic SwiftMailer
WebProfilerBundle
5 Librerías externas 8 Bundles
TwigBundle
SecurityBundle
FrameworkBundle
Symfony 2 y su ecosistema
![Page 8: Symfony2 y su ecosistema](https://reader034.vdocuments.net/reader034/viewer/2022042714/554bb377b4c9053a298b45ea/html5/thumbnails/8.jpg)
ClassLoader
https://github.com/symfony/ClassLoader
Carga automáticamente las clases de tu proyecto si
– Definición de las clases siguen el estadar PSR-0 (PHP 5.3)
Codemotion/Model/Task.php Codemotion\Model\Task
– Definición de las clases según el convenio PEAR
Codemotion/Model/Task.php Codemotion_Model_Task
8 Symfony 2 y su ecosistema
![Page 9: Symfony2 y su ecosistema](https://reader034.vdocuments.net/reader034/viewer/2022042714/554bb377b4c9053a298b45ea/html5/thumbnails/9.jpg)
HttpFoundation
https://github.com/symfony/HttpFoundation
Define una capa orientada a objetos de la especificación de HTTP
9 Symfony 2 y su ecosistema
![Page 10: Symfony2 y su ecosistema](https://reader034.vdocuments.net/reader034/viewer/2022042714/554bb377b4c9053a298b45ea/html5/thumbnails/10.jpg)
HttpFoundation
https://github.com/symfony/HttpFoundation
Define una capa orientada a objetos de la especificación de HTTP
10
Request query
request cookies
attributes files
server headers
Symfony 2 y su ecosistema
![Page 11: Symfony2 y su ecosistema](https://reader034.vdocuments.net/reader034/viewer/2022042714/554bb377b4c9053a298b45ea/html5/thumbnails/11.jpg)
HttpFoundation
https://github.com/symfony/HttpFoundation
Define una capa orientada a objetos de la especificación de HTTP
11
Request query
request cookies
attributes files
server headers
$_GET
$_GET $_POST $_COOKIE
$_FILES
$_SERVER
Symfony 2 y su ecosistema
![Page 12: Symfony2 y su ecosistema](https://reader034.vdocuments.net/reader034/viewer/2022042714/554bb377b4c9053a298b45ea/html5/thumbnails/12.jpg)
HttpFoundation
https://github.com/symfony/HttpFoundation
Define una capa orientada a objetos de la especificación de HTTP
12
Request query
request cookies
attributes files
server headers
$_GET
$_GET $_POST $_COOKIE
$_FILES
$_SERVER
all() keys()
replace() add() get() set() has()
remove()
Symfony 2 y su ecosistema
![Page 13: Symfony2 y su ecosistema](https://reader034.vdocuments.net/reader034/viewer/2022042714/554bb377b4c9053a298b45ea/html5/thumbnails/13.jpg)
HttpFoundation
https://github.com/symfony/HttpFoundation
Define una capa orientada a objetos de la especificación de HTTP
13
Request query
request cookies
attributes files
server headers
$_GET
$_GET $_POST $_COOKIE
$_FILES
$_SERVER
all() keys()
replace() add() get() set() has()
remove()
Response
content status
headers
Symfony 2 y su ecosistema
![Page 14: Symfony2 y su ecosistema](https://reader034.vdocuments.net/reader034/viewer/2022042714/554bb377b4c9053a298b45ea/html5/thumbnails/14.jpg)
HttpFoundation
https://github.com/symfony/HttpFoundation
Define una capa orientada a objetos de la especificación de HTTP
14
Request query
request cookies
attributes files
server headers
$_GET
$_GET $_POST $_COOKIE
$_FILES
$_SERVER
all() keys()
replace() add() get() set() has()
remove()
Response
content status
headers
$_GET
send() prepare()
Symfony 2 y su ecosistema
![Page 15: Symfony2 y su ecosistema](https://reader034.vdocuments.net/reader034/viewer/2022042714/554bb377b4c9053a298b45ea/html5/thumbnails/15.jpg)
HttpFoundation
https://github.com/symfony/HttpFoundation
Define una capa orientada a objetos de la especificación de HTTP
15
Request query
request cookies
attributes files
server headers
$_GET
$_GET $_POST $_COOKIE
$_FILES
$_SERVER
all() keys()
replace() add() get() set() has()
remove()
Response
content status
headers
$_GET
send() prepare()
cache
setPublic() setPrivate()
expire) setExpires() setMaxAge()
setLastModified() …
Symfony 2 y su ecosistema
![Page 16: Symfony2 y su ecosistema](https://reader034.vdocuments.net/reader034/viewer/2022042714/554bb377b4c9053a298b45ea/html5/thumbnails/16.jpg)
Routing
https://github.com/symfony/Routing
Mapea una petición HTTP (Request) a un conjunto de variables
16 Symfony 2 y su ecosistema
![Page 17: Symfony2 y su ecosistema](https://reader034.vdocuments.net/reader034/viewer/2022042714/554bb377b4c9053a298b45ea/html5/thumbnails/17.jpg)
Routing
https://github.com/symfony/Routing
Mapea una petición HTTP (Request) a un conjunto de variables
17
Router
app.php/task/list
configuración
Symfony 2 y su ecosistema
![Page 18: Symfony2 y su ecosistema](https://reader034.vdocuments.net/reader034/viewer/2022042714/554bb377b4c9053a298b45ea/html5/thumbnails/18.jpg)
Routing
https://github.com/symfony/Routing
Mapea una petición HTTP (Request) a un conjunto de variables
18
Router
app.php/task/list
configuración
task/list
Symfony 2 y su ecosistema
![Page 19: Symfony2 y su ecosistema](https://reader034.vdocuments.net/reader034/viewer/2022042714/554bb377b4c9053a298b45ea/html5/thumbnails/19.jpg)
Routing
https://github.com/symfony/Routing
Mapea una petición HTTP (Request) a un conjunto de variables
19
Router
app.php/task/list
configuración
task/list controller: TaskController, action: listAction
Symfony 2 y su ecosistema
![Page 20: Symfony2 y su ecosistema](https://reader034.vdocuments.net/reader034/viewer/2022042714/554bb377b4c9053a298b45ea/html5/thumbnails/20.jpg)
Routing
https://github.com/symfony/Routing
Mapea una petición HTTP (Request) a un conjunto de variables
20
Router
app.php/task/list
app.php/task/1/show
configuración
task/list controller: TaskController, action: listAction
Symfony 2 y su ecosistema
![Page 21: Symfony2 y su ecosistema](https://reader034.vdocuments.net/reader034/viewer/2022042714/554bb377b4c9053a298b45ea/html5/thumbnails/21.jpg)
Routing
https://github.com/symfony/Routing
Mapea una petición HTTP (Request) a un conjunto de variables
21
Router
app.php/task/list
app.php/task/1/show
configuración
task/list
task/{id}/show
controller: TaskController, action: listAction
Symfony 2 y su ecosistema
![Page 22: Symfony2 y su ecosistema](https://reader034.vdocuments.net/reader034/viewer/2022042714/554bb377b4c9053a298b45ea/html5/thumbnails/22.jpg)
Routing
https://github.com/symfony/Routing
Mapea una petición HTTP (Request) a un conjunto de variables
22
Router
app.php/task/list
app.php/task/1/show
task/list
task/{id}/show
controller: TaskController, action: listAction
controller: TaskController, action: showAction, id: 1
configuración
Symfony 2 y su ecosistema
![Page 23: Symfony2 y su ecosistema](https://reader034.vdocuments.net/reader034/viewer/2022042714/554bb377b4c9053a298b45ea/html5/thumbnails/23.jpg)
Doctrine 2
Conjunto de librerías PHP que nos proporciona servicios de persistencia
23 Symfony 2 y su ecosistema
![Page 24: Symfony2 y su ecosistema](https://reader034.vdocuments.net/reader034/viewer/2022042714/554bb377b4c9053a298b45ea/html5/thumbnails/24.jpg)
Doctrine 2
https://github.com/doctrine/doctrine2
24
Doctrine Common
Symfony 2 y su ecosistema
- Extensiones al core de PHP (Colecciones, Anotaciones, Cache, ...)
![Page 25: Symfony2 y su ecosistema](https://reader034.vdocuments.net/reader034/viewer/2022042714/554bb377b4c9053a298b45ea/html5/thumbnails/25.jpg)
Doctrine 2
https://github.com/doctrine/doctrine2
25
Doctrine Common
Doctrine DBAL - Manipulación de la estructura de bbdd. - Abstracción del tipo de bbdd (IMBDB2, PDOIbm, PDOMySQL, PDOOracle, PDOSqlite, …).
Symfony 2 y su ecosistema
- Extensiones al core de PHP (Colecciones, Anotaciones, Cache, ...)
![Page 26: Symfony2 y su ecosistema](https://reader034.vdocuments.net/reader034/viewer/2022042714/554bb377b4c9053a298b45ea/html5/thumbnails/26.jpg)
Doctrine 2
https://github.com/doctrine/doctrine2
26
Doctrine Common
Doctrine DBAL
Doctrine ORM
- Extensiones al core de PHP (Colecciones, Anotaciones, Cache, ...)
- Manipulación de la estructura de bbdd. - Abstracción del tipo de bbdd (IMBDB2, PDOIbm, PDOMySQL, PDOOracle, PDOSqlite, …).
- Ofrece persistencia transparente a los objetos de PHP. - Doctrine Query Language (DQL) nos abstrae el mapeo entre las columnas y tablas de la bbdd y los objetos.
Symfony 2 y su ecosistema
![Page 27: Symfony2 y su ecosistema](https://reader034.vdocuments.net/reader034/viewer/2022042714/554bb377b4c9053a298b45ea/html5/thumbnails/27.jpg)
Doctrine 2 ORM
Active Record vs DataMapper
27
Tarea
nombre
descripción
elementos
save()
delete()
update()
Symfony 2 y su ecosistema
![Page 28: Symfony2 y su ecosistema](https://reader034.vdocuments.net/reader034/viewer/2022042714/554bb377b4c9053a298b45ea/html5/thumbnails/28.jpg)
Doctrine 2 ORM
Active Record vs DataMapper
28
Tarea
nombre
descripción
elementos
save()
delete()
update()
$tarea = new Tarea(); $tarea->setNombre(..) $tarea->save() $tarea->delete()
Symfony 2 y su ecosistema
![Page 29: Symfony2 y su ecosistema](https://reader034.vdocuments.net/reader034/viewer/2022042714/554bb377b4c9053a298b45ea/html5/thumbnails/29.jpg)
Doctrine 2 ORM
Active Record vs DataMapper
29
Tarea
nombre
descripción
elementos
save()
delete()
update()
$tarea = new Tarea(); $tarea->setNombre(..) $tarea->save() $tarea->delete()
Tarea
nombre
descripción
elementos
Symfony 2 y su ecosistema
![Page 30: Symfony2 y su ecosistema](https://reader034.vdocuments.net/reader034/viewer/2022042714/554bb377b4c9053a298b45ea/html5/thumbnails/30.jpg)
Doctrine 2 ORM
Active Record vs DataMapper
30
Tarea
nombre
descripción
elementos
save()
delete()
update()
$tarea = new Tarea(); $tarea->setNombre(..) $tarea->save() $tarea->delete()
Tarea
nombre
descripción
elementos
$tarea = new Tarea(); $tarea->setNombre(...) $em->persist($tarea) $em->remove($tarea) $em->flush()
Symfony 2 y su ecosistema
![Page 31: Symfony2 y su ecosistema](https://reader034.vdocuments.net/reader034/viewer/2022042714/554bb377b4c9053a298b45ea/html5/thumbnails/31.jpg)
Doctrine 2 ORM
Entity Manager
31
Es el punto de acceso a las funcionalidad del ORM y el el encargado de persistir las entidades.
persist ($entity) remove ($entity)
flush()
Symfony 2 y su ecosistema
![Page 32: Symfony2 y su ecosistema](https://reader034.vdocuments.net/reader034/viewer/2022042714/554bb377b4c9053a298b45ea/html5/thumbnails/32.jpg)
Doctrine 2 ORM
Entity Respository
32
Colección de entidades de un mismo tipo. Es el encargado de separar la lógica de Doctrine de tu modelo.
findAll () findBy (array)
findOneBy(array) …
Symfony 2 y su ecosistema
![Page 33: Symfony2 y su ecosistema](https://reader034.vdocuments.net/reader034/viewer/2022042714/554bb377b4c9053a298b45ea/html5/thumbnails/33.jpg)
Doctrine 2 ORM
DQL
33
Lenguaje de búsquedas para tu modelo de objetos. SELECT t FROM Codemotion\Model\Task t WHERE t.horas > 2
Symfony 2 y su ecosistema
![Page 34: Symfony2 y su ecosistema](https://reader034.vdocuments.net/reader034/viewer/2022042714/554bb377b4c9053a298b45ea/html5/thumbnails/34.jpg)
Doctrine 2 ORM
DQL
34
Lenguaje de búsquedas para tu modelo de objetos. SELECT t FROM Codemotion\Model\Task t WHERE t.horas > 2
Query Builder
$qb->add('select', ‘t') -> add('from', Codemotion\Model\Task t') ->
add('where', ‘t.horas > 2');
Symfony 2 y su ecosistema
![Page 35: Symfony2 y su ecosistema](https://reader034.vdocuments.net/reader034/viewer/2022042714/554bb377b4c9053a298b45ea/html5/thumbnails/35.jpg)
Doctrine 2 ORM
Comandos
35
Doctrine ORM viene con un conjunto de herramientas que nos simplifica enormemente el trabajo con la bbdd.
orm:validate-schema orm:schema-tool:update | create | drop
orm:generate-entities …
Symfony 2 y su ecosistema
![Page 36: Symfony2 y su ecosistema](https://reader034.vdocuments.net/reader034/viewer/2022042714/554bb377b4c9053a298b45ea/html5/thumbnails/36.jpg)
Doctrine 2 ORM
Documentación
36
http://www.doctrine-project.org/
Symfony 2 y su ecosistema
![Page 37: Symfony2 y su ecosistema](https://reader034.vdocuments.net/reader034/viewer/2022042714/554bb377b4c9053a298b45ea/html5/thumbnails/37.jpg)
Twig
https://github.com/fabpot/Twig
Twig es un sistema de plantillas para PHP muy conciso, flexible y con una sintaxis orientada a plantillas
http://twig.sensiolabs.org/
37 Symfony 2 y su ecosistema
![Page 38: Symfony2 y su ecosistema](https://reader034.vdocuments.net/reader034/viewer/2022042714/554bb377b4c9053a298b45ea/html5/thumbnails/38.jpg)
Twig
CONCISO
38
<?php echo $tarea->getNombre() ?>
<?php echo $hoy->format(‘Y-m-d’); ?>
Symfony 2 y su ecosistema
![Page 39: Symfony2 y su ecosistema](https://reader034.vdocuments.net/reader034/viewer/2022042714/554bb377b4c9053a298b45ea/html5/thumbnails/39.jpg)
Twig
CONCISO
39
<?php echo $tarea->getNombre() ?> {{ tarea.nombre }}
<?php echo $hoy->format(‘Y-m-d’); ?> {{ hoy|date(‘Y-m-d’) }}
Symfony 2 y su ecosistema
![Page 40: Symfony2 y su ecosistema](https://reader034.vdocuments.net/reader034/viewer/2022042714/554bb377b4c9053a298b45ea/html5/thumbnails/40.jpg)
Twig
40
SINTAXIS ORIENTADA A PLANTILLAS
<?php if ($tareas): ?>
<?php foreach ($tareas as $tarea): ?>
* <?php echo $tarea->getNombre(); ?>
<?php endforeach; ?>
<?php else: ?>
No hay tareas disponibles
<?php endif; ?>
Symfony 2 y su ecosistema
![Page 41: Symfony2 y su ecosistema](https://reader034.vdocuments.net/reader034/viewer/2022042714/554bb377b4c9053a298b45ea/html5/thumbnails/41.jpg)
Twig
41
SINTAXIS ORIENTADA A PLANTILLAS
<?php if ($tareas): ?>
<?php foreach ($tareas as $tarea): ?>
* <?php echo $tarea->getNombre(); ?>
<?php endforeach; ?>
<?php else: ?>
No hay tareas disponibles
<?php endif; ?>
{% for tarea in tareas %}
* {{ tarea.nombre }}
{% else %}
No hay tareas disponibles
{% endfor %}
Symfony 2 y su ecosistema
![Page 42: Symfony2 y su ecosistema](https://reader034.vdocuments.net/reader034/viewer/2022042714/554bb377b4c9053a298b45ea/html5/thumbnails/42.jpg)
Twig
42
Flexible
Permite a los desarrolladores definirse cualquier tag, filtro, función, …
http://twig.sensiolabs.org/doc/api.html http://twig.sensiolabs.org/doc/extensions.html
Symfony 2 y su ecosistema
![Page 43: Symfony2 y su ecosistema](https://reader034.vdocuments.net/reader034/viewer/2022042714/554bb377b4c9053a298b45ea/html5/thumbnails/43.jpg)
Dependency Injection
43
https://github.com/symfony/DependencyInjection
Estandariza y centraliza la manera en la que se crean los objetos de tu aplicación
Symfony 2 y su ecosistema
![Page 44: Symfony2 y su ecosistema](https://reader034.vdocuments.net/reader034/viewer/2022042714/554bb377b4c9053a298b45ea/html5/thumbnails/44.jpg)
Dependency Injection
44
Pero, …¿ Qué es una inyección de dependencias?
Symfony 2 y su ecosistema
![Page 45: Symfony2 y su ecosistema](https://reader034.vdocuments.net/reader034/viewer/2022042714/554bb377b4c9053a298b45ea/html5/thumbnails/45.jpg)
Dependency Injection
45
Pero, …¿ Qué es una inyección de dependencias?
$gestorTareas = new GestorTareas($em);
Symfony 2 y su ecosistema
![Page 46: Symfony2 y su ecosistema](https://reader034.vdocuments.net/reader034/viewer/2022042714/554bb377b4c9053a298b45ea/html5/thumbnails/46.jpg)
Dependency Injection
46
Pero, …¿ Qué es una inyección de dependencias?
$gestorTareas = new GestorTareas($em);
$gestorTareas = new GestorTareas(); $gestorTareas->setEntityManager($em);
Symfony 2 y su ecosistema
![Page 47: Symfony2 y su ecosistema](https://reader034.vdocuments.net/reader034/viewer/2022042714/554bb377b4c9053a298b45ea/html5/thumbnails/47.jpg)
Dependency Injection Container (DIC)
47
DIC
Reglas de creación
de objetos
Symfony 2 y su ecosistema
![Page 48: Symfony2 y su ecosistema](https://reader034.vdocuments.net/reader034/viewer/2022042714/554bb377b4c9053a298b45ea/html5/thumbnails/48.jpg)
Dependency Injection Container (DIC)
48
DIC
Reglas de creación
de objetos
get(‘gestor_tareas’)
Symfony 2 y su ecosistema
![Page 49: Symfony2 y su ecosistema](https://reader034.vdocuments.net/reader034/viewer/2022042714/554bb377b4c9053a298b45ea/html5/thumbnails/49.jpg)
Dependency Injection Container (DIC)
49
DIC
Reglas de creación
de objetos
get(‘gestor_tareas’) “gestor_tareas” depende de
“entity_manager”
Symfony 2 y su ecosistema
![Page 50: Symfony2 y su ecosistema](https://reader034.vdocuments.net/reader034/viewer/2022042714/554bb377b4c9053a298b45ea/html5/thumbnails/50.jpg)
Dependency Injection Container (DIC)
50
DIC
Reglas de creación
de objetos
get(‘gestor_tareas’)
$em = $container->get(‘entity_manager’); $gestorTareas = new GestorTareas($em) return $gestorTareas;
“gestor_tareas” depende de “entity_manager”
Symfony 2 y su ecosistema
![Page 51: Symfony2 y su ecosistema](https://reader034.vdocuments.net/reader034/viewer/2022042714/554bb377b4c9053a298b45ea/html5/thumbnails/51.jpg)
Dependency Injection Container (DIC)
51
DIC
Reglas de creación
de objetos
$gestorTareas get(‘gestor_tareas’)
$em = $container->get(‘entity_manager’); $gestorTareas = new GestorTareas($em) return $gestorTareas;
“gestor_tareas” depende de “entity_manager”
Symfony 2 y su ecosistema
![Page 52: Symfony2 y su ecosistema](https://reader034.vdocuments.net/reader034/viewer/2022042714/554bb377b4c9053a298b45ea/html5/thumbnails/52.jpg)
http://github.com/javiacei/codemotion-sf2
52 Symfony 2 y su ecosistema
![Page 53: Symfony2 y su ecosistema](https://reader034.vdocuments.net/reader034/viewer/2022042714/554bb377b4c9053a298b45ea/html5/thumbnails/53.jpg)
Gracias.
53 Symfony 2 y su ecosistema
[email protected] twitter.com/javiacei
http://es.linkedin.com/pub/francisco-javier-aceituno-lapido/32/313/94b