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 - Talk is cheap, show me the c̶o̶d̶e̶ pro...
Search
Jean Pimentel
June 25, 2012
Programming
3
300
Silex - Talk is cheap, show me the c̶o̶d̶e̶ prototype!
Por que prototipar?
O que são microframeworks?
Silex, microframework PHP.
Como usar o Silex?
Jean Pimentel
June 25, 2012
Tweet
Share
More Decks by Jean Pimentel
See All by Jean Pimentel
Introdução ao Flutter
jeanpimentel
0
74
Clean Code - Como não escrever um código digno de "WTF?!"
jeanpimentel
0
54
Acessibilidade no Android - 101 - Google IO Extended 2017
jeanpimentel
0
150
DiffUtil
jeanpimentel
0
87
Recursos do iOS que talvez você não conheça!
jeanpimentel
2
260
Apps realtime com Firebase
jeanpimentel
0
65
A Linguagem Swift
jeanpimentel
0
42
Other Decks in Programming
See All in Programming
NSOutlineView何もわからん:( 前編 / I Don't Understand About NSOutlineView :( Pt. 1
usagimaru
0
350
イベント駆動で成長して委員会
happymana
1
340
WebフロントエンドにおけるGraphQL(あるいはバックエンドのAPI)との向き合い方 / #241106_plk_frontend
izumin5210
4
1.4k
Amazon Qを使ってIaCを触ろう!
maruto
0
420
3rd party scriptでもReactを使いたい! Preact + Reactのハイブリッド開発
righttouch
PRO
1
610
よくできたテンプレート言語として TypeScript + JSX を利用する試み / Using TypeScript + JSX outside of Web Frontend #TSKaigiKansai
izumin5210
7
1.8k
Remix on Hono on Cloudflare Workers
yusukebe
1
310
카카오페이는 어떻게 수천만 결제를 처리할까? 우아한 결제 분산락 노하우
kakao
PRO
0
110
2024/11/8 関西Kaggler会 2024 #3 / Kaggle Kernel で Gemma 2 × vLLM を動かす。
kohecchi
5
970
WebAssembly Unleashed: Powering Server-Side Applications
chrisft25
0
150
광고 소재 심사 과정에 AI를 도입하여 광고 서비스 생산성 향상시키기
kakao
PRO
0
180
3 Effective Rules for Using Signals in Angular
manfredsteyer
PRO
0
130
Featured
See All Featured
Learning to Love Humans: Emotional Interface Design
aarron
273
40k
We Have a Design System, Now What?
morganepeng
50
7.2k
VelocityConf: Rendering Performance Case Studies
addyosmani
325
24k
Building an army of robots
kneath
302
43k
Facilitating Awesome Meetings
lara
50
6.1k
For a Future-Friendly Web
brad_frost
175
9.4k
Being A Developer After 40
akosma
87
590k
実際に使うSQLの書き方 徹底解説 / pgcon21j-tutorial
soudai
169
50k
4 Signs Your Business is Dying
shpigford
180
21k
"I'm Feeling Lucky" - Building Great Search Experiences for Today's Users (#IAC19)
danielanewman
226
22k
GraphQLとの向き合い方2022年版
quramy
43
13k
A designer walks into a library…
pauljervisheath
204
24k
Transcript
None
JEAN PIMENTEL Ciência da Computação – UFJF Analista de Sistemas
– Bolt Brasil @jeanpimentel
IDEIAS NÃO VALEM NADA!
Etapas pra produzir sua ideia: 1. Ideia 2. Documentação 3.
Mockups 4. Protótipos 5. Vídeos
POR QUE PROTOTIPAR?
None
"IT'S ONE THING TO TALK ABOUT THEM AND HAVE STORYBOARDS
AND ANOTHER THING TO SEE THEM FOR REAL.“ ROBERT HOEKMAN, JR.
None
POR MELHOR QUE VOCÊ SEJA, SEMPRE COMETERÁ ERROS.
VOCÊ NÃO PRECISA FAZER TUDO, SOMENTE O PRINCIPAL.
TERMINE O PROTÓTIPO EM 1 DIA.
ITERAÇÕES: REESTUDE O PROBLEMA, REPENSE A SOLUÇÃO.
NÃO PERCA TEMPO COM O MELHOR CÓDIGO, ELE É DESCARTÁVEL
E SOFRERÁ MUITAS ALTERAÇÕES.
NÃO PERCA TEMPO COM O MELHOR CÓDIGO, ELE É DESCARTÁVEL
E SOFRERÁ MUITAS ALTERAÇÕES. NÃO PRECISA FUNCIONAR NO IE6, NEM TER UM BOM HTML. PODE ATÉ USAR TABELA.
MICROFRAMEWORK
MICROFRAMEWORK • Esqueleto da aplicação • Controllers (MVC) – Rotas
Ações – Requests Responses • Simples
MICROFRAMEWORK • Ruby Sinatra, Cuba • Python Flask,
Pyramid • PHP Silex, Slim, Flight • Java Napalm
SILEX
SILEX • Não é o Symfony2 • PHP 5.3 •
PSR-0 • PSR-1 (?) • PSR-2 (?) • Conciso, extensível e testável
COMO USAR
1 { 2 "require": { 3 "php": ">=5.3.2", 4 "silex/silex":
"*" 5 } 6 } 1 curl -s http://getcomposer.org/installer | php 2 php composer.phar install composer.json shell
1 <?php 2 3 // COMPOSER 4 require_once __DIR__.'/../vendor/autoload.php'; 5
6 $app = new Silex\Application(); 7 8 // SEUS MÉTODOS AQUI 9 10 $app->run(); public/index.php
SERVICE PROVIDERS
1 <?php 2 3 // COMPOSER 4 require_once __DIR__.'/../vendor/autoload.php'; 5
6 $app = new Silex\Application(); 7 $app->register(new Silex\Provider\SessionServiceProvider()); 8 $app->register(new Silex\Provider\TwigServiceProvider(), array( 9 'twig.path' => __DIR__ . '/../views', 10 )); 11 12 // SEUS MÉTODOS AQUI 13 14 $app->run();
SERVICES PROVIDERS • Validator • Form • Monolog • Doctrine
• Translation • E você pode fazer o seu. Basta implementar uma interface.
ROUTING
1 <?php 2 3 // COMPOSER 4 require_once __DIR__.'/../vendor/autoload.php'; 5
6 $app = new Silex\Application(); 7 $app->register(new Silex\Provider\SessionServiceProvider()); 8 $app->register(new Silex\Provider\TwigServiceProvider(), array( 9 'twig.path' => __DIR__ . '/../views', 10 )); 11 12 $app->get('/posts', function () { 13 $posts = /* Minha query */ 14 15 $output = ''; 16 foreach ($posts as $post) 17 $output .= $post['title'] . '<br />'; 18 19 return $output; 20 }); 21 22 $app->run();
1 <?php 2 3 // COMPOSER 4 require_once __DIR__.'/../vendor/autoload.php'; 5
6 $app = new Silex\Application(); 7 $app->register(new Silex\Provider\SessionServiceProvider()); 8 $app->register(new Silex\Provider\TwigServiceProvider(), array( 9 'twig.path' => __DIR__ . '/../views', 10 )); 11 12 $app->get('/posts', function () use ($app) { 13 $posts = /* Minha query */ 14 return $app['twig']->render('blog.twig', array('posts' => $posts)); 15 }); 16 17 $app->run(); 1 {% if posts %} 2 <ul> 3 {% for post in posts %} 4 <li>{{ post.title }}<li> 5 {% endfor %} 6 </ul> 7 {% endif %} UTILIZANDO O TWIG views/blog.twig
1 <?php 2 3 // COMPOSER 4 require_once __DIR__.'/../vendor/autoload.php'; 5
6 $app = new Silex\Application(); 7 $app->register(new Silex\Provider\SessionServiceProvider()); 8 $app->register(new Silex\Provider\TwigServiceProvider(), array( 9 'twig.path' => __DIR__ . '/../views', 10 )); 11 12 $app->get('/posts', function () use ($app) { 13 $posts = /* Minha query */ 14 return $app['twig']->render('blog.twig', array('posts' => $posts)); 15 }); 16 17 $app->get('/post/{id}', function ($id) use ($app) { 18 $post = /* Minha query buscando o post */ 19 return $app['twig']->render('post.twig', array('post' => $post)); 20 }) 21 ->assert('id', '\d+'); 22 23 $app->run(); UTILIZANDO PARÂMETROS
1 <?php 2 3 // COMPOSER 4 require_once __DIR__.'/../vendor/autoload.php'; 5
6 $app = new Silex\Application(); 7 $app->register(new Silex\Provider\SessionServiceProvider()); 8 $app->register(new Silex\Provider\TwigServiceProvider(), array( 9 'twig.path' => __DIR__ . '/../views', 10 )); 11 12 $app->get('/posts', function () use ($app) { 13 $posts = /* Minha query */ 14 return $app['twig']->render('blog.twig', array('posts' => $posts)); 15 }); 16 17 $app->get('/post/{id}', function ($id) use ($app) { 18 return $app['twig']->render('post.twig', array('post' => $id)); 19 }) 20 ->convert('id', function ($id) { return /* Minha query buscando post */ }); 21 22 $app->run(); CONVERTENDO O PARÂMETRO
1 <?php 2 3 // COMPOSER 4 require_once __DIR__.'/../vendor/autoload.php'; 5
6 $app = new Silex\Application(); 7 $app->register(new Silex\Provider\SessionServiceProvider()); 8 $app->register(new Silex\Provider\TwigServiceProvider(), array( 9 'twig.path' => __DIR__ . '/../views', 10 )); 11 12 $postProvider = function ($id) { 13 return /* Minha query buscando post */ 14 }; 15 16 $app->get('/posts', function () use ($app) { 17 $posts = /* Minha query */ 18 return $app['twig']->render('blog.twig', array('posts' => $posts)); 19 }); 20 21 $app->get('/post/{id}', function ($id) use ($app) { 22 return $app['twig']->render('post.twig', array('post' => $id)); 23 }) 24 ->convert('id', $postProvider); 25 26 $app->run(); REUTILIZANDO FUNÇÕES
2 3 // COMPOSER 4 require_once __DIR__.'/../vendor/autoload.php'; 5 6 $app
= new Silex\Application(); 7 $app->register(new Silex\Provider\SessionServiceProvider()); 8 $app->register(new Silex\Provider\TwigServiceProvider(), array( 9 'twig.path' => __DIR__ . '/../views', 10 )); 11 12 $postProvider = function ($id) { 13 return /* Minha query buscando o post */ 14 }; 15 16 $app->get('/posts', function () use ($app) { 17 $posts = /* Minha query */ 18 return $app['twig']->render('blog.twig', array('posts' => $posts)); 19 }); 20 21 $app->get('/post/{id}', function ($id) use ($app) { 22 return $app['twig']->render('post.twig', array('post' => $id)); 23 }) 24 ->convert('id', $postProvider); 25 26 $app->get('/posts/categoria/{categoria}', function ($id) use ($app) { 27 $posts = /* Minha query por posts com categoria X */ 28 return $app['twig']->render('blog.twig', array('posts' => $posts)); 29 }) 30 ->value('categoria', 'php'); 31 32 $app->run(); PARÂMETROS COM VALOR DEFAULT
1 <?php 2 3 // COMPOSER 4 require_once __DIR__.'/../vendor/autoload.php'; 5
6 $app = new Silex\Application(); 7 $app->register(new Silex\Provider\SessionServiceProvider()); 8 $app->register(new Silex\Provider\TwigServiceProvider(), array( 9 'twig.path' => __DIR__ . '/../views', 10 )); 11 12 /* ... */ 13 14 $app->get('/contato', function () use ($app) { 15 return $app['twig']->render('contato.twig') 16 }); 17 18 $app->post('/contato', function (Request $request) use ($app) { 19 $contato = array( 20 'nome' => $request->get('nome'), 21 'email' => $request->get('email'), 22 'assunto' => $request->get('assunto'), 23 'mensagem' => $request->get('mensagem'), 24 ); 25 /* Minha query salvando o contato */ 26 return $app['twig']->render('contato-sucesso.twig') 27 }); 28 29 $app->run(); GET, POST, PUT, DELETE OU MATCH
ROUTE MIDDLEWARES
1 <?php 2 3 // COMPOSER 4 require_once __DIR__.'/../vendor/autoload.php'; 5
6 $app = new Silex\Application(); 7 $app->register(new Silex\Provider\SessionServiceProvider()); 8 $app->register(new Silex\Provider\TwigServiceProvider(), array( 9 'twig.path' => __DIR__ . '/../views', 10 )); 11 12 /* ... */ 13 14 $mustBeAnonymous = function (Request $request) use ($app) { 15 if ($app['session']->has('user')) 16 return $app->redirect('/logout'); 17 }; 18 19 $app->get('/signup', function () use ($app) { 20 return $app['twig']->render('signup.twig') 21 }) 22 ->before($mustBeAnonymous); 23 24 $app->run(); BEFORE ROUTE
2 3 // COMPOSER 4 require_once __DIR__.'/../vendor/autoload.php'; 5 6 $app
= new Silex\Application(); 7 $app->register(new Silex\Provider\SessionServiceProvider()); 8 $app->register(new Silex\Provider\TwigServiceProvider(), array( 9 'twig.path' => __DIR__ . '/../views', 10 )); 11 12 /* ... */ 13 14 $app->get('/meu-relatorio', function () use ($app) { 15 return $app['twig']->render('signup.twig') 16 }) 17 ->before(function (Request $request) use ($app) { 18 if (!$app['session']->has('user')) 19 return $app->redirect('/login'); 20 }) 21 ->before(function (Request $request) use ($app) { 22 $app['time.start'] = microtime(true) 23 }) 24 ->after(function (Request $request, Response $response) use ($app) { 25 $duracao = microtime(true) - $app['time.start']; 26 /* Minha função pra gravar em logs */ 27 }); 28 29 $app->run(); AFTER ROUTE
APPLICATION FILTERS
1 <?php 2 3 // COMPOSER 4 require_once __DIR__.'/../vendor/autoload.php'; 5
6 $app = new Silex\Application(); 7 $app->register(new Silex\Provider\SessionServiceProvider()); 8 $app->register(new Silex\Provider\TwigServiceProvider(), array( 9 'twig.path' => __DIR__ . '/../views', 10 )); 11 12 $app->before(function (Request $request) use ($app) { 13 // set up 14 $request->getSession()->start(); 15 $app['time.start'] = microtime(true); 16 }); 17 18 $app->after(function (Request $request, Response $response) use ($app) { 19 // tear down 20 $app['time.end'] = microtime(true); 21 }); 22 23 $app->finish(function (Request $request, Response $response) use ($app) { 24 // after response has been sent 25 $duracao = $app['time.end'] - $app['time.start']; 26 /* Minha função pra gravar em logs */ 27 }); 28 29 $app->run(); BEFORE, AFTER, FINISH
ERROR CONTROL
1 <?php 2 3 // COMPOSER 4 require_once __DIR__.'/../vendor/autoload.php'; 5
6 $app = new Silex\Application(); 7 $app->register(new Silex\Provider\SessionServiceProvider()); 8 $app->register(new Silex\Provider\TwigServiceProvider(), array( 9 'twig.path' => __DIR__ . '/../views', 10 )); 11 12 /* ... */ 13 14 $app['debug'] = TRUE; 15 16 $app->error(function (\Exception $e, $code) { 17 /* Tratamento da exception */ 18 }); 19 20 $app->error(function (\LogicException $e, $code) { 21 /* Tratamento da exception */ 22 }); 23 24 $app->run();
DÚVIDAS?
OBRIGADO!