Slide 1

Slide 1 text

No content

Slide 2

Slide 2 text

• phpBB • Symfony2 • Silex • Composer igorw @igorwesome

Slide 3

Slide 3 text

No content

Slide 4

Slide 4 text

$s=stream_socket_server('tcp://127.0.0.1:81'); while($c=stream_socket_accept($s)) fwrite($c,"HTTP/1.1 200 OK\r\nContent-Length: 2\r\n\r\nHi");

Slide 5

Slide 5 text

require __DIR__.'/c.php'; if (!is_callable($c = @$_GET['c'] ?: function() { echo 'Woah!'; })) throw new Exception('Error'); $c();

Slide 6

Slide 6 text

Silex Symfony2 μ-framework

Slide 7

Slide 7 text

Silex is not Symfony

Slide 8

Slide 8 text

Microframework

Slide 9

Slide 9 text

No content

Slide 10

Slide 10 text

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

Slide 11

Slide 11 text

Why?

Slide 12

Slide 12 text

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

Slide 13

Slide 13 text

simple

Slide 14

Slide 14 text

What makes silex special?

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

• concise • extensible • testable

Slide 19

Slide 19 text

• concise • extensible • testable

Slide 20

Slide 20 text

Http Kernel Interface

Slide 21

Slide 21 text

Response handle(Request $request)

Slide 22

Slide 22 text

client

Slide 23

Slide 23 text

request client

Slide 24

Slide 24 text

reponse client request

Slide 25

Slide 25 text

clean

Slide 26

Slide 26 text

PSR-0

Slide 27

Slide 27 text

No content

Slide 28

Slide 28 text

Silex is not Symfony

Slide 29

Slide 29 text

Silex is a user interface for Symfony

Slide 30

Slide 30 text

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

Slide 31

Slide 31 text

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

Slide 32

Slide 32 text

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

Slide 33

Slide 33 text

Composer require_once 'vendor/autoload.php'; $app = new Silex\Application(); $app->get('/', function() { return "Hello world!"; });

Slide 34

Slide 34 text

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

Slide 35

Slide 35 text

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

Slide 36

Slide 36 text

$app->run();

Slide 37

Slide 37 text

No content

Slide 38

Slide 38 text

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

Slide 39

Slide 39 text

server { index index.php location / { try_files $uri $uri/ /index.php; } location ~ index\.php$ { fastcgi_pass /var/run/php5-fpm.sock; fastcgi_index index.php; include fastcgi_params; } }

Slide 40

Slide 40 text

Wait a minute!

Slide 41

Slide 41 text

Lambdas λ

Slide 42

Slide 42 text

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

Slide 43

Slide 43 text

PHP 5.3

Slide 44

Slide 44 text

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

Slide 45

Slide 45 text

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

Slide 46

Slide 46 text

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

Slide 47

Slide 47 text

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

Slide 48

Slide 48 text

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

Slide 49

Slide 49 text

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

Slide 50

Slide 50 text

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

Slide 51

Slide 51 text

event listener $emitter = new EventEmitter(); $emitter->on('user.create', function (User $user) { $msg = sprintf("User %s created.", $user->name); log($msg); });

Slide 52

Slide 52 text

Usage

Slide 53

Slide 53 text

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

Slide 54

Slide 54 text

Dynamic Routing

Slide 55

Slide 55 text

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

Slide 56

Slide 56 text

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

Slide 57

Slide 57 text

Controllers

Slide 58

Slide 58 text

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

Slide 59

Slide 59 text

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

Slide 60

Slide 60 text

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

Slide 61

Slide 61 text

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

Slide 62

Slide 62 text

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

Slide 63

Slide 63 text

Before & After

Slide 64

Slide 64 text

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

Slide 65

Slide 65 text

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

Slide 66

Slide 66 text

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

Slide 67

Slide 67 text

Methods

Slide 68

Slide 68 text

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

Slide 69

Slide 69 text

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

Slide 70

Slide 70 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 71

Slide 71 text

Caching

Slide 72

Slide 72 text

Error Handling

Slide 73

Slide 73 text

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

Slide 74

Slide 74 text

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

Slide 75

Slide 75 text

$app['debug'] = true;

Slide 76

Slide 76 text

Redirecting

Slide 77

Slide 77 text

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

Slide 78

Slide 78 text

Pimple

Slide 79

Slide 79 text

~50 NCLOC

Slide 80

Slide 80 text

$container = new Pimple();

Slide 81

Slide 81 text

$app = new Silex\Application();

Slide 82

Slide 82 text

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

Slide 83

Slide 83 text

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

Slide 84

Slide 84 text

$service = $app['some_service'];

Slide 85

Slide 85 text

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

Slide 86

Slide 86 text

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

Slide 87

Slide 87 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 88

Slide 88 text

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

Slide 89

Slide 89 text

Service Providers

Slide 90

Slide 90 text

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

Slide 91

Slide 91 text

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

Slide 92

Slide 92 text

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

Slide 93

Slide 93 text

Redis

Slide 94

Slide 94 text

$app->register(new PredisServiceProvider());

Slide 95

Slide 95 text

$app->get('/{id}', function ($id) use ($app) { $key = 'pastes:'.$id; $paste = $app['predis']->hgetall($key); ... });

Slide 96

Slide 96 text

No content

Slide 97

Slide 97 text

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

Slide 98

Slide 98 text

and many more...

Slide 99

Slide 99 text

No content

Slide 100

Slide 100 text

New feature: Streaming

Slide 101

Slide 101 text

$app->get('/some-video', function () use ($app) { $file = $app['app.video_filename']; $stream = function () use ($file) { readfile($file); }; return $app->stream( $stream, 200, array('Content-Type' => 'video/mpeg') ); });

Slide 102

Slide 102 text

New feature: Middlewares

Slide 103

Slide 103 text

$mustBeLogged = function (Request $request) use ($app) { if (!$app['session']->has('userId')) { return $app->redirect('/user/login'); } }; $app->get('/user/my-profile', function () { ... }) ->middleware($mustBeLogged);

Slide 104

Slide 104 text

New feature: JSON helper

Slide 105

Slide 105 text

$app->get('/users/{id}', function ($id) use ($app) { $user = getUser($id); if (!$user) { $message = 'The user was not found.'; $error = array('message' => $message); return $app->json($error, 404); } return $app->json($user); });

Slide 106

Slide 106 text

New feature: Finish filter

Slide 107

Slide 107 text

$app->finish(function () { // send e-mails // very slow query // some slow API calls });

Slide 108

Slide 108 text

New feature: Composer

Slide 109

Slide 109 text

No content

Slide 110

Slide 110 text

{ "minimum-stability": "dev", "require": { "silex/silex": "1.0.*@dev" } } composer.json

Slide 111

Slide 111 text

$ php composer.phar install

Slide 112

Slide 112 text

require 'vendor/autoload.php';

Slide 113

Slide 113 text

on github fabpot/Silex fabpot/Pimple

Slide 114

Slide 114 text

silex.sensiolabs.org

Slide 115

Slide 115 text

Ω

Slide 116

Slide 116 text

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