Upgrade to Pro
— share decks privately, control downloads, hide ads and more …
Speaker Deck
Features
Speaker Deck
PRO
Sign in
Sign up for free
Search
Search
Silex (confoo)
Search
Igor Wiedler
March 02, 2012
Programming
11
1.7k
Silex (confoo)
Igor Wiedler
March 02, 2012
Tweet
Share
More Decks by Igor Wiedler
See All by Igor Wiedler
Redis Bedtime Stories
igorw
1
240
Wide Event Analytics (LISA19)
igorw
4
890
a day in the life of a request
igorw
0
100
production: an owner's manual
igorw
0
130
The Power of 2
igorw
0
230
LISP 1.5 Programmer's Manual: A Dramatic Reading
igorw
0
360
The Moral Character of Software
igorw
1
240
interdisciplinary computing (domcode)
igorw
0
250
miniKanren (clojure berlin)
igorw
1
240
Other Decks in Programming
See All in Programming
Outline View in SwiftUI
1024jp
1
330
距離関数を極める! / SESSIONS 2024
gam0022
0
280
cmp.Or に感動した
otakakot
2
140
Macとオーディオ再生 2024/11/02
yusukeito
0
370
A Journey of Contribution and Collaboration in Open Source
ivargrimstad
0
900
Pinia Colada が実現するスマートな非同期処理
naokihaba
4
220
Streams APIとTCPフロー制御 / Web Streams API and TCP flow control
tasshi
2
350
AWS IaCの注目アップデート 2024年10月版
konokenj
3
3.3k
Better Code Design in PHP
afilina
PRO
0
120
3 Effective Rules for Using Signals in Angular
manfredsteyer
PRO
1
100
Why Jakarta EE Matters to Spring - and Vice Versa
ivargrimstad
0
1.1k
ECS Service Connectのこれまでのアップデートと今後のRoadmapを見てみる
tkikuc
2
250
Featured
See All Featured
5 minutes of I Can Smell Your CMS
philhawksworth
202
19k
RailsConf & Balkan Ruby 2019: The Past, Present, and Future of Rails at GitHub
eileencodes
131
33k
Writing Fast Ruby
sferik
627
61k
Practical Orchestrator
shlominoach
186
10k
Visualization
eitanlees
145
15k
We Have a Design System, Now What?
morganepeng
50
7.2k
Being A Developer After 40
akosma
86
590k
Practical Tips for Bootstrapping Information Extraction Pipelines
honnibal
PRO
10
720
Teambox: Starting and Learning
jrom
133
8.8k
jQuery: Nuts, Bolts and Bling
dougneiner
61
7.5k
Docker and Python
trallard
40
3.1k
Side Projects
sachag
452
42k
Transcript
Wednesday, February 29, 12
• phpBB • Symfony2 • Silex • Composer igorw @igorwesome
Wednesday, February 29, 12
Wednesday, February 29, 12
Silex Symfony2 μ-framework Wednesday, February 29, 12
Silex is not Symfony Wednesday, February 29, 12
Microframework Wednesday, February 29, 12
Wednesday, February 29, 12
What? • Bare bones • Routes mapped to controllers •
The ‘C’ of ‘MVC’ • REST • Single file app Wednesday, February 29, 12
Why? Wednesday, February 29, 12
Sometimes a full-stack framework is too much for a simple
task. Wednesday, February 29, 12
simple Wednesday, February 29, 12
What makes silex special? Wednesday, February 29, 12
• concise • extensible • testable Wednesday, February 29, 12
• concise • extensible • testable Wednesday, February 29, 12
• concise • extensible • testable Wednesday, February 29, 12
• concise • extensible • testable Wednesday, February 29, 12
• concise • extensible • testable Wednesday, February 29, 12
Http Kernel Interface Wednesday, February 29, 12
Response handle(Request $request) Wednesday, February 29, 12
client Wednesday, February 29, 12
request client Wednesday, February 29, 12
reponse client request Wednesday, February 29, 12
clean Wednesday, February 29, 12
PSR-0 Wednesday, February 29, 12
Wednesday, February 29, 12
Silex is not Symfony Wednesday, February 29, 12
Silex is a user interface for Symfony Wednesday, February 29,
12
require_once __DIR__.'/silex.phar'; $app = new Silex\Application(); $app->get('/', function() { return
"Hello world!"; }); Wednesday, February 29, 12
Phar require_once __DIR__.'/silex.phar'; $app = new Silex\Application(); $app->get('/', function() {
return "Hello world!"; }); Wednesday, February 29, 12
Application require_once __DIR__.'/silex.phar'; $app = new Silex\Application(); $app->get('/', function() {
return "Hello world!"; }); Wednesday, February 29, 12
require_once __DIR__.'/silex.phar'; $app = new Silex\Application(); $app->get('/', function() { return
"Hello world!"; }); Controller Wednesday, February 29, 12
$app->run(); Wednesday, February 29, 12
Wednesday, February 29, 12
<IfModule mod_rewrite.c> RewriteEngine On RewriteBase /some/path RewriteCond %{REQUEST_FILENAME} !-f RewriteCond
%{REQUEST_FILENAME} !-d RewriteRule ^(.*)$ index.php [QSA,L] </IfModule> Wednesday, February 29, 12
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; } } Wednesday, February 29, 12
Wait a minute! Wednesday, February 29, 12
Lambdas λ Wednesday, February 29, 12
$(function () { $("a").on("click", function (event) { alert("Thanks for visiting!");
}); }); Wednesday, February 29, 12
PHP 5.3 Wednesday, February 29, 12
$f = function ($a, $b) { return $a + $b;
}; $f(1, 2); Wednesday, February 29, 12
lazy $f = function () { exit; }; Wednesday, February
29, 12
nested $f = function () { return function () {
return true; }; }; $g = $f(); $value = $g(); Wednesday, February 29, 12
scope $outer = 'world'; $f = function () use ($outer)
{ $inner = 'hello'; return "$inner $outer"; }; => "hello world" Wednesday, February 29, 12
scope $helloWorld = function () { $outer = 'world'; $f
= function () use ($outer) { $inner = 'hello'; return "$inner $outer"; }; return $f(); } Wednesday, February 29, 12
passing $output = function ($info) { echo $info."\n"; }; $doStuff
= function ($output) { $output('doing some magic'); doMagic(); $output('did some magic'); }; Wednesday, February 29, 12
factory $userFactory = function ($name) { return new User($name); };
// ... $user = $userFactory($_POST['name']); Wednesday, February 29, 12
event listener $emitter = new EventEmitter(); $emitter->on('user.create', function (User $user)
{ $msg = sprintf("User %s created.", $user->name); log($msg); }); Wednesday, February 29, 12
Usage Wednesday, February 29, 12
$app->get('/', function () { return "Hello world!"; }); Wednesday, February
29, 12
Dynamic Routing Wednesday, February 29, 12
$app->get('/hello/{name}', function ($name) use ($app) { return "Hello ".$app->escape($name); });
Wednesday, February 29, 12
$app->get('/hello/{name}', function ($name) use ($app) { return "Hello ".$app->escape($name); });
Wednesday, February 29, 12
Controllers Wednesday, February 29, 12
assert $app->get('/blog/{id}', function ($id) { ... }) ->assert('id', '\d+'); Wednesday,
February 29, 12
value $app->get('/{page}', function ($page) { ... }) ->value('page', 'index'); Wednesday,
February 29, 12
bind $app->get('/', function () { ... }) ->bind('homepage'); $app['url_generator']->generate('homepage') Wednesday,
February 29, 12
bind $app->get('/blog/{id}', function ($id) { ... }) ->bind('blog.post'); $app['url_generator'] ->generate('blog.post',
array('id' => $id)) Wednesday, February 29, 12
convert $app->get('/blog/{post}', function (Post $post) { ... }) ->convert('post', function
($post) use ($app) { $id = (int) $post; return $app['posts']->find($id); }); Wednesday, February 29, 12
Before & After Wednesday, February 29, 12
$app->before(function () { ... }); $app->get('/', function () { ...
}); $app->after(function () { ... }); Wednesday, February 29, 12
$app->before(function (Request $request) { $loggedIn = $request ->getSession() ->get('logged_in'); if
(!$loggedIn) { return new RedirectResponse('/login'); } }); Wednesday, February 29, 12
$app->after(function (Request $request, Response $response) { // tweak the Response
}); Wednesday, February 29, 12
Methods Wednesday, February 29, 12
• get • post • put • delete • head
• options Wednesday, February 29, 12
$app->get('/posts/{id}', ...); $app->post('/posts', ...); $app->put('/posts/{id}', ...); $app->delete('/post/{id}', ...); Wednesday, February
29, 12
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); }); Wednesday, February 29, 12
Caching Wednesday, February 29, 12
Error Handling Wednesday, February 29, 12
use Symfony\Component\HttpFoundation\Response; $app->error(function (\Exception $e, $code) { return new Response('Whoops!',
$code); }); Wednesday, February 29, 12
$app->abort(404, "Could not find the thing."); Wednesday, February 29, 12
$app['debug'] = true; Wednesday, February 29, 12
Redirecting Wednesday, February 29, 12
$app->get('/', function () use ($app) { return $app->redirect('/hello'); }); Wednesday,
February 29, 12
Pimple Wednesday, February 29, 12
50 NCLOC Wednesday, February 29, 12
$container = new Pimple(); Wednesday, February 29, 12
$app = new Silex\Application(); Wednesday, February 29, 12
Parameters $app['some_parameter'] = 'value'; $app['asset.host'] = 'http://cdn.mysite.com/'; $app['database.dsn'] = 'mysql:dbname=myapp';
Wednesday, February 29, 12
Services $app['some_service'] = function () { return new Service(); };
Wednesday, February 29, 12
$service = $app['some_service']; Wednesday, February 29, 12
Dependencies $app['some_service'] = function ($app) { return new Service( $app['some_other_service'],
$app['some_service.config'] ); }; Wednesday, February 29, 12
Shared $app['some_service'] = $app->share(function () { return new Service(); });
Wednesday, February 29, 12
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); Wednesday, February 29, 12
Exposed Services • debug • request • autoloader • routes
• controllers • dispatcher • resolver • kernel Wednesday, February 29, 12
Service Providers Wednesday, February 29, 12
interface ServiceProviderInterface { function register(Application $app); } Wednesday, February 29,
12
Core Service Providers • doctrine • form • http cache
• monolog • session • swiftmailer • symfony bridges • translation • twig • url generator • validator Wednesday, February 29, 12
3rd Party • doctrine orm • pomm (postgres) • predis
• mongo • KyotoTycoon • memcache • rest • markdown • gravatar • buzz • config • solr • profiler • ... Wednesday, February 29, 12
Redis Wednesday, February 29, 12
$app->register(new PredisServiceProvider()); Wednesday, February 29, 12
$app->get('/{id}', function ($id) use ($app) { $key = 'pastes:'.$id; $paste
= $app['predis']->hgetall($key); ... }); Wednesday, February 29, 12
Wednesday, February 29, 12
• smallish sites • well-defined scope • prototyping • restful
apis When to use Wednesday, February 29, 12
and many more... Wednesday, February 29, 12
Wednesday, February 29, 12
New feature: Composer Wednesday, February 29, 12
Wednesday, February 29, 12
{ "require": { "silex/silex": "1.0.*" } } composer.json Wednesday, February
29, 12
$ php composer.phar install Wednesday, February 29, 12
require 'vendor/.composer/autoload.php'; Wednesday, February 29, 12
New feature: Streaming Wednesday, February 29, 12
$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') ); }); Wednesday, February 29, 12
on github fabpot/Silex fabpot/Pimple Wednesday, February 29, 12
silex.sensiolabs.org Wednesday, February 29, 12
Ω Wednesday, February 29, 12
Questions? joind.in/5971 @igorwesome speakerdeck.com /u/igorw Wednesday, February 29, 12