![Page 1: Silex, desarrollo web ágil y profesional con PHP](https://reader034.vdocuments.co/reader034/viewer/2022051110/54b7b95d4a7959181f8b46bc/html5/thumbnails/1.jpg)
JAVIER EGUILUZBILBOSTACK26 ENERO 2013
Silexdesarrollo web ágil y profesional con PHP
![Page 2: Silex, desarrollo web ágil y profesional con PHP](https://reader034.vdocuments.co/reader034/viewer/2022051110/54b7b95d4a7959181f8b46bc/html5/thumbnails/2.jpg)
Gracias a la organización.
Asier Fran Ibon Vicenç
![Page 3: Silex, desarrollo web ágil y profesional con PHP](https://reader034.vdocuments.co/reader034/viewer/2022051110/54b7b95d4a7959181f8b46bc/html5/thumbnails/3.jpg)
Agenda1. Introducción
2. Programando aplicaciones
3. DEMO
4. Buenas prácticas
5. Puntos débiles
![Page 4: Silex, desarrollo web ágil y profesional con PHP](https://reader034.vdocuments.co/reader034/viewer/2022051110/54b7b95d4a7959181f8b46bc/html5/thumbnails/4.jpg)
Introducción
![Page 5: Silex, desarrollo web ágil y profesional con PHP](https://reader034.vdocuments.co/reader034/viewer/2022051110/54b7b95d4a7959181f8b46bc/html5/thumbnails/5.jpg)
¿Por qué?
![Page 6: Silex, desarrollo web ágil y profesional con PHP](https://reader034.vdocuments.co/reader034/viewer/2022051110/54b7b95d4a7959181f8b46bc/html5/thumbnails/6.jpg)
En el mundo de los negocios...
1.Rápido
2.Barato
3.Bueno
![Page 7: Silex, desarrollo web ágil y profesional con PHP](https://reader034.vdocuments.co/reader034/viewer/2022051110/54b7b95d4a7959181f8b46bc/html5/thumbnails/7.jpg)
En el mundo de los negocios...
1.Rápido
2.Barato
3.Bueno
ELIGE DOS
![Page 8: Silex, desarrollo web ágil y profesional con PHP](https://reader034.vdocuments.co/reader034/viewer/2022051110/54b7b95d4a7959181f8b46bc/html5/thumbnails/8.jpg)
En el mundo de la programación...
1.Bien hecho
2.Terminado a tiempo
3.Barato
![Page 9: Silex, desarrollo web ágil y profesional con PHP](https://reader034.vdocuments.co/reader034/viewer/2022051110/54b7b95d4a7959181f8b46bc/html5/thumbnails/9.jpg)
En el mundo de la programación...
1.Bien hecho
2.Terminado a tiempo
3.Barato
ELIGE UNA
![Page 10: Silex, desarrollo web ágil y profesional con PHP](https://reader034.vdocuments.co/reader034/viewer/2022051110/54b7b95d4a7959181f8b46bc/html5/thumbnails/10.jpg)
En el mundo de la programación...
1.Bien hecho
2.Terminado a tiempo
3.Barato✔
✔
✔
Con Silex puedes tenerlo todo
![Page 11: Silex, desarrollo web ágil y profesional con PHP](https://reader034.vdocuments.co/reader034/viewer/2022051110/54b7b95d4a7959181f8b46bc/html5/thumbnails/11.jpg)
¡quiero hacerlo bien!¡no tengo
tiempo!
![Page 12: Silex, desarrollo web ágil y profesional con PHP](https://reader034.vdocuments.co/reader034/viewer/2022051110/54b7b95d4a7959181f8b46bc/html5/thumbnails/12.jpg)
¿Qué esSilex?
![Page 13: Silex, desarrollo web ágil y profesional con PHP](https://reader034.vdocuments.co/reader034/viewer/2022051110/54b7b95d4a7959181f8b46bc/html5/thumbnails/13.jpg)
Foto: Wikipedia
![Page 14: Silex, desarrollo web ágil y profesional con PHP](https://reader034.vdocuments.co/reader034/viewer/2022051110/54b7b95d4a7959181f8b46bc/html5/thumbnails/14.jpg)
Foto: WikipediaSojourner (2007)
Curiosity (2012)
![Page 15: Silex, desarrollo web ágil y profesional con PHP](https://reader034.vdocuments.co/reader034/viewer/2022051110/54b7b95d4a7959181f8b46bc/html5/thumbnails/15.jpg)
Sojourner (2007)
Curiosity(2012)
Masa 11 kg 900 kg
Coste ($) 150 M 1.800 M
Memoria RAM 64 KB 256 MB
CPU 2 MHz 200 MHz
![Page 16: Silex, desarrollo web ágil y profesional con PHP](https://reader034.vdocuments.co/reader034/viewer/2022051110/54b7b95d4a7959181f8b46bc/html5/thumbnails/16.jpg)
Es importante tener en cuenta que...
• El grande jamás podrá ser tan ágil como el pequeño.
• El pequeño jamás podrá competir en potencia y funcionalidad con el grande.
![Page 17: Silex, desarrollo web ágil y profesional con PHP](https://reader034.vdocuments.co/reader034/viewer/2022051110/54b7b95d4a7959181f8b46bc/html5/thumbnails/17.jpg)
SilexThe PHP micro-frameworkbased on the Symfony2 Components
el logo de Silex
![Page 18: Silex, desarrollo web ágil y profesional con PHP](https://reader034.vdocuments.co/reader034/viewer/2022051110/54b7b95d4a7959181f8b46bc/html5/thumbnails/18.jpg)
los dos son frameworks y usan los mismos componentes, pero...
![Page 19: Silex, desarrollo web ágil y profesional con PHP](https://reader034.vdocuments.co/reader034/viewer/2022051110/54b7b95d4a7959181f8b46bc/html5/thumbnails/19.jpg)
...Symfony es un framework muy grande y Silex es un framework muy pequeño
![Page 20: Silex, desarrollo web ágil y profesional con PHP](https://reader034.vdocuments.co/reader034/viewer/2022051110/54b7b95d4a7959181f8b46bc/html5/thumbnails/20.jpg)
Silex Symfony2
Peso 5 MB 8 MB
Archivos 3.366 6.578
Nivel dificultad
Flexibilidad
![Page 21: Silex, desarrollo web ágil y profesional con PHP](https://reader034.vdocuments.co/reader034/viewer/2022051110/54b7b95d4a7959181f8b46bc/html5/thumbnails/21.jpg)
Los creadores de Silex
Fabien Potencier@fabpot
Igor Wiedler@igorw
![Page 22: Silex, desarrollo web ágil y profesional con PHP](https://reader034.vdocuments.co/reader034/viewer/2022051110/54b7b95d4a7959181f8b46bc/html5/thumbnails/22.jpg)
+ fácil- calidad
+ calidad- facil
![Page 23: Silex, desarrollo web ágil y profesional con PHP](https://reader034.vdocuments.co/reader034/viewer/2022051110/54b7b95d4a7959181f8b46bc/html5/thumbnails/23.jpg)
+ fácil- calidad
+ calidad- facil
![Page 24: Silex, desarrollo web ágil y profesional con PHP](https://reader034.vdocuments.co/reader034/viewer/2022051110/54b7b95d4a7959181f8b46bc/html5/thumbnails/24.jpg)
Programandoaplicaciones
![Page 25: Silex, desarrollo web ágil y profesional con PHP](https://reader034.vdocuments.co/reader034/viewer/2022051110/54b7b95d4a7959181f8b46bc/html5/thumbnails/25.jpg)
Funciones anónimasy closures
![Page 26: Silex, desarrollo web ágil y profesional con PHP](https://reader034.vdocuments.co/reader034/viewer/2022051110/54b7b95d4a7959181f8b46bc/html5/thumbnails/26.jpg)
function suma($a, $b) { return $a + $b;}
Función normal
![Page 27: Silex, desarrollo web ágil y profesional con PHP](https://reader034.vdocuments.co/reader034/viewer/2022051110/54b7b95d4a7959181f8b46bc/html5/thumbnails/27.jpg)
function ($a, $b) { return $a + $b;}
Función anónima
![Page 28: Silex, desarrollo web ágil y profesional con PHP](https://reader034.vdocuments.co/reader034/viewer/2022051110/54b7b95d4a7959181f8b46bc/html5/thumbnails/28.jpg)
$app->get('...', function ($a, $b) { return $a + $b;});
Usando una función anónima
![Page 29: Silex, desarrollo web ágil y profesional con PHP](https://reader034.vdocuments.co/reader034/viewer/2022051110/54b7b95d4a7959181f8b46bc/html5/thumbnails/29.jpg)
$a = 3;function ($b) { return $a + $b;}
Closure
![Page 30: Silex, desarrollo web ágil y profesional con PHP](https://reader034.vdocuments.co/reader034/viewer/2022051110/54b7b95d4a7959181f8b46bc/html5/thumbnails/30.jpg)
$a = 3;function ($b) { return $a + $b;}
Closure
NO FUNCIONA
![Page 31: Silex, desarrollo web ágil y profesional con PHP](https://reader034.vdocuments.co/reader034/viewer/2022051110/54b7b95d4a7959181f8b46bc/html5/thumbnails/31.jpg)
$a = 3;function ($b) use ($a) { return $a + $b;}
Closure
![Page 32: Silex, desarrollo web ágil y profesional con PHP](https://reader034.vdocuments.co/reader034/viewer/2022051110/54b7b95d4a7959181f8b46bc/html5/thumbnails/32.jpg)
$app->get('...', function ($var) use ($app) { // ...});
El código más común de Silex
![Page 33: Silex, desarrollo web ágil y profesional con PHP](https://reader034.vdocuments.co/reader034/viewer/2022051110/54b7b95d4a7959181f8b46bc/html5/thumbnails/33.jpg)
El primerejemplo
![Page 34: Silex, desarrollo web ágil y profesional con PHP](https://reader034.vdocuments.co/reader034/viewer/2022051110/54b7b95d4a7959181f8b46bc/html5/thumbnails/34.jpg)
Holamundo.
![Page 35: Silex, desarrollo web ágil y profesional con PHP](https://reader034.vdocuments.co/reader034/viewer/2022051110/54b7b95d4a7959181f8b46bc/html5/thumbnails/35.jpg)
http://bilbostack.com/hola
![Page 36: Silex, desarrollo web ágil y profesional con PHP](https://reader034.vdocuments.co/reader034/viewer/2022051110/54b7b95d4a7959181f8b46bc/html5/thumbnails/36.jpg)
require_once __DIR__.'/../vendor/autoload.php';
$app = new Silex\Application();
$app->get('/hola', function() { return 'Hola mundo';});
$app->run();
Hola Mundo en Silex
![Page 37: Silex, desarrollo web ágil y profesional con PHP](https://reader034.vdocuments.co/reader034/viewer/2022051110/54b7b95d4a7959181f8b46bc/html5/thumbnails/37.jpg)
require_once __DIR__.'/../vendor/autoload.php';
$app = new Silex\Application();
$app->get('/hola', function() { return 'Hola mundo';});
$app->run();
Cargar clases automáticamente
![Page 38: Silex, desarrollo web ágil y profesional con PHP](https://reader034.vdocuments.co/reader034/viewer/2022051110/54b7b95d4a7959181f8b46bc/html5/thumbnails/38.jpg)
require_once __DIR__.'/../vendor/autoload.php';
$app = new Silex\Application();
$app->get('/hola', function() { return 'Hola mundo';});
$app->run();
Crear la aplicación Silex
![Page 39: Silex, desarrollo web ágil y profesional con PHP](https://reader034.vdocuments.co/reader034/viewer/2022051110/54b7b95d4a7959181f8b46bc/html5/thumbnails/39.jpg)
require_once __DIR__.'/../vendor/autoload.php';
$app = new Silex\Application();
$app->get('/hola', function() { return 'Hola mundo';});
$app->run();
Ejecutar código para una URL
![Page 40: Silex, desarrollo web ágil y profesional con PHP](https://reader034.vdocuments.co/reader034/viewer/2022051110/54b7b95d4a7959181f8b46bc/html5/thumbnails/40.jpg)
require_once __DIR__.'/../vendor/autoload.php';
$app = new Silex\Application();
$app->get('/hola', function() { return 'Hola mundo';});
$app->run();
Arrancar la aplicación
![Page 41: Silex, desarrollo web ágil y profesional con PHP](https://reader034.vdocuments.co/reader034/viewer/2022051110/54b7b95d4a7959181f8b46bc/html5/thumbnails/41.jpg)
require_once __DIR__.'/../vendor/autoload.php';
$app = new Silex\Application();
$app->get('/hola', function() { return 'Hola mundo';});
$app->run();
Hola Mundo en Silex
![Page 42: Silex, desarrollo web ágil y profesional con PHP](https://reader034.vdocuments.co/reader034/viewer/2022051110/54b7b95d4a7959181f8b46bc/html5/thumbnails/42.jpg)
require 'sinatra'
get '/hola' do "Hola mundo."end
Sinatra / Ruby
![Page 43: Silex, desarrollo web ágil y profesional con PHP](https://reader034.vdocuments.co/reader034/viewer/2022051110/54b7b95d4a7959181f8b46bc/html5/thumbnails/43.jpg)
var http = require('http');
http.createServer(function (request, response) { response.writeHead( 200, { 'Content-Type': 'text/plain' } ); response.end('Hola mundo\n');}).listen(80);
node.js / JavaScript
![Page 44: Silex, desarrollo web ágil y profesional con PHP](https://reader034.vdocuments.co/reader034/viewer/2022051110/54b7b95d4a7959181f8b46bc/html5/thumbnails/44.jpg)
Un ejemploreal
![Page 45: Silex, desarrollo web ágil y profesional con PHP](https://reader034.vdocuments.co/reader034/viewer/2022051110/54b7b95d4a7959181f8b46bc/html5/thumbnails/45.jpg)
![Page 46: Silex, desarrollo web ágil y profesional con PHP](https://reader034.vdocuments.co/reader034/viewer/2022051110/54b7b95d4a7959181f8b46bc/html5/thumbnails/46.jpg)
github.com/
javiereguiluz/bilbostack
Fork me on GitHub
![Page 47: Silex, desarrollo web ágil y profesional con PHP](https://reader034.vdocuments.co/reader034/viewer/2022051110/54b7b95d4a7959181f8b46bc/html5/thumbnails/47.jpg)
Enrutamiento
![Page 48: Silex, desarrollo web ágil y profesional con PHP](https://reader034.vdocuments.co/reader034/viewer/2022051110/54b7b95d4a7959181f8b46bc/html5/thumbnails/48.jpg)
$app->get('/', function () use ($app) { // ...});
Ruta de la portada
![Page 49: Silex, desarrollo web ágil y profesional con PHP](https://reader034.vdocuments.co/reader034/viewer/2022051110/54b7b95d4a7959181f8b46bc/html5/thumbnails/49.jpg)
$app->get('/agenda', function () use ($app) { // ...});
Ruta básica
![Page 50: Silex, desarrollo web ágil y profesional con PHP](https://reader034.vdocuments.co/reader034/viewer/2022051110/54b7b95d4a7959181f8b46bc/html5/thumbnails/50.jpg)
$app->get('/speakers/{slug}', function () use ($app) { // ...});
Ruta con partes variables
![Page 51: Silex, desarrollo web ágil y profesional con PHP](https://reader034.vdocuments.co/reader034/viewer/2022051110/54b7b95d4a7959181f8b46bc/html5/thumbnails/51.jpg)
$app->get('/speakers/{slug}', function () use ($app) { // ...});
Ruta con partes variables
![Page 52: Silex, desarrollo web ágil y profesional con PHP](https://reader034.vdocuments.co/reader034/viewer/2022051110/54b7b95d4a7959181f8b46bc/html5/thumbnails/52.jpg)
$app->get('/speakers/{slug}', function () use ($app) { // ...});
Ruta con partes variables
/speakers/pablo-garaizar/speakers/carlos-sanchez/speakers/esto-esta-mal
![Page 53: Silex, desarrollo web ágil y profesional con PHP](https://reader034.vdocuments.co/reader034/viewer/2022051110/54b7b95d4a7959181f8b46bc/html5/thumbnails/53.jpg)
$app->post('/registro', function () use ($app) { // ...});
Otros métodos HTTP
![Page 54: Silex, desarrollo web ágil y profesional con PHP](https://reader034.vdocuments.co/reader034/viewer/2022051110/54b7b95d4a7959181f8b46bc/html5/thumbnails/54.jpg)
$app->post('/registro', function () use ($app) { // ...});
Otros métodos HTTP
$app->put( );$app->delete( );
![Page 55: Silex, desarrollo web ágil y profesional con PHP](https://reader034.vdocuments.co/reader034/viewer/2022051110/54b7b95d4a7959181f8b46bc/html5/thumbnails/55.jpg)
$app->match('/registro', function () use ($app) { // ...});
Ruta para todos los métodos HTTP
![Page 56: Silex, desarrollo web ágil y profesional con PHP](https://reader034.vdocuments.co/reader034/viewer/2022051110/54b7b95d4a7959181f8b46bc/html5/thumbnails/56.jpg)
$app->match('/registro', function () use ($app) { // ...})->method('GET|POST');
Ruta para algunos métodos HTTP
![Page 57: Silex, desarrollo web ágil y profesional con PHP](https://reader034.vdocuments.co/reader034/viewer/2022051110/54b7b95d4a7959181f8b46bc/html5/thumbnails/57.jpg)
$app->get('/speakers/{slug}', function ($slug) { // ...});
Rutas con variables
![Page 58: Silex, desarrollo web ágil y profesional con PHP](https://reader034.vdocuments.co/reader034/viewer/2022051110/54b7b95d4a7959181f8b46bc/html5/thumbnails/58.jpg)
$app->get('/speakers/{track}/{slug}', function ($track, $slug) use ($app) { // ...});
Rutas con variables
![Page 59: Silex, desarrollo web ágil y profesional con PHP](https://reader034.vdocuments.co/reader034/viewer/2022051110/54b7b95d4a7959181f8b46bc/html5/thumbnails/59.jpg)
use Symfony\Component\HttpFoundation\Request;
$app->get('/speakers/{slug}', function (Request $request, $slug) { // ...});
Variables especiales
![Page 60: Silex, desarrollo web ágil y profesional con PHP](https://reader034.vdocuments.co/reader034/viewer/2022051110/54b7b95d4a7959181f8b46bc/html5/thumbnails/60.jpg)
use Symfony\Component\HttpFoundation\Request;
$app->get('/speakers/{slug}', function (Request $request, $slug) { // ...});
Variables especiales
![Page 61: Silex, desarrollo web ágil y profesional con PHP](https://reader034.vdocuments.co/reader034/viewer/2022051110/54b7b95d4a7959181f8b46bc/html5/thumbnails/61.jpg)
use Symfony\Component\HttpFoundation\Request;
$app->get('/speakers/{slug}', function (Request $request, $slug) { // ...});
Variables especiales
$request->server->get('HTTP_USER_AGENT')$request->get('slug')
![Page 62: Silex, desarrollo web ágil y profesional con PHP](https://reader034.vdocuments.co/reader034/viewer/2022051110/54b7b95d4a7959181f8b46bc/html5/thumbnails/62.jpg)
$app->get('/schedule/{slug}', function ($slug) use ($app) { // ...})->convert('slug', function ($slug) { return strtolower($slug);});
Modificando las variables de la ruta
![Page 63: Silex, desarrollo web ágil y profesional con PHP](https://reader034.vdocuments.co/reader034/viewer/2022051110/54b7b95d4a7959181f8b46bc/html5/thumbnails/63.jpg)
$app->get('/schedule/{slug}', function ($slug) use ($app) { // ...})->convert('slug', function ($slug) { return str_replace('-', '_', $slug);});
Modificando las variables de la ruta
![Page 64: Silex, desarrollo web ágil y profesional con PHP](https://reader034.vdocuments.co/reader034/viewer/2022051110/54b7b95d4a7959181f8b46bc/html5/thumbnails/64.jpg)
$app->get('/schedule/{track}', function ($track) { // ...})->assert('track', '\d+');
Restringiendo las variables de la ruta
![Page 65: Silex, desarrollo web ágil y profesional con PHP](https://reader034.vdocuments.co/reader034/viewer/2022051110/54b7b95d4a7959181f8b46bc/html5/thumbnails/65.jpg)
$app->get('/schedule/{track}', function ($track) { // ...})->assert('track', 'track\d+');
Restringiendo las variables de la ruta
![Page 66: Silex, desarrollo web ágil y profesional con PHP](https://reader034.vdocuments.co/reader034/viewer/2022051110/54b7b95d4a7959181f8b46bc/html5/thumbnails/66.jpg)
$app->get('/schedule/{track}', function ($track) { // ...})->assert('track', '1|2');
Restringiendo las variables de la ruta
![Page 67: Silex, desarrollo web ágil y profesional con PHP](https://reader034.vdocuments.co/reader034/viewer/2022051110/54b7b95d4a7959181f8b46bc/html5/thumbnails/67.jpg)
$app->get('/schedule/{track}', function ($track) { // ...})->value('track', '1');
Valores por defecto
![Page 68: Silex, desarrollo web ágil y profesional con PHP](https://reader034.vdocuments.co/reader034/viewer/2022051110/54b7b95d4a7959181f8b46bc/html5/thumbnails/68.jpg)
$app->get('/schedule/{track}', function ($track) { // ...})->value('track', '1');
Valores por defecto
/schedule/schedule/1
![Page 69: Silex, desarrollo web ágil y profesional con PHP](https://reader034.vdocuments.co/reader034/viewer/2022051110/54b7b95d4a7959181f8b46bc/html5/thumbnails/69.jpg)
$app->get('/', function () use ($app) { // ...})->bind('portada');
Rutas con nombre
![Page 70: Silex, desarrollo web ágil y profesional con PHP](https://reader034.vdocuments.co/reader034/viewer/2022051110/54b7b95d4a7959181f8b46bc/html5/thumbnails/70.jpg)
Controladores
![Page 71: Silex, desarrollo web ágil y profesional con PHP](https://reader034.vdocuments.co/reader034/viewer/2022051110/54b7b95d4a7959181f8b46bc/html5/thumbnails/71.jpg)
$app->get('/donde-comer', function () use ($app) { return $app['twig']->render('comer.twig');})->bind('comer');
Controlador básico
![Page 72: Silex, desarrollo web ágil y profesional con PHP](https://reader034.vdocuments.co/reader034/viewer/2022051110/54b7b95d4a7959181f8b46bc/html5/thumbnails/72.jpg)
$app->get('/', function () use ($app) { $ponentes = $app['ponentes'];
return $app['twig']->render('portada.twig', array('ponentes' => $ponentes) );})->bind('portada');
Controlador típico
![Page 73: Silex, desarrollo web ágil y profesional con PHP](https://reader034.vdocuments.co/reader034/viewer/2022051110/54b7b95d4a7959181f8b46bc/html5/thumbnails/73.jpg)
$app->get('/', function () use ($app) { $ponentes = $app['ponentes'];
return $app['twig']->render('portada.twig', array('ponentes' => $ponentes) );})->bind('portada');
Controlador típico
![Page 74: Silex, desarrollo web ágil y profesional con PHP](https://reader034.vdocuments.co/reader034/viewer/2022051110/54b7b95d4a7959181f8b46bc/html5/thumbnails/74.jpg)
$app->get('/speakers/{slug}', function ($slug) use ($app) {
if (...) { $app->abort(404, "No existe el ponente."); }
// ... return $app['twig']->render('ponente.twig',...);
})->bind('ponente');
Gestión de errores
![Page 75: Silex, desarrollo web ágil y profesional con PHP](https://reader034.vdocuments.co/reader034/viewer/2022051110/54b7b95d4a7959181f8b46bc/html5/thumbnails/75.jpg)
$app->get('/speakers/{slug}', function ($slug) use ($app) {
if (...) { $app->abort(404, "No existe el ponente."); }
// ... return $app['twig']->render('ponente.twig',...);
})->bind('ponente');
Gestión de errores
![Page 76: Silex, desarrollo web ágil y profesional con PHP](https://reader034.vdocuments.co/reader034/viewer/2022051110/54b7b95d4a7959181f8b46bc/html5/thumbnails/76.jpg)
$app->error(function (\Exception $e, $code) use ($app) {
// ...
});
Controlador especial para errores
![Page 77: Silex, desarrollo web ágil y profesional con PHP](https://reader034.vdocuments.co/reader034/viewer/2022051110/54b7b95d4a7959181f8b46bc/html5/thumbnails/77.jpg)
$app->error(function (\Exception $e, $code) use ($app) {
// ...
});
Controlador especial para errores
![Page 78: Silex, desarrollo web ágil y profesional con PHP](https://reader034.vdocuments.co/reader034/viewer/2022051110/54b7b95d4a7959181f8b46bc/html5/thumbnails/78.jpg)
$app->get('/', ...)->bind('portada');
$app->get('/agenda', ...)->bind('agenda');
$app->get('/speakers/{slug}', ...)->bind('ponente');
$app->get('/schedule/{slug}', ...)->bind('ponencia');
Esqueleto de una aplicación Silex
![Page 79: Silex, desarrollo web ágil y profesional con PHP](https://reader034.vdocuments.co/reader034/viewer/2022051110/54b7b95d4a7959181f8b46bc/html5/thumbnails/79.jpg)
1 aplicación = 1 archivo
$app->get('/', ...)->bind('portada');
$app->get('/agenda', ...)->bind('agenda');
$app->get('/speakers/{slug}', ...)->bind('ponente');
$app->get('/schedule/{slug}', ...)->bind('ponencia');
controllers.php
![Page 80: Silex, desarrollo web ágil y profesional con PHP](https://reader034.vdocuments.co/reader034/viewer/2022051110/54b7b95d4a7959181f8b46bc/html5/thumbnails/80.jpg)
1 aplicación = 1 archivo
$app->get('/', ...)->bind('portada');
$app->get('/agenda', ...)->bind('agenda');
$app->get('/speakers/{slug}', ...)->bind('ponente');
$app->get('/schedule/{slug}', ...)->bind('ponencia');
controllers.php
EL ORDEN IMPORTA
![Page 81: Silex, desarrollo web ágil y profesional con PHP](https://reader034.vdocuments.co/reader034/viewer/2022051110/54b7b95d4a7959181f8b46bc/html5/thumbnails/81.jpg)
Middlewares(filtros)
![Page 82: Silex, desarrollo web ágil y profesional con PHP](https://reader034.vdocuments.co/reader034/viewer/2022051110/54b7b95d4a7959181f8b46bc/html5/thumbnails/82.jpg)
Petición HTTP
Tu aplicación
Página HTML
Objeto Request
Objeto Response
Silex
Silex
![Page 83: Silex, desarrollo web ágil y profesional con PHP](https://reader034.vdocuments.co/reader034/viewer/2022051110/54b7b95d4a7959181f8b46bc/html5/thumbnails/83.jpg)
Petición HTTP
Tu aplicación
Página HTML
Objeto Request
Objeto Response
Silex
Silex
!
!
!
![Page 84: Silex, desarrollo web ágil y profesional con PHP](https://reader034.vdocuments.co/reader034/viewer/2022051110/54b7b95d4a7959181f8b46bc/html5/thumbnails/84.jpg)
$app->before(function (Request $request) { // ...});
Filtro before
![Page 85: Silex, desarrollo web ágil y profesional con PHP](https://reader034.vdocuments.co/reader034/viewer/2022051110/54b7b95d4a7959181f8b46bc/html5/thumbnails/85.jpg)
Secuencia de ejecución• Buscar la ruta que pide el usuario.
• Comprobar la seguridad.
• Ejecutar filtro before()
• Ejecutar controlador de la ruta
![Page 86: Silex, desarrollo web ágil y profesional con PHP](https://reader034.vdocuments.co/reader034/viewer/2022051110/54b7b95d4a7959181f8b46bc/html5/thumbnails/86.jpg)
$app->before(function (Request $request) { if (...) { return new RedirectResponse('/login'); }});
Ejemplo de filtro before
![Page 87: Silex, desarrollo web ágil y profesional con PHP](https://reader034.vdocuments.co/reader034/viewer/2022051110/54b7b95d4a7959181f8b46bc/html5/thumbnails/87.jpg)
$app->after(function (Request $request, Response $response) { // ...});
Filtro after
![Page 88: Silex, desarrollo web ágil y profesional con PHP](https://reader034.vdocuments.co/reader034/viewer/2022051110/54b7b95d4a7959181f8b46bc/html5/thumbnails/88.jpg)
Secuencia de ejecución• Buscar la ruta que pide el usuario.
• Comprobar la seguridad.
• Ejecutar filtro before( )
• Ejecutar controlador de la ruta.
• Ejecutar filtro after( )
• Enviar respuesta al usuario.
![Page 89: Silex, desarrollo web ágil y profesional con PHP](https://reader034.vdocuments.co/reader034/viewer/2022051110/54b7b95d4a7959181f8b46bc/html5/thumbnails/89.jpg)
$app->after(function (Request $request) { log('...');});
Ejemplo de filtro after
![Page 90: Silex, desarrollo web ágil y profesional con PHP](https://reader034.vdocuments.co/reader034/viewer/2022051110/54b7b95d4a7959181f8b46bc/html5/thumbnails/90.jpg)
$app->after(function (Request $request) { log('...');});
Ejemplo de filtro after
¿Dónde está mi respuesta?
![Page 91: Silex, desarrollo web ágil y profesional con PHP](https://reader034.vdocuments.co/reader034/viewer/2022051110/54b7b95d4a7959181f8b46bc/html5/thumbnails/91.jpg)
$app->finish(function (Request $request, Response $response) { // ...});
Filtro finish
![Page 92: Silex, desarrollo web ágil y profesional con PHP](https://reader034.vdocuments.co/reader034/viewer/2022051110/54b7b95d4a7959181f8b46bc/html5/thumbnails/92.jpg)
$app->get('/', ...)->bind('portada')->before(...);
$app->get('/agenda', ...)->bind('agenda')->before(...)->after(...);
$app->get('/speakers/{slug}', ...)->bind('ponente')->before(...);
$app->get('/schedule/{slug}', ...)->bind('ponencia')->before(...);
Cada ruta con su filtro
![Page 93: Silex, desarrollo web ágil y profesional con PHP](https://reader034.vdocuments.co/reader034/viewer/2022051110/54b7b95d4a7959181f8b46bc/html5/thumbnails/93.jpg)
Proveedores deservicios
![Page 94: Silex, desarrollo web ágil y profesional con PHP](https://reader034.vdocuments.co/reader034/viewer/2022051110/54b7b95d4a7959181f8b46bc/html5/thumbnails/94.jpg)
Núcleo de Silex
Bases de datos
Plantillas
Caché HTTP
Envío de emails
Formularios
proveedores
![Page 95: Silex, desarrollo web ágil y profesional con PHP](https://reader034.vdocuments.co/reader034/viewer/2022051110/54b7b95d4a7959181f8b46bc/html5/thumbnails/95.jpg)
Núcleo de Silex
Bases de datos
Plantillas
Caché HTTP
Envío de emails
Formularios
proveedores
![Page 96: Silex, desarrollo web ágil y profesional con PHP](https://reader034.vdocuments.co/reader034/viewer/2022051110/54b7b95d4a7959181f8b46bc/html5/thumbnails/96.jpg)
Núcleo de Silex
Bases de datos
Plantillas
Caché HTTP
Envío de emails
Formularios
proveedores
![Page 97: Silex, desarrollo web ágil y profesional con PHP](https://reader034.vdocuments.co/reader034/viewer/2022051110/54b7b95d4a7959181f8b46bc/html5/thumbnails/97.jpg)
Núcleo de Silex
Bases de datos
Plantillas
Caché HTTP
Envío de emails
Formularios
![Page 98: Silex, desarrollo web ágil y profesional con PHP](https://reader034.vdocuments.co/reader034/viewer/2022051110/54b7b95d4a7959181f8b46bc/html5/thumbnails/98.jpg)
Bases de datos Serializador Seguridad
Formularios Logger Sesiones
Validación Emails Plantillas
Caché HTTP URL
![Page 99: Silex, desarrollo web ágil y profesional con PHP](https://reader034.vdocuments.co/reader034/viewer/2022051110/54b7b95d4a7959181f8b46bc/html5/thumbnails/99.jpg)
use Silex\Provider\FormServiceProvider;
// ...
$app->register(new FormServiceProvider());
Usar formularios de Symfony2
![Page 100: Silex, desarrollo web ágil y profesional con PHP](https://reader034.vdocuments.co/reader034/viewer/2022051110/54b7b95d4a7959181f8b46bc/html5/thumbnails/100.jpg)
use Silex\Provider\DoctrineServiceProvider;
// ...
$app->register(new DoctrineServiceProvider());
Acceder a BBDD con Doctrine2
![Page 101: Silex, desarrollo web ágil y profesional con PHP](https://reader034.vdocuments.co/reader034/viewer/2022051110/54b7b95d4a7959181f8b46bc/html5/thumbnails/101.jpg)
use Silex\Provider\TwigServiceProvider;
// ...
$app->register(new TwigServiceProvider(), array( 'twig.path' => array(__DIR__.'/../templates'), 'twig.options' => array('...'),));
Utilizar las plantillas de Twig
![Page 102: Silex, desarrollo web ágil y profesional con PHP](https://reader034.vdocuments.co/reader034/viewer/2022051110/54b7b95d4a7959181f8b46bc/html5/thumbnails/102.jpg)
DEMO
![Page 103: Silex, desarrollo web ágil y profesional con PHP](https://reader034.vdocuments.co/reader034/viewer/2022051110/54b7b95d4a7959181f8b46bc/html5/thumbnails/103.jpg)
github.com/javiereguiluz/bilbostack
Fork me on GitH
ub
![Page 104: Silex, desarrollo web ágil y profesional con PHP](https://reader034.vdocuments.co/reader034/viewer/2022051110/54b7b95d4a7959181f8b46bc/html5/thumbnails/104.jpg)
Instalación
![Page 105: Silex, desarrollo web ágil y profesional con PHP](https://reader034.vdocuments.co/reader034/viewer/2022051110/54b7b95d4a7959181f8b46bc/html5/thumbnails/105.jpg)
Descargar archivo comprimido
silex.sensiolabs.org
![Page 106: Silex, desarrollo web ágil y profesional con PHP](https://reader034.vdocuments.co/reader034/viewer/2022051110/54b7b95d4a7959181f8b46bc/html5/thumbnails/106.jpg)
Descargar archivo comprimido
![Page 107: Silex, desarrollo web ágil y profesional con PHP](https://reader034.vdocuments.co/reader034/viewer/2022051110/54b7b95d4a7959181f8b46bc/html5/thumbnails/107.jpg)
Descargar archivo comprimido
![Page 108: Silex, desarrollo web ágil y profesional con PHP](https://reader034.vdocuments.co/reader034/viewer/2022051110/54b7b95d4a7959181f8b46bc/html5/thumbnails/108.jpg)
Descargar archivo comprimido
![Page 109: Silex, desarrollo web ágil y profesional con PHP](https://reader034.vdocuments.co/reader034/viewer/2022051110/54b7b95d4a7959181f8b46bc/html5/thumbnails/109.jpg)
{ "require": { "silex/silex": "1.0.*" }, "minimum-stability": "dev"}
Instalación via composer.json
![Page 110: Silex, desarrollo web ágil y profesional con PHP](https://reader034.vdocuments.co/reader034/viewer/2022051110/54b7b95d4a7959181f8b46bc/html5/thumbnails/110.jpg)
{ "require": { "fabpot/silex-skeleton": "*" }, "minimum-stability": "dev"}
Instalación via composer.json
![Page 111: Silex, desarrollo web ágil y profesional con PHP](https://reader034.vdocuments.co/reader034/viewer/2022051110/54b7b95d4a7959181f8b46bc/html5/thumbnails/111.jpg)
$ composer create-project fabpot/silex-skeleton --stability=dev
Instalación recomendada
![Page 112: Silex, desarrollo web ágil y profesional con PHP](https://reader034.vdocuments.co/reader034/viewer/2022051110/54b7b95d4a7959181f8b46bc/html5/thumbnails/112.jpg)
![Page 113: Silex, desarrollo web ágil y profesional con PHP](https://reader034.vdocuments.co/reader034/viewer/2022051110/54b7b95d4a7959181f8b46bc/html5/thumbnails/113.jpg)
![Page 114: Silex, desarrollo web ágil y profesional con PHP](https://reader034.vdocuments.co/reader034/viewer/2022051110/54b7b95d4a7959181f8b46bc/html5/thumbnails/114.jpg)
![Page 115: Silex, desarrollo web ágil y profesional con PHP](https://reader034.vdocuments.co/reader034/viewer/2022051110/54b7b95d4a7959181f8b46bc/html5/thumbnails/115.jpg)
![Page 116: Silex, desarrollo web ágil y profesional con PHP](https://reader034.vdocuments.co/reader034/viewer/2022051110/54b7b95d4a7959181f8b46bc/html5/thumbnails/116.jpg)
![Page 117: Silex, desarrollo web ágil y profesional con PHP](https://reader034.vdocuments.co/reader034/viewer/2022051110/54b7b95d4a7959181f8b46bc/html5/thumbnails/117.jpg)
web/index.php
src/app.php
src/controllers.php
![Page 118: Silex, desarrollo web ágil y profesional con PHP](https://reader034.vdocuments.co/reader034/viewer/2022051110/54b7b95d4a7959181f8b46bc/html5/thumbnails/118.jpg)
web/index.php
src/app.php
src/controllers.php
No tocar
![Page 119: Silex, desarrollo web ágil y profesional con PHP](https://reader034.vdocuments.co/reader034/viewer/2022051110/54b7b95d4a7959181f8b46bc/html5/thumbnails/119.jpg)
web/index.php
src/app.php
src/controllers.php
No tocar Activas servicios
![Page 120: Silex, desarrollo web ágil y profesional con PHP](https://reader034.vdocuments.co/reader034/viewer/2022051110/54b7b95d4a7959181f8b46bc/html5/thumbnails/120.jpg)
web/index.php
src/app.php
src/controllers.php
No tocar Activas servicios
$app->get('/', ...});
![Page 121: Silex, desarrollo web ágil y profesional con PHP](https://reader034.vdocuments.co/reader034/viewer/2022051110/54b7b95d4a7959181f8b46bc/html5/thumbnails/121.jpg)
Buenasprácticas
![Page 122: Silex, desarrollo web ágil y profesional con PHP](https://reader034.vdocuments.co/reader034/viewer/2022051110/54b7b95d4a7959181f8b46bc/html5/thumbnails/122.jpg)
Organizandolos controladores
![Page 123: Silex, desarrollo web ágil y profesional con PHP](https://reader034.vdocuments.co/reader034/viewer/2022051110/54b7b95d4a7959181f8b46bc/html5/thumbnails/123.jpg)
$app->get('/', ...);
blog.php
$app->get('/', ...);
backend.php
$app->get('/', ...);
// ...
controllers.php
![Page 124: Silex, desarrollo web ágil y profesional con PHP](https://reader034.vdocuments.co/reader034/viewer/2022051110/54b7b95d4a7959181f8b46bc/html5/thumbnails/124.jpg)
$app->mount('/blog', include 'blog.php');$app->mount('/admin', include 'backend.php');
Importando controladores
![Page 125: Silex, desarrollo web ágil y profesional con PHP](https://reader034.vdocuments.co/reader034/viewer/2022051110/54b7b95d4a7959181f8b46bc/html5/thumbnails/125.jpg)
$app->mount('/blog', include 'blog.php');$app->mount('/admin', include 'backend.php');
Importando controladores
![Page 126: Silex, desarrollo web ágil y profesional con PHP](https://reader034.vdocuments.co/reader034/viewer/2022051110/54b7b95d4a7959181f8b46bc/html5/thumbnails/126.jpg)
$app->mount('/blog', include 'blog.php');$app->mount('/admin', include 'backend.php');
Importando controladores
PORTADA: /PORTADA DE BLOG: /blog/PORTADA DE BACKEND: /admin/
![Page 127: Silex, desarrollo web ágil y profesional con PHP](https://reader034.vdocuments.co/reader034/viewer/2022051110/54b7b95d4a7959181f8b46bc/html5/thumbnails/127.jpg)
1 aplicación = 1 archivo
$app->get('/', ...)->bind('portada');
$app->get('/agenda', ...)->bind('agenda');
$app->get('/speakers/{slug}', ...)->bind('ponente');
$app->get('/schedule/{slug}', ...)->bind('ponencia');
controllers.php
![Page 128: Silex, desarrollo web ágil y profesional con PHP](https://reader034.vdocuments.co/reader034/viewer/2022051110/54b7b95d4a7959181f8b46bc/html5/thumbnails/128.jpg)
1 aplicación pequeña = 1 archivo
$app->get('/', ...)->bind('portada');
$app->get('/agenda', ...)->bind('agenda');
$app->get('/speakers/{slug}', ...)->bind('ponente');
$app->get('/schedule/{slug}', ...)->bind('ponencia');
controllers.php
![Page 129: Silex, desarrollo web ágil y profesional con PHP](https://reader034.vdocuments.co/reader034/viewer/2022051110/54b7b95d4a7959181f8b46bc/html5/thumbnails/129.jpg)
$app->get('/', ...);
$app->get('/ver', ...);
$app->get('/listar', ...);
$app->get('/', ...);
1 aplicación mediana = N archivos
$app->get('/', ...)->bind('portada');
$app->get('/agenda', ...)->bind('agenda');
$app->get('/speakers/{slug}', ...)->bind('ponente');
$app->get('/schedule/{slug}', ...)->bind('ponencia');
controllers.php
blog.php
backend.php
![Page 130: Silex, desarrollo web ágil y profesional con PHP](https://reader034.vdocuments.co/reader034/viewer/2022051110/54b7b95d4a7959181f8b46bc/html5/thumbnails/130.jpg)
Escalando unaaplicación Silex
![Page 131: Silex, desarrollo web ágil y profesional con PHP](https://reader034.vdocuments.co/reader034/viewer/2022051110/54b7b95d4a7959181f8b46bc/html5/thumbnails/131.jpg)
namespace Igorw\Shop\Controller;
use Silex\Application;use Symfony\Component\HttpFoundation\Request;
class ShopController{ public function indexAction(Request $request, Application $app) { ... }}
Controladores como clases
![Page 132: Silex, desarrollo web ágil y profesional con PHP](https://reader034.vdocuments.co/reader034/viewer/2022051110/54b7b95d4a7959181f8b46bc/html5/thumbnails/132.jpg)
$app->get('/', controller('shop/index'));
$app->match('/login', controller('shop/login'));
$app->get('/product', controller('shop/
product'));
Controladores como clases
![Page 133: Silex, desarrollo web ágil y profesional con PHP](https://reader034.vdocuments.co/reader034/viewer/2022051110/54b7b95d4a7959181f8b46bc/html5/thumbnails/133.jpg)
http://igor.io/2012/11/09/scaling-silex.html
![Page 134: Silex, desarrollo web ágil y profesional con PHP](https://reader034.vdocuments.co/reader034/viewer/2022051110/54b7b95d4a7959181f8b46bc/html5/thumbnails/134.jpg)
Puntosdébiles
![Page 135: Silex, desarrollo web ágil y profesional con PHP](https://reader034.vdocuments.co/reader034/viewer/2022051110/54b7b95d4a7959181f8b46bc/html5/thumbnails/135.jpg)
i18n Depuración
ORM Documentación
ESI
![Page 136: Silex, desarrollo web ágil y profesional con PHP](https://reader034.vdocuments.co/reader034/viewer/2022051110/54b7b95d4a7959181f8b46bc/html5/thumbnails/136.jpg)
i18n (internacionalización)• Fácil traducir contenidos
{{ "Hola Mundo"|trans }}
• No es cómodo traducir rutas y contenidos de la base de datos.
![Page 137: Silex, desarrollo web ágil y profesional con PHP](https://reader034.vdocuments.co/reader034/viewer/2022051110/54b7b95d4a7959181f8b46bc/html5/thumbnails/137.jpg)
ORM• No hay soporte oficial del ORM
completo (Doctrine2).
• No hay ORM alternativos ligeros.
• Si tu aplicación necesita un ORM, puede ser demasiado grande para Silex.
![Page 138: Silex, desarrollo web ágil y profesional con PHP](https://reader034.vdocuments.co/reader034/viewer/2022051110/54b7b95d4a7959181f8b46bc/html5/thumbnails/138.jpg)
// activar el modo debug$app['debug'] = true;
// activar el loguse Silex\Provider\MonologServiceProvider;
$app->register(new MonologServiceProvider(), array( 'monolog.logfile' => __DIR__.'/../logs/dev.log',));
Depuración
![Page 139: Silex, desarrollo web ágil y profesional con PHP](https://reader034.vdocuments.co/reader034/viewer/2022051110/54b7b95d4a7959181f8b46bc/html5/thumbnails/139.jpg)
Mensajes de error
![Page 140: Silex, desarrollo web ágil y profesional con PHP](https://reader034.vdocuments.co/reader034/viewer/2022051110/54b7b95d4a7959181f8b46bc/html5/thumbnails/140.jpg)
![Page 141: Silex, desarrollo web ágil y profesional con PHP](https://reader034.vdocuments.co/reader034/viewer/2022051110/54b7b95d4a7959181f8b46bc/html5/thumbnails/141.jpg)
ESI• Silex incluye soporte de ESI.
• Su uso es bastante incómodo (comparado con Symfony2).
![Page 142: Silex, desarrollo web ágil y profesional con PHP](https://reader034.vdocuments.co/reader034/viewer/2022051110/54b7b95d4a7959181f8b46bc/html5/thumbnails/142.jpg)
Documentación• Todas las características de Silex
están documentadas.
• No es tan abundante como la de Symfony2.
![Page 143: Silex, desarrollo web ágil y profesional con PHP](https://reader034.vdocuments.co/reader034/viewer/2022051110/54b7b95d4a7959181f8b46bc/html5/thumbnails/143.jpg)
Documentación• Oficial
silex.sensiolabs.org/documentation
• Traducciónlibrosweb.es/silex
![Page 144: Silex, desarrollo web ágil y profesional con PHP](https://reader034.vdocuments.co/reader034/viewer/2022051110/54b7b95d4a7959181f8b46bc/html5/thumbnails/144.jpg)
Conclusiones
![Page 145: Silex, desarrollo web ágil y profesional con PHP](https://reader034.vdocuments.co/reader034/viewer/2022051110/54b7b95d4a7959181f8b46bc/html5/thumbnails/145.jpg)
Conclusiones• Silex es un microframework, pero no es
un juguete.
![Page 146: Silex, desarrollo web ágil y profesional con PHP](https://reader034.vdocuments.co/reader034/viewer/2022051110/54b7b95d4a7959181f8b46bc/html5/thumbnails/146.jpg)
Conclusiones• Silex es un microframework, pero no es
un juguete.
• Silex sirve para cualquier aplicación web que no sea enorme.
![Page 147: Silex, desarrollo web ágil y profesional con PHP](https://reader034.vdocuments.co/reader034/viewer/2022051110/54b7b95d4a7959181f8b46bc/html5/thumbnails/147.jpg)
Conclusiones• Silex es un microframework, pero no es
un juguete.
• Silex sirve para cualquier aplicación web que no sea enorme.
• Silex reduce a “horas” el tiempo de desarrollo de aplicaciones enteras.
![Page 148: Silex, desarrollo web ágil y profesional con PHP](https://reader034.vdocuments.co/reader034/viewer/2022051110/54b7b95d4a7959181f8b46bc/html5/thumbnails/148.jpg)
Conclusiones• Silex es un microframework, pero no es
un juguete.
• Silex sirve para cualquier aplicación web que no sea enorme.
• Silex reduce a “horas” el tiempo de desarrollo de aplicaciones enteras.
• Silex combina la agilidad de PHP con la profesionalidad de Symfony.
![Page 149: Silex, desarrollo web ágil y profesional con PHP](https://reader034.vdocuments.co/reader034/viewer/2022051110/54b7b95d4a7959181f8b46bc/html5/thumbnails/149.jpg)
GRACIAS.
![Page 150: Silex, desarrollo web ágil y profesional con PHP](https://reader034.vdocuments.co/reader034/viewer/2022051110/54b7b95d4a7959181f8b46bc/html5/thumbnails/150.jpg)
Contacto• [email protected]
• twitter.com/javiereguiluz
• github.com/javiereguiluz
• linkedin.com/in/javiereguiluz
domingo, 27 de enero de 13