Más contenido relacionado
Similar a Introduzione a Silex (20)
Introduzione a Silex
- 1. © H-art 2011 | All Rights Reserved | H-art is a GroupM Company
- 2. Development area
meeting #4/2011
© H-art 2011 | All Rights Reserved | H-art is a GroupM Company
- 3. SILEX
PHP Micro-framework
© H-art 2011 | All Rights Reserved | H-art is a GroupM Company
- 6. Perfetti quando un framework è
“troppa roba”
© H-art 2011 | All Rights Reserved | H-art is a GroupM Company
- 7. SILEX
http://silex-project.org/
https://github.com/fabpot/Silex
© H-art 2011 | All Rights Reserved | H-art is a GroupM Company
- 8. Perchè silex ?
• 400 KB
• Ottima implementazione
• Basato su symfony 2 (riuso di componenti e knowledge)
• Customizzabile con estensioni
© H-art 2011 | All Rights Reserved | H-art is a GroupM Company
- 9. SILEX
require_once __DIR__.'/silex.phar';
$app = new SilexApplication();
$app->get('/hello/{name}', function($name) {
return "Hello $name";
});
$app->run();
© H-art 2011 | All Rights Reserved | H-art is a GroupM Company
- 10. SILEX
require_once __DIR__.'/silex.phar';
$app = new SilexApplication(); Il Framework
$app->get('/hello/{name}', function($name) {
return "Hello $name";
});
$app->run();
© H-art 2011 | All Rights Reserved | H-art is a GroupM Company
- 11. SILEX
require_once __DIR__.'/silex.phar';
$app = new SilexApplication(); L’ applicazione
$app->get('/hello/{name}', function($name) {
return "Hello $name";
});
$app->run();
© H-art 2011 | All Rights Reserved | H-art is a GroupM Company
- 12. SILEX
require_once __DIR__.'/silex.phar';
$app = new SilexApplication();
$app->get('/hello/{name}', function($name) {
return "Hello $name";
});
$app->run();
La action
© H-art 2011 | All Rights Reserved | H-art is a GroupM Company
- 13. Una route
SILEX
require_once __DIR__.'/silex.phar';
$app = new SilexApplication();
$app->get('/hello/{name}', function($name) {
return "Hello $name";
});
$app->run();
La action
© H-art 2011 | All Rights Reserved | H-art is a GroupM Company
- 14. Route
SILEX
Controller
require_once __DIR__.'/silex.phar';
$app = new SilexApplication();
$app->get('/hello/{name}', function($name) {
return "Hello $name";
});
$app->run();
La action
© H-art 2011 | All Rights Reserved | H-art is a GroupM Company
- 15. SILEX
require_once __DIR__.'/silex.phar';
$app = new SilexApplication();
$app->get('/hello/{name}', function($name) {
return "Hello $name";
});
E si balla!!
$app->run();
© H-art 2011 | All Rights Reserved | H-art is a GroupM Company
- 16. Un pò di più ?
© H-art 2011 | All Rights Reserved | H-art is a GroupM Company
- 18. Posso definire dei comportamenti o delle operazioni da eseguire prima e dopo
ogni action passando delle closure ai filtri before e after
$app->before(function () {
// attivo una connesione a DB ?
// carico qualche layout generico ?
});
$app->after(function () {
// chiudo connessione a DB ?
//
});
© H-art 2011 | All Rights Reserved | H-art is a GroupM Company
- 20. Posso definire dei comportamenti in caso di errore per fare in modo che
l’applicazione li notifichi in maniera “decente”
use SymfonyComponentHttpFoundationResponse;
use SymfonyComponentHttpKernelExceptionHttpException;
use SymfonyComponentHttpKernelExceptionNotFoundHttpException;
$app->error(function (Exception $e) {
if ($e instanceof NotFoundHttpException) {
return new Response('The requested page could not be
found.', 404);
}
$code = ($e instanceof HttpException) ? $e->getStatusCode() :
500;
return new Response('We are sorry, but something went
terribly wrong.', $code);
});
© H-art 2011 | All Rights Reserved | H-art is a GroupM Company
- 22. Silex mette a disposizione il metodo escape() per ottenere l’escaping delle
variabili
$app->get('/name', function () use ($app) {
$name = $app['request']->get('name');
return "You provided the name {$app-
>escape($name)}.";
});
© H-art 2011 | All Rights Reserved | H-art is a GroupM Company
- 24. Variabili
$app->get('/blog/show/{id}', function ($id) {
...
});
$app->get('/blog/show/{postId}/{commentId}',
function ($postId, $commentId) {
...
});
$app->get('/blog/show/{postId}/{commentId}',
function ($commentId, $postId) {
...
});
© H-art 2011 | All Rights Reserved | H-art is a GroupM Company
- 25. Converter
$app->get('/user/{id}', function ($id) {
// ...
})->convert('id', function ($id) { return (int)
$id; });
Il parametro $id viene passato alla
closure e non alla action che riceve
invece il valore restituito dalla
closure
© H-art 2011 | All Rights Reserved | H-art is a GroupM Company
- 26. Converter
$userProvider = function ($id) {
return new User($id);
};
$app->get('/user/{user}', function (User $user) {
// ...
})->convert('user', $userProvider);
$app->get('/user/{user}/edit', function (User
$user) {
// ...
})->convert('user', $userProvider);
© H-art 2011 | All Rights Reserved | H-art is a GroupM Company
- 27. Requirements
$app->get('/blog/show/{postId}/{commentId}',
function ($postId, $commentId) {
...
})
->assert('postId', 'd+')
->assert('commentId', 'd+');
© H-art 2011 | All Rights Reserved | H-art is a GroupM Company
- 28. Default Values
$app->get('/{pageName}', function ($pageName) {
...
})
->value('pageName', 'index');
© H-art 2011 | All Rights Reserved | H-art is a GroupM Company
- 30. // blog.php
require_once __DIR__.'/silex.phar';
$app = new SilexApplication();
// define your blog app
$app->get('/post/{id}', function ($id) { ... });
// return the app instance
return $app;
$blog = require __DIR__.'/blog.php';
$app = new SilexApplication();
$app->mount('/blog', $blog);
$app->run();
© H-art 2011 | All Rights Reserved | H-art is a GroupM Company
- 31. Ancora non basta ?
© H-art 2011 | All Rights Reserved | H-art is a GroupM Company
- 33. Extensions incluse
• DoctrineExtension
• MonologExtension
• SessionExtension
• TwigExtension
• TranslationExtension
• UrlGeneratorExtension
• ValidatorExtension
Altre implementabili attraverso API
© H-art 2011 | All Rights Reserved | H-art is a GroupM Company
- 35. ... class FooAppTest extends WebTestCase
public function testInitialPage()
{
$client = $this->createClient();
$crawler = $client->request('GET', '/');
$this->assertTrue($client->getResponse()->isOk());
$this->assertEquals(1, count($crawler-
>filter('h1:contains("Contact us")')));
$this->assertEquals(1, count($crawler-
>filter('form')));
...
}
© H-art 2011 | All Rights Reserved | H-art is a GroupM Company
- 36. ... class FooAppTest extends WebTestCase
Il browser
public function testInitialPage()
{
$client = $this->createClient();
$crawler = $client->request('GET', '/');
$this->assertTrue($client->getResponse()->isOk());
$this->assertEquals(1, count($crawler-
>filter('h1:contains("Contact us")')));
$this->assertEquals(1, count($crawler-
>filter('form')));
...
}
© H-art 2011 | All Rights Reserved | H-art is a GroupM Company
- 37. ... class FooAppTest extends WebTestCase
Il parser della pagina
public function testInitialPage()
{
$client = $this->createClient();
$crawler = $client->request('GET', '/');
$this->assertTrue($client->getResponse()->isOk());
$this->assertEquals(1, count($crawler-
>filter('h1:contains("Contact us")')));
$this->assertEquals(1, count($crawler-
>filter('form')));
...
}
© H-art 2011 | All Rights Reserved | H-art is a GroupM Company
- 38. ... class FooAppTest extends WebTestCase
Verifiche su contenuto e
public function testInitialPage() Response
{
$client = $this->createClient();
$crawler = $client->request('GET', '/');
$this->assertTrue($client->getResponse()->isOk());
$this->assertEquals(1, count($crawler-
>filter('h1:contains("Contact us")')));
$this->assertEquals(1, count($crawler-
>filter('form')));
...
}
© H-art 2011 | All Rights Reserved | H-art is a GroupM Company
Notas del editor
- \n
- \n
- \n
- \n
- \n
- \n
- \n
- \n
- \n
- \n
- \n
- \n
- \n
- \n
- \n
- \n
- \n
- \n
- \n
- \n
- \n
- \n
- \n
- \n
- \n
- \n
- \n
- \n
- \n
- \n
- \n
- \n
- \n
- \n
- \n
- \n
- \n
- \n
- \n
- \n
- \n