Slide 1

Slide 1 text

No content

Slide 2

Slide 2 text

• phpBB • Symfony2 • Silex igorw

Slide 3

Slide 3 text

No content

Slide 4

Slide 4 text

Silex Symfony goes micro μ

Slide 5

Slide 5 text

Silex is not Symfony

Slide 6

Slide 6 text

Microframework

Slide 7

Slide 7 text

No content

Slide 8

Slide 8 text

What? • Bare bones • Routes mapped to controllers • The ‘C’ of ‘MVC’ • REST • Single file app

Slide 9

Slide 9 text

Why?

Slide 10

Slide 10 text

Sometimes a full-stack framework is too much for a simple task.

Slide 11

Slide 11 text

simple

Slide 12

Slide 12 text

What makes silex special?

Slide 13

Slide 13 text

• concise • extensible • testable

Slide 14

Slide 14 text

• concise • extensible • testable

Slide 15

Slide 15 text

• concise • extensible • testable

Slide 16

Slide 16 text

• concise • extensible • testable

Slide 17

Slide 17 text

• concise • extensible • testable

Slide 18

Slide 18 text

Http Kernel Interface

Slide 19

Slide 19 text

Response handle(Request $request)

Slide 20

Slide 20 text

client

Slide 21

Slide 21 text

request client

Slide 22

Slide 22 text

reponse client request

Slide 23

Slide 23 text

clean

Slide 24

Slide 24 text

PSR-0

Slide 25

Slide 25 text

No content

Slide 26

Slide 26 text

Silex is not Symfony

Slide 27

Slide 27 text

Silex is a user interface for Symfony

Slide 28

Slide 28 text

require_once __DIR__.'/silex.phar'; $app = new Silex\Application(); $app->get('/', function() { return "Hello world!"; });

Slide 29

Slide 29 text

Phar require_once __DIR__.'/silex.phar'; $app = new Silex\Application(); $app->get('/', function() { return "Hello world!"; });

Slide 30

Slide 30 text

Application require_once __DIR__.'/silex.phar'; $app = new Silex\Application(); $app->get('/', function() { return "Hello world!"; });

Slide 31

Slide 31 text

require_once __DIR__.'/silex.phar'; $app = new Silex\Application(); $app->get('/', function() { return "Hello world!"; }); Controller

Slide 32

Slide 32 text

$app->run();

Slide 33

Slide 33 text

No content

Slide 34

Slide 34 text

RewriteEngine On RewriteBase /some/path RewriteCond %{REQUEST_FILENAME} !-f RewriteCond %{REQUEST_FILENAME} !-d RewriteRule ^(.*)$ index.php [QSA,L]

Slide 35

Slide 35 text

server { location / { if (-f $request_filename) { break; } rewrite ^(.*) /index.php last; } location ~ index\.php$ { fastcgi_pass /var/run/php5-fpm.sock; fastcgi_index index.php; include fastcgi_params; } }

Slide 36

Slide 36 text

Wait a minute!

Slide 37

Slide 37 text

Lambdas λ

Slide 38

Slide 38 text

$(function () { $("a").click(function (event) { alert("Thanks for visiting!"); }); });

Slide 39

Slide 39 text

PHP 5.3

Slide 40

Slide 40 text

$f = function ($a, $b) { return $a + $b; }; $f(1, 2);

Slide 41

Slide 41 text

lazy $f = function () { exit; };

Slide 42

Slide 42 text

nested $f = function () { return function () { return true; }; }; $g = $f(); $value = $g();

Slide 43

Slide 43 text

scope $outer = 'world'; $f = function () use ($outer) { $inner = 'hello'; return "$inner $outer"; }; => "hello world"

Slide 44

Slide 44 text

scope $helloWorld = function () { $outer = 'world'; $f = function () use ($outer) { $inner = 'hello'; return "$inner $outer"; }; return $f(); }

Slide 45

Slide 45 text

passing $output = function ($info) { echo $info."\n"; }; $doStuff = function ($output) { $output('doing some magic'); doMagic(); $output('did some magic'); };

Slide 46

Slide 46 text

factory $userFactory = function ($name) { return new User($name); }; // ... $user = $userFactory($_POST['name']);

Slide 47

Slide 47 text

Usage

Slide 48

Slide 48 text

$app->get('/', function () { return "Hello world!"; });

Slide 49

Slide 49 text

Dynamic Routing

Slide 50

Slide 50 text

$app->get('/hello/{name}', function ($name) use ($app) { return "Hello ".$app->escape($name); });

Slide 51

Slide 51 text

$app->get('/hello/{name}', function ($name) use ($app) { return "Hello ".$app->escape($name); });

Slide 52

Slide 52 text

Controllers

Slide 53

Slide 53 text

assert $app->get('/blog/{id}', function ($id) { ... }) ->assert('id', '\d+');

Slide 54

Slide 54 text

value $app->get('/{page}', function ($page) { ... }) ->value('page', 'index');

Slide 55

Slide 55 text

bind $app->get('/', function () { ... }) ->bind('homepage'); $app['url_generator']->generate('homepage')

Slide 56

Slide 56 text

bind $app->get('/blog/{id}', function ($id) { ... }) ->bind('blog_post'); $app['url_generator'] ->generate('blog_post', array('id' => $id))

Slide 57

Slide 57 text

convert $app->get('/blog/{post}', function (Post $post) { ... }) ->convert('post', function ($post) use ($app) { $id = (int) $post; return $app['posts']->find($id); });

Slide 58

Slide 58 text

Before & After

Slide 59

Slide 59 text

$app->before(function () { ... }); $app->get('/', function () { ... }); $app->after(function () { ... });

Slide 60

Slide 60 text

$app->before(function (Request $request) { $loggedIn = $request ->getSession() ->get('logged_in'); if (!$loggedIn) { return new RedirectResponse('/login'); } });

Slide 61

Slide 61 text

$app->after(function (Request $request, Response $response) { // tweak the Response });

Slide 62

Slide 62 text

$app->after(function ( Request $request, Response $response, ) use ($app) { $response->headers->set('x-csrf-token', $app['csrf_token']); });

Slide 63

Slide 63 text

REST

Slide 64

Slide 64 text

• get • post • put • delete • head • options • patch

Slide 65

Slide 65 text

$app->get('/posts/{id}', ...); $app->post('/posts', ...); $app->put('/posts/{id}', ...); $app->delete('/post/{id}', ...);

Slide 66

Slide 66 text

use Symfony\Component\HttpFoundation\Request; use Symfony\Component\HttpFoundation\Response; $app->post('/message', function (Request $request) { mail( '[email protected]', 'New message', $request->get('body') ); return new Response('Email has been sent!', 201); });

Slide 67

Slide 67 text

Caching

Slide 68

Slide 68 text

Error Handling

Slide 69

Slide 69 text

use Symfony\Component\HttpFoundation\Response; $app->error(function (\Exception $e, $code) { return new Response('Whoops!', $code); });

Slide 70

Slide 70 text

use Symfony\Component\HttpKernel\Exception\NotFoundHttpException; throw new NotFoundHttpException("Could not find what you were looking for.");

Slide 71

Slide 71 text

$app->abort(404, "Could not find the thing.");

Slide 72

Slide 72 text

$app['debug'] = true;

Slide 73

Slide 73 text

Redirecting

Slide 74

Slide 74 text

$app->get('/', function () use ($app) { return $app->redirect('/hello'); });

Slide 75

Slide 75 text

Pimple

Slide 76

Slide 76 text

50 NCLOC

Slide 77

Slide 77 text

Symfony2 DIC Pimple

Slide 78

Slide 78 text

Symfony2 DIC Pimple Container

Slide 79

Slide 79 text

Symfony2 DIC Pimple Container Builder

Slide 80

Slide 80 text

Symfony2 DIC Pimple Container Builder Extension

Slide 81

Slide 81 text

Symfony2 DIC Pimple Container Builder Extension Loader

Slide 82

Slide 82 text

Symfony2 DIC Pimple Container Builder Extension XML/Yaml Loader

Slide 83

Slide 83 text

Symfony2 DIC Pimple Container Builder Extension XML/Yaml Compiler Loader

Slide 84

Slide 84 text

Symfony2 DIC Pimple Container Builder Extension XML/Yaml Compiler Loader Container

Slide 85

Slide 85 text

Symfony2 DIC Pimple Container Builder Extension XML/Yaml Compiler Loader Container ServiceProvider ( )

Slide 86

Slide 86 text

$container = new Pimple();

Slide 87

Slide 87 text

$app = new Silex\Application();

Slide 88

Slide 88 text

Parameters $app['some_parameter'] = 'value'; $app['asset.host'] = 'http://cdn.mysite.com/'; $app['database.dsn'] = 'mysql:dbname=myapp';

Slide 89

Slide 89 text

Services $app['some_service'] = function () { return new Service(); };

Slide 90

Slide 90 text

$service = $app['some_service'];

Slide 91

Slide 91 text

Dependencies $app['some_service'] = function ($app) { return new Service( $app['some_other_service'], $app['some_service.config'] ); };

Slide 92

Slide 92 text

Shared $app['some_service'] = $app->share(function () { return new Service(); });

Slide 93

Slide 93 text

Protected $app['lambda_parameter'] = $app->protect( function ($a, $b) { return $a + $b; }); // will not execute the lambda $add = $app['lambda_parameter']; // calling it now echo $add(2, 3);

Slide 94

Slide 94 text

Exposed Services • debug • request • autoloader • routes • controllers • dispatcher • resolver • kernel

Slide 95

Slide 95 text

Service Providers

Slide 96

Slide 96 text

interface ServiceProviderInterface { function register(Application $app); }

Slide 97

Slide 97 text

Core Service Providers • doctrine • form • http cache • monolog • session • swiftmailer • symfony bridges • translation • twig • url generator • validator

Slide 98

Slide 98 text

Twig

Slide 99

Slide 99 text

$app->register( new Silex\ServiceProvider\TwigServiceProvider(), array( 'twig.path' => __DIR__.'/views', 'twig.class_path' => __DIR__.'/vendor/twig/lib', ) );

Slide 100

Slide 100 text

$app->get('/', function () use ($app) { return $app['twig']->render('hello.twig'); });

Slide 101

Slide 101 text

3rd Party • doctrine orm • pomm (postgres) • predis • mongo • KyotoTycoon • memcache • rest • markdown • gravatar • buzz • config • solr • profiler • ...

Slide 102

Slide 102 text

Functional Testing

Slide 103

Slide 103 text

• src • app.php • web • index.php • tests • bootstrap.php • YourTest.php

Slide 104

Slide 104 text

src/app.php require_once __DIR__.'/../vendor/silex.phar'; ... return $app;

Slide 105

Slide 105 text

web/index.php $app = require_once __DIR__.'/../src/app.php'; $app->run();

Slide 106

Slide 106 text

tests/bootstrap.php require_once __DIR__.'/../vendor/silex.phar';

Slide 107

Slide 107 text

use Silex\WebTestCase; class YourTest extends WebTestCase { public function createApp() { return require __DIR__.'/../src/app.php'; } // tests... } tests/YourTest.php

Slide 108

Slide 108 text

public function testAbout() { $client = $this->createClient(); $client->request('GET', '/about'); $response = $client->getResponse(); $this->assertTrue($response->isOk()); $this->assertContains('trashbin', $response->getContent()); $this->assertContains('github', $response->getContent()); $this->assertContains('igorw', $response->getContent()); }

Slide 109

Slide 109 text

phpunit.xml.dist ./tests/

Slide 110

Slide 110 text

$ phpunit

Slide 111

Slide 111 text

No content

Slide 112

Slide 112 text

No content

Slide 113

Slide 113 text

• smallish sites • well-defined scope • prototyping • restful apis When to use

Slide 114

Slide 114 text

and many more...

Slide 115

Slide 115 text

The future

Slide 116

Slide 116 text

The future • Cookbooks

Slide 117

Slide 117 text

The future • Cookbooks • Best practices

Slide 118

Slide 118 text

The future • Cookbooks • Best practices • Symfony2 integration

Slide 119

Slide 119 text

The future • Cookbooks • Best practices • Symfony2 integration • FOSUserBundle

Slide 120

Slide 120 text

The future • Cookbooks • Best practices • Symfony2 integration • FOSUserBundle • Composer

Slide 121

Slide 121 text

on github fabpot/Silex fabpot/Pimple

Slide 122

Slide 122 text

silex.sensiolabs.org

Slide 123

Slide 123 text

Ω

Slide 124

Slide 124 text

Questions? joind.in/3699 @igorwesome speakerdeck.com /u/igorw