SlideShare ist ein Scribd-Unternehmen logo
1 von 42
Downloaden Sie, um offline zu lesen
Complex Sites with Silex 
Chris Tankersley 
ZendCon 2014
2 
Who Am I? 
● A PHP Developer for 10 Years 
● Lots of projects no one uses, 
and a few some do 
● https://github.com/dragonmantank
3 
What is Silex?
4 
Microframework 
● Built off of the Symfony2 
Components 
● Micro Service Container 
● Provides Routing 
● Provides Extensions
5 
Why to Not Use Silex 
● You have to piece things 
together 
● Not great for large projects
6 
Why use Silex? 
● You can use the libraries you 
want 
● Great for small/medium 
projects 
● Prototypers best friend
7 
Sites Change over time
8 
Silex Allows Us To Adapt
9 
Basic Silex App
10 
Set up Silex 
composer require “silex/silex”:”~1.2”
11 
Sample Silex App 
<?php 
require_once __DIR__ . '/../vendor/autoload.php'; 
$app = new SilexApplication(); 
$app->get('/', function() use ($app) { 
return 'Hello World'; 
}); 
$app->run();
12 
Congrats!
13 
And you add more pages 
<?php 
require_once __DIR__ . '/../vendor/autoload.php'; 
$app = new SilexApplication(); 
$app->get('/', function() use ($app) {/*...*/}); 
$app->get('/about, function() use ($app) {/*...*/}); 
$app->get('/contact', function() use ($app) {/*...*/}); 
$app->get('/services', function() use ($app) {/*...*/}); 
$app->run();
14 
Service Providers
15 
What do they do? 
● Allows code reuse 
● For both services and 
controllers
16 
Service Providers 
● Twig 
● URL Generator 
● Session 
● Validator 
● Form 
● HTTP Cache 
● HTTP Fragments 
● Security 
● Remember Me 
● Switftmailer 
● Monolog 
● Translation 
● Serializer 
● Doctrine 
● Controllers as 
Services
17 
And you add templating 
<?php 
require_once __DIR__ . '/../vendor/autoload.php'; 
$app = new SilexApplication(); 
$app->register(new SilexProviderTwigServiceProvider(), array( 
'twig.path' => __DIR__.'/../views', 
)); 
$app->get('/', function() use ($app) { 
return $app['twig']->render('homepage.html.twig'); 
}); 
$app->get('/about, function() use ($app) {/*...*/}); 
$app->get('/contact', function() use ($app) {/*...*/}); 
$app->get('/services', function() use ($app) {/*...*/}); 
$app->run();
18 
Adding a Form 
●We need to generate the form 
●We need to accept the form 
●We need to e-mail the form
19 
Now we add a form 
<?php 
require_once __DIR__ . '/../vendor/autoload.php'; 
use SymfonyComponentHttpFoundationRequest; 
$app = new SilexApplication(); 
$app->register(new SilexProviderTwigServiceProvider(), array( 
'twig.path' => __DIR__.'/views', 
)); 
$app->register(new SilexProviderFormServiceProvider()); 
$app->register(new SilexProviderTranslationServiceProvider()); 
$app->get('/', function() use ($app) {/*...*/}); 
$app->get('/about, function() use ($app) {/*...*/}); 
$app->get('/contact', function(Request $request) use ($app) { 
$form = $app['form.factory']->createBuilder('form') 
->add('name') 
->add('email') 
->getForm() 
; 
$form->handleRequest($request); 
if($form->isValid()) { 
$data = $form->getData(); 
// Mail it 
} 
return $app['twig']->render('contact.html.twig', array('form' => $form->createView())); 
}); 
$app->get('/services', function() use ($app) {/*..}); 
$app->run();
20 
HTTP Method Routing
21 
Whoops
22 
Allow Both Methods 
<?php 
require_once __DIR__ . '/../vendor/autoload.php'; 
use SymfonyComponentHttpFoundationRequest; 
$app = new SilexApplication(); 
$app->register(new SilexProviderTwigServiceProvider(), array( 
'twig.path' => __DIR__.'/views', 
)); 
$app->register(new SilexProviderFormServiceProvider()); 
$app->register(new SilexProviderTranslationServiceProvider()); 
$app->get('/', function() use ($app) {/*...*/}); 
$app->get('/about, function() use ($app) {/*...*/}); 
$app->match('/contact', function(Request $request) use ($app) { 
$form = $app['form.factory']->createBuilder('form') 
->add('name') 
->add('email') 
->getForm() 
; 
$form->handleRequest($request); 
if($form->isValid()) { 
$data = $form->getData(); 
// Mail it 
} 
return $app['twig']->render('contact.html.twig', array('form' => $form->createView())); 
})->method('GET|POST'); 
$app->get('/services', function() use ($app) {/*..}); 
$app->run();
23 
We've silently introduced a problem into our 
code
24 
Code Quickly Turns to 
Spaghetti 
http://commons.wikimedia.org/wiki/File:Spaghetti-prepared.jpg
25 
Pretend to be a big framework
26 
What do we need to do? 
● Consistent File Layout 
● Separate out functionality 
● Make things more testable
27 
Clean up our file structure
28 
Break up that big file 
● File for bootstrapping the app 
● File for routes 
● File for running the app
29 
web/index.php 
require_once __DIR__ . '/../vendor/autoload.php'; 
$app = new SilexApplication(); 
require_once __DIR__ . '/../app/bootstrap.php'; 
require_once __DIR__ . '/../app/routes.php'; 
$app->run();
30 
app/bootstrap.php 
$config = include_once 'config/config.php'; 
$app['config'] = $config; 
$app->register(new SilexProviderTwigServiceProvider(), array( 
'twig.path' => $app['config']['template_path'], 
)); 
$app->register(new SilexProviderFormServiceProvider()); 
$app->register(new SilexProviderTranslationServiceProvider());
31 
app/routes.php 
$app->get('/', function() use ($app) {/*...*/}); 
$app->get('/about, function() use ($app) {/*...*/}); 
$app->match('/contact', function(Request $request) use ($app) { 
$form = $app['form.factory']->createBuilder('form') 
->add('name') 
->add('email') 
->getForm() 
; 
$form->handleRequest($request); 
if($form->isValid()) { 
$data = $form->getData(); 
// Mail it 
} 
return $app['twig']->render('contact.html.twig', array('form' => $form->createView())); 
})->method('GET|POST'); 
$app->get('/services', function() use ($app) {/*..});
32 
Service Providers
33 
For Services 
● Implement 
SilexServiceProviderInterface 
● Implement boot() and 
register()
34 
For Controllers 
● Implement 
SilexControllerProviderInterface 
● Implement connect()
35 
Sample Controller 
use SilexApplication; 
use SilexControllerProviderInterface; 
class BlogController implements ControllerProviderInterface { 
public function connect(Application $app) { 
$controllers = $app['controllers_factory']; 
$controllers->get('/', array($this, 'index')); 
$controllers->get('/inline', function() use ($app) { 
/* Logic Here */ 
}); 
} 
protected function index(Application $app) { 
/* Logic Here */ 
} 
} 
$app->mount('/blog', new BlogController());
36 
Controllers as Services 
● Ships with Silex 
● Allows attaching objects to 
routes 
● Favors dependency injection 
● Framework Agnostic
37 
Create our Object 
// src/MyApp/Controller/IndexController.php 
namespace MyAppController; 
use SilexApplication; 
use SymfonyComponentHttpFoundationRequest; 
class IndexController 
{ 
public function indexAction(Application $app) 
{ 
return $app['twig']->render('Index/index.html.twig'); 
} 
public function aboutAction(Application $app) { /* … */ } 
public function servicesAction(Application $app) { /* … */ } 
public function contactAction(Application $app) { /* … */ } 
}
38 
Register Controllers as 
Services 
$config = include_once 'config/config.php'; 
$app['config'] = $config; 
$app->register(new SilexProviderTwigServiceProvider(), array( 
'twig.path' => $app['config']['template_path'], 
)); 
$app->register(new SilexProviderFormServiceProvider()); 
$app->register(new SilexProviderTranslationServiceProvider()); 
$app['controller.index'] = $app->share(function() use ($app) { 
return new MyAppControllerIndexController(); 
});
39 
Register our routes 
$app->get('/', 'controller.index:indexAction')->bind('homepage'); 
$app->get('/about', 'controller.index:aboutAction')->bind('about'); 
$app->get('/services', 'controller.index:servicesAction')->bind('services'); 
$app->match('/contact', 'controller.index:contactAction')->bind('contact')->method('GET|POST');
40 
A Little Planning goes a Long Way
41 
Questions?
42 
Thanks! 
● http://joind.in/talk/view/12080 
● @dragonmantank 
● chris@ctankersley.com

Weitere ähnliche Inhalte

Was ist angesagt?

And the Greatest of These Is ... Rack Support
And the Greatest of These Is ... Rack SupportAnd the Greatest of These Is ... Rack Support
And the Greatest of These Is ... Rack SupportBen Scofield
 
Building Cloud Castles
Building Cloud CastlesBuilding Cloud Castles
Building Cloud CastlesBen Scofield
 
Unit testing after Zend Framework 1.8
Unit testing after Zend Framework 1.8Unit testing after Zend Framework 1.8
Unit testing after Zend Framework 1.8Michelangelo van Dam
 
Phpne august-2012-symfony-components-friends
Phpne august-2012-symfony-components-friendsPhpne august-2012-symfony-components-friends
Phpne august-2012-symfony-components-friendsMichael Peacock
 
Desenvolvendo APIs usando Rails - Guru SC 2012
Desenvolvendo APIs usando Rails - Guru SC 2012Desenvolvendo APIs usando Rails - Guru SC 2012
Desenvolvendo APIs usando Rails - Guru SC 2012Rafael Felix da Silva
 
Php Unit With Zend Framework Zendcon09
Php Unit With Zend Framework   Zendcon09Php Unit With Zend Framework   Zendcon09
Php Unit With Zend Framework Zendcon09Michelangelo van Dam
 
Алексей Плеханов: Новинки Laravel 5
Алексей Плеханов: Новинки Laravel 5Алексей Плеханов: Новинки Laravel 5
Алексей Плеханов: Новинки Laravel 5Oleg Poludnenko
 
Dance for the puppet master: G6 Tech Talk
Dance for the puppet master: G6 Tech TalkDance for the puppet master: G6 Tech Talk
Dance for the puppet master: G6 Tech TalkMichael Peacock
 
New Symfony Tips & Tricks (SymfonyCon Paris 2015)
New Symfony Tips & Tricks (SymfonyCon Paris 2015)New Symfony Tips & Tricks (SymfonyCon Paris 2015)
New Symfony Tips & Tricks (SymfonyCon Paris 2015)Javier Eguiluz
 
Keep It Simple Security (Symfony cafe 28-01-2016)
Keep It Simple Security (Symfony cafe 28-01-2016)Keep It Simple Security (Symfony cafe 28-01-2016)
Keep It Simple Security (Symfony cafe 28-01-2016)Oleg Zinchenko
 
Angular server-side communication
Angular server-side communicationAngular server-side communication
Angular server-side communicationAlexe Bogdan
 
Slim RedBeanPHP and Knockout
Slim RedBeanPHP and KnockoutSlim RedBeanPHP and Knockout
Slim RedBeanPHP and KnockoutVic Metcalfe
 
Symfony Guard Authentication: Fun with API Token, Social Login, JWT and more
Symfony Guard Authentication: Fun with API Token, Social Login, JWT and moreSymfony Guard Authentication: Fun with API Token, Social Login, JWT and more
Symfony Guard Authentication: Fun with API Token, Social Login, JWT and moreRyan Weaver
 

Was ist angesagt? (20)

And the Greatest of These Is ... Rack Support
And the Greatest of These Is ... Rack SupportAnd the Greatest of These Is ... Rack Support
And the Greatest of These Is ... Rack Support
 
Building Cloud Castles
Building Cloud CastlesBuilding Cloud Castles
Building Cloud Castles
 
Unit testing after Zend Framework 1.8
Unit testing after Zend Framework 1.8Unit testing after Zend Framework 1.8
Unit testing after Zend Framework 1.8
 
Phpne august-2012-symfony-components-friends
Phpne august-2012-symfony-components-friendsPhpne august-2012-symfony-components-friends
Phpne august-2012-symfony-components-friends
 
Desenvolvendo APIs usando Rails - Guru SC 2012
Desenvolvendo APIs usando Rails - Guru SC 2012Desenvolvendo APIs usando Rails - Guru SC 2012
Desenvolvendo APIs usando Rails - Guru SC 2012
 
Php Unit With Zend Framework Zendcon09
Php Unit With Zend Framework   Zendcon09Php Unit With Zend Framework   Zendcon09
Php Unit With Zend Framework Zendcon09
 
New in php 7
New in php 7New in php 7
New in php 7
 
Алексей Плеханов: Новинки Laravel 5
Алексей Плеханов: Новинки Laravel 5Алексей Плеханов: Новинки Laravel 5
Алексей Плеханов: Новинки Laravel 5
 
Silex Cheat Sheet
Silex Cheat SheetSilex Cheat Sheet
Silex Cheat Sheet
 
Dance for the puppet master: G6 Tech Talk
Dance for the puppet master: G6 Tech TalkDance for the puppet master: G6 Tech Talk
Dance for the puppet master: G6 Tech Talk
 
New Symfony Tips & Tricks (SymfonyCon Paris 2015)
New Symfony Tips & Tricks (SymfonyCon Paris 2015)New Symfony Tips & Tricks (SymfonyCon Paris 2015)
New Symfony Tips & Tricks (SymfonyCon Paris 2015)
 
Getting Started-with-Laravel
Getting Started-with-LaravelGetting Started-with-Laravel
Getting Started-with-Laravel
 
PhpSpec extension points
PhpSpec extension pointsPhpSpec extension points
PhpSpec extension points
 
Keep It Simple Security (Symfony cafe 28-01-2016)
Keep It Simple Security (Symfony cafe 28-01-2016)Keep It Simple Security (Symfony cafe 28-01-2016)
Keep It Simple Security (Symfony cafe 28-01-2016)
 
Oro meetup #4
Oro meetup #4Oro meetup #4
Oro meetup #4
 
Phinx talk
Phinx talkPhinx talk
Phinx talk
 
Zend framework
Zend frameworkZend framework
Zend framework
 
Angular server-side communication
Angular server-side communicationAngular server-side communication
Angular server-side communication
 
Slim RedBeanPHP and Knockout
Slim RedBeanPHP and KnockoutSlim RedBeanPHP and Knockout
Slim RedBeanPHP and Knockout
 
Symfony Guard Authentication: Fun with API Token, Social Login, JWT and more
Symfony Guard Authentication: Fun with API Token, Social Login, JWT and moreSymfony Guard Authentication: Fun with API Token, Social Login, JWT and more
Symfony Guard Authentication: Fun with API Token, Social Login, JWT and more
 

Andere mochten auch

Introducción a Silex. Aprendiendo a hacer las cosas bien en PHP
Introducción a Silex. Aprendiendo a hacer las cosas bien en PHPIntroducción a Silex. Aprendiendo a hacer las cosas bien en PHP
Introducción a Silex. Aprendiendo a hacer las cosas bien en PHPDaniel Primo
 
Your marketing funnel is a hot mess
Your marketing funnel is a hot messYour marketing funnel is a hot mess
Your marketing funnel is a hot messGetResponse
 
Silex meets SOAP & REST
Silex meets SOAP & RESTSilex meets SOAP & REST
Silex meets SOAP & RESTHugo Hamon
 
Google drive for nonprofits webinar
Google drive for nonprofits webinarGoogle drive for nonprofits webinar
Google drive for nonprofits webinarCraig Grella
 
&lt;iframe src="//www.slideshare.net/slideshow/embed_code/42392945" width="47...
&lt;iframe src="//www.slideshare.net/slideshow/embed_code/42392945" width="47...&lt;iframe src="//www.slideshare.net/slideshow/embed_code/42392945" width="47...
&lt;iframe src="//www.slideshare.net/slideshow/embed_code/42392945" width="47...Reputation VIP
 
Project performance tracking analysis and reporting
Project performance tracking analysis and reportingProject performance tracking analysis and reporting
Project performance tracking analysis and reportingCharles Cotter, PhD
 
How to Embed a PowerPoint Presentation Using SlideShare
How to Embed a PowerPoint Presentation Using SlideShareHow to Embed a PowerPoint Presentation Using SlideShare
How to Embed a PowerPoint Presentation Using SlideShareJoie Ocon
 
Optimize Your Sales & Marketing Funnel
Optimize Your Sales & Marketing FunnelOptimize Your Sales & Marketing Funnel
Optimize Your Sales & Marketing FunnelHubSpot
 
How To Embed SlideShare Shows Into WordPress.com
How To Embed SlideShare Shows Into WordPress.comHow To Embed SlideShare Shows Into WordPress.com
How To Embed SlideShare Shows Into WordPress.comKathy Gill
 
2015 Upload Campaigns Calendar - SlideShare
2015 Upload Campaigns Calendar - SlideShare2015 Upload Campaigns Calendar - SlideShare
2015 Upload Campaigns Calendar - SlideShareSlideShare
 
What to Upload to SlideShare
What to Upload to SlideShareWhat to Upload to SlideShare
What to Upload to SlideShareSlideShare
 
Getting Started With SlideShare
Getting Started With SlideShareGetting Started With SlideShare
Getting Started With SlideShareSlideShare
 

Andere mochten auch (13)

Introducción a Silex. Aprendiendo a hacer las cosas bien en PHP
Introducción a Silex. Aprendiendo a hacer las cosas bien en PHPIntroducción a Silex. Aprendiendo a hacer las cosas bien en PHP
Introducción a Silex. Aprendiendo a hacer las cosas bien en PHP
 
Sales-Funnel-Slideshare
Sales-Funnel-SlideshareSales-Funnel-Slideshare
Sales-Funnel-Slideshare
 
Your marketing funnel is a hot mess
Your marketing funnel is a hot messYour marketing funnel is a hot mess
Your marketing funnel is a hot mess
 
Silex meets SOAP & REST
Silex meets SOAP & RESTSilex meets SOAP & REST
Silex meets SOAP & REST
 
Google drive for nonprofits webinar
Google drive for nonprofits webinarGoogle drive for nonprofits webinar
Google drive for nonprofits webinar
 
&lt;iframe src="//www.slideshare.net/slideshow/embed_code/42392945" width="47...
&lt;iframe src="//www.slideshare.net/slideshow/embed_code/42392945" width="47...&lt;iframe src="//www.slideshare.net/slideshow/embed_code/42392945" width="47...
&lt;iframe src="//www.slideshare.net/slideshow/embed_code/42392945" width="47...
 
Project performance tracking analysis and reporting
Project performance tracking analysis and reportingProject performance tracking analysis and reporting
Project performance tracking analysis and reporting
 
How to Embed a PowerPoint Presentation Using SlideShare
How to Embed a PowerPoint Presentation Using SlideShareHow to Embed a PowerPoint Presentation Using SlideShare
How to Embed a PowerPoint Presentation Using SlideShare
 
Optimize Your Sales & Marketing Funnel
Optimize Your Sales & Marketing FunnelOptimize Your Sales & Marketing Funnel
Optimize Your Sales & Marketing Funnel
 
How To Embed SlideShare Shows Into WordPress.com
How To Embed SlideShare Shows Into WordPress.comHow To Embed SlideShare Shows Into WordPress.com
How To Embed SlideShare Shows Into WordPress.com
 
2015 Upload Campaigns Calendar - SlideShare
2015 Upload Campaigns Calendar - SlideShare2015 Upload Campaigns Calendar - SlideShare
2015 Upload Campaigns Calendar - SlideShare
 
What to Upload to SlideShare
What to Upload to SlideShareWhat to Upload to SlideShare
What to Upload to SlideShare
 
Getting Started With SlideShare
Getting Started With SlideShareGetting Started With SlideShare
Getting Started With SlideShare
 

Ähnlich wie Complex Sites with Silex

What's New In Laravel 5
What's New In Laravel 5What's New In Laravel 5
What's New In Laravel 5Darren Craig
 
Doctrine For Beginners
Doctrine For BeginnersDoctrine For Beginners
Doctrine For BeginnersJonathan Wage
 
WCMTL 15 - Create your own shortcode (Fr)
WCMTL 15 - Create your own shortcode (Fr)WCMTL 15 - Create your own shortcode (Fr)
WCMTL 15 - Create your own shortcode (Fr)MichaelBontyes
 
Creating REST Applications with the Slim Micro-Framework by Vikram Vaswani
Creating REST Applications with the Slim Micro-Framework by Vikram VaswaniCreating REST Applications with the Slim Micro-Framework by Vikram Vaswani
Creating REST Applications with the Slim Micro-Framework by Vikram Vaswanivvaswani
 
How and why i roll my own node.js framework
How and why i roll my own node.js frameworkHow and why i roll my own node.js framework
How and why i roll my own node.js frameworkBen Lin
 
Angular server side rendering - Strategies & Technics
Angular server side rendering - Strategies & Technics Angular server side rendering - Strategies & Technics
Angular server side rendering - Strategies & Technics Eliran Eliassy
 
Pyramid Deployment and Maintenance
Pyramid Deployment and MaintenancePyramid Deployment and Maintenance
Pyramid Deployment and MaintenanceJazkarta, Inc.
 
Zend Framework 1.9 Setup & Using Zend_Tool
Zend Framework 1.9 Setup & Using Zend_ToolZend Framework 1.9 Setup & Using Zend_Tool
Zend Framework 1.9 Setup & Using Zend_ToolGordon Forsythe
 
AWS re:Invent 2016: Chalice: A Serverless Microframework for Python (DEV308)
AWS re:Invent 2016: Chalice: A Serverless Microframework for Python (DEV308)AWS re:Invent 2016: Chalice: A Serverless Microframework for Python (DEV308)
AWS re:Invent 2016: Chalice: A Serverless Microframework for Python (DEV308)Amazon Web Services
 
Refresh Austin - Intro to Dexy
Refresh Austin - Intro to DexyRefresh Austin - Intro to Dexy
Refresh Austin - Intro to Dexyananelson
 
Into the ZF2 Service Manager
Into the ZF2 Service ManagerInto the ZF2 Service Manager
Into the ZF2 Service ManagerChris Tankersley
 
symfony on action - WebTech 207
symfony on action - WebTech 207symfony on action - WebTech 207
symfony on action - WebTech 207patter
 
ZFConf 2010: Zend Framework & MVC, Model Implementation (Part 2, Dependency I...
ZFConf 2010: Zend Framework & MVC, Model Implementation (Part 2, Dependency I...ZFConf 2010: Zend Framework & MVC, Model Implementation (Part 2, Dependency I...
ZFConf 2010: Zend Framework & MVC, Model Implementation (Part 2, Dependency I...ZFConf Conference
 
A gently introduction to AngularJS
A gently introduction to AngularJSA gently introduction to AngularJS
A gently introduction to AngularJSGregor Woiwode
 

Ähnlich wie Complex Sites with Silex (20)

Silex Cheat Sheet
Silex Cheat SheetSilex Cheat Sheet
Silex Cheat Sheet
 
What's New In Laravel 5
What's New In Laravel 5What's New In Laravel 5
What's New In Laravel 5
 
Doctrine For Beginners
Doctrine For BeginnersDoctrine For Beginners
Doctrine For Beginners
 
WCMTL 15 - Create your own shortcode (Fr)
WCMTL 15 - Create your own shortcode (Fr)WCMTL 15 - Create your own shortcode (Fr)
WCMTL 15 - Create your own shortcode (Fr)
 
Introduction to angular js
Introduction to angular jsIntroduction to angular js
Introduction to angular js
 
REST API for your WP7 App
REST API for your WP7 AppREST API for your WP7 App
REST API for your WP7 App
 
Creating REST Applications with the Slim Micro-Framework by Vikram Vaswani
Creating REST Applications with the Slim Micro-Framework by Vikram VaswaniCreating REST Applications with the Slim Micro-Framework by Vikram Vaswani
Creating REST Applications with the Slim Micro-Framework by Vikram Vaswani
 
How and why i roll my own node.js framework
How and why i roll my own node.js frameworkHow and why i roll my own node.js framework
How and why i roll my own node.js framework
 
Angular server side rendering - Strategies & Technics
Angular server side rendering - Strategies & Technics Angular server side rendering - Strategies & Technics
Angular server side rendering - Strategies & Technics
 
NestJS
NestJSNestJS
NestJS
 
Pyramid deployment
Pyramid deploymentPyramid deployment
Pyramid deployment
 
Pyramid Deployment and Maintenance
Pyramid Deployment and MaintenancePyramid Deployment and Maintenance
Pyramid Deployment and Maintenance
 
Zend Framework 1.9 Setup & Using Zend_Tool
Zend Framework 1.9 Setup & Using Zend_ToolZend Framework 1.9 Setup & Using Zend_Tool
Zend Framework 1.9 Setup & Using Zend_Tool
 
AWS re:Invent 2016: Chalice: A Serverless Microframework for Python (DEV308)
AWS re:Invent 2016: Chalice: A Serverless Microframework for Python (DEV308)AWS re:Invent 2016: Chalice: A Serverless Microframework for Python (DEV308)
AWS re:Invent 2016: Chalice: A Serverless Microframework for Python (DEV308)
 
Hexagonal architecture
Hexagonal architectureHexagonal architecture
Hexagonal architecture
 
Refresh Austin - Intro to Dexy
Refresh Austin - Intro to DexyRefresh Austin - Intro to Dexy
Refresh Austin - Intro to Dexy
 
Into the ZF2 Service Manager
Into the ZF2 Service ManagerInto the ZF2 Service Manager
Into the ZF2 Service Manager
 
symfony on action - WebTech 207
symfony on action - WebTech 207symfony on action - WebTech 207
symfony on action - WebTech 207
 
ZFConf 2010: Zend Framework & MVC, Model Implementation (Part 2, Dependency I...
ZFConf 2010: Zend Framework & MVC, Model Implementation (Part 2, Dependency I...ZFConf 2010: Zend Framework & MVC, Model Implementation (Part 2, Dependency I...
ZFConf 2010: Zend Framework & MVC, Model Implementation (Part 2, Dependency I...
 
A gently introduction to AngularJS
A gently introduction to AngularJSA gently introduction to AngularJS
A gently introduction to AngularJS
 

Mehr von Chris Tankersley

Docker is Dead: Long Live Containers
Docker is Dead: Long Live ContainersDocker is Dead: Long Live Containers
Docker is Dead: Long Live ContainersChris Tankersley
 
Bend time to your will with git
Bend time to your will with gitBend time to your will with git
Bend time to your will with gitChris Tankersley
 
Using PHP Functions! (Not those functions, Google Cloud Functions)
Using PHP Functions! (Not those functions, Google Cloud Functions)Using PHP Functions! (Not those functions, Google Cloud Functions)
Using PHP Functions! (Not those functions, Google Cloud Functions)Chris Tankersley
 
Dead Simple APIs with OpenAPI
Dead Simple APIs with OpenAPIDead Simple APIs with OpenAPI
Dead Simple APIs with OpenAPIChris Tankersley
 
Killer Docker Workflows for Development
Killer Docker Workflows for DevelopmentKiller Docker Workflows for Development
Killer Docker Workflows for DevelopmentChris Tankersley
 
Docker for Developers - PHP Detroit 2018
Docker for Developers - PHP Detroit 2018Docker for Developers - PHP Detroit 2018
Docker for Developers - PHP Detroit 2018Chris Tankersley
 
BASHing at the CLI - Midwest PHP 2018
BASHing at the CLI - Midwest PHP 2018BASHing at the CLI - Midwest PHP 2018
BASHing at the CLI - Midwest PHP 2018Chris Tankersley
 
You Were Lied To About Optimization
You Were Lied To About OptimizationYou Were Lied To About Optimization
You Were Lied To About OptimizationChris Tankersley
 
Docker for PHP Developers - php[world] 2017
Docker for PHP Developers - php[world] 2017Docker for PHP Developers - php[world] 2017
Docker for PHP Developers - php[world] 2017Chris Tankersley
 
Docker for PHP Developers - Madison PHP 2017
Docker for PHP Developers - Madison PHP 2017Docker for PHP Developers - Madison PHP 2017
Docker for PHP Developers - Madison PHP 2017Chris Tankersley
 
Docker for Developers - php[tek] 2017
Docker for Developers - php[tek] 2017Docker for Developers - php[tek] 2017
Docker for Developers - php[tek] 2017Chris Tankersley
 
Why Docker? Dayton PHP, April 2017
Why Docker? Dayton PHP, April 2017Why Docker? Dayton PHP, April 2017
Why Docker? Dayton PHP, April 2017Chris Tankersley
 
OOP Is More Then Cars and Dogs - Midwest PHP 2017
OOP Is More Then Cars and Dogs - Midwest PHP 2017OOP Is More Then Cars and Dogs - Midwest PHP 2017
OOP Is More Then Cars and Dogs - Midwest PHP 2017Chris Tankersley
 
From Docker to Production - SunshinePHP 2017
From Docker to Production - SunshinePHP 2017From Docker to Production - SunshinePHP 2017
From Docker to Production - SunshinePHP 2017Chris Tankersley
 
Docker for Developers - Sunshine PHP
Docker for Developers - Sunshine PHPDocker for Developers - Sunshine PHP
Docker for Developers - Sunshine PHPChris Tankersley
 
Coming to Terms with OOP In Drupal - php[world] 2016
Coming to Terms with OOP In Drupal - php[world] 2016Coming to Terms with OOP In Drupal - php[world] 2016
Coming to Terms with OOP In Drupal - php[world] 2016Chris Tankersley
 
How We Got Here: A Brief History of Open Source
How We Got Here: A Brief History of Open SourceHow We Got Here: A Brief History of Open Source
How We Got Here: A Brief History of Open SourceChris Tankersley
 

Mehr von Chris Tankersley (20)

Docker is Dead: Long Live Containers
Docker is Dead: Long Live ContainersDocker is Dead: Long Live Containers
Docker is Dead: Long Live Containers
 
Bend time to your will with git
Bend time to your will with gitBend time to your will with git
Bend time to your will with git
 
Using PHP Functions! (Not those functions, Google Cloud Functions)
Using PHP Functions! (Not those functions, Google Cloud Functions)Using PHP Functions! (Not those functions, Google Cloud Functions)
Using PHP Functions! (Not those functions, Google Cloud Functions)
 
Dead Simple APIs with OpenAPI
Dead Simple APIs with OpenAPIDead Simple APIs with OpenAPI
Dead Simple APIs with OpenAPI
 
Killer Docker Workflows for Development
Killer Docker Workflows for DevelopmentKiller Docker Workflows for Development
Killer Docker Workflows for Development
 
You Got Async in my PHP!
You Got Async in my PHP!You Got Async in my PHP!
You Got Async in my PHP!
 
Docker for Developers - PHP Detroit 2018
Docker for Developers - PHP Detroit 2018Docker for Developers - PHP Detroit 2018
Docker for Developers - PHP Detroit 2018
 
Docker for Developers
Docker for DevelopersDocker for Developers
Docker for Developers
 
They are Watching You
They are Watching YouThey are Watching You
They are Watching You
 
BASHing at the CLI - Midwest PHP 2018
BASHing at the CLI - Midwest PHP 2018BASHing at the CLI - Midwest PHP 2018
BASHing at the CLI - Midwest PHP 2018
 
You Were Lied To About Optimization
You Were Lied To About OptimizationYou Were Lied To About Optimization
You Were Lied To About Optimization
 
Docker for PHP Developers - php[world] 2017
Docker for PHP Developers - php[world] 2017Docker for PHP Developers - php[world] 2017
Docker for PHP Developers - php[world] 2017
 
Docker for PHP Developers - Madison PHP 2017
Docker for PHP Developers - Madison PHP 2017Docker for PHP Developers - Madison PHP 2017
Docker for PHP Developers - Madison PHP 2017
 
Docker for Developers - php[tek] 2017
Docker for Developers - php[tek] 2017Docker for Developers - php[tek] 2017
Docker for Developers - php[tek] 2017
 
Why Docker? Dayton PHP, April 2017
Why Docker? Dayton PHP, April 2017Why Docker? Dayton PHP, April 2017
Why Docker? Dayton PHP, April 2017
 
OOP Is More Then Cars and Dogs - Midwest PHP 2017
OOP Is More Then Cars and Dogs - Midwest PHP 2017OOP Is More Then Cars and Dogs - Midwest PHP 2017
OOP Is More Then Cars and Dogs - Midwest PHP 2017
 
From Docker to Production - SunshinePHP 2017
From Docker to Production - SunshinePHP 2017From Docker to Production - SunshinePHP 2017
From Docker to Production - SunshinePHP 2017
 
Docker for Developers - Sunshine PHP
Docker for Developers - Sunshine PHPDocker for Developers - Sunshine PHP
Docker for Developers - Sunshine PHP
 
Coming to Terms with OOP In Drupal - php[world] 2016
Coming to Terms with OOP In Drupal - php[world] 2016Coming to Terms with OOP In Drupal - php[world] 2016
Coming to Terms with OOP In Drupal - php[world] 2016
 
How We Got Here: A Brief History of Open Source
How We Got Here: A Brief History of Open SourceHow We Got Here: A Brief History of Open Source
How We Got Here: A Brief History of Open Source
 

Kürzlich hochgeladen

Boost Fertility New Invention Ups Success Rates.pdf
Boost Fertility New Invention Ups Success Rates.pdfBoost Fertility New Invention Ups Success Rates.pdf
Boost Fertility New Invention Ups Success Rates.pdfsudhanshuwaghmare1
 
Apidays Singapore 2024 - Modernizing Securities Finance by Madhu Subbu
Apidays Singapore 2024 - Modernizing Securities Finance by Madhu SubbuApidays Singapore 2024 - Modernizing Securities Finance by Madhu Subbu
Apidays Singapore 2024 - Modernizing Securities Finance by Madhu Subbuapidays
 
Mastering MySQL Database Architecture: Deep Dive into MySQL Shell and MySQL R...
Mastering MySQL Database Architecture: Deep Dive into MySQL Shell and MySQL R...Mastering MySQL Database Architecture: Deep Dive into MySQL Shell and MySQL R...
Mastering MySQL Database Architecture: Deep Dive into MySQL Shell and MySQL R...Miguel Araújo
 
Connector Corner: Accelerate revenue generation using UiPath API-centric busi...
Connector Corner: Accelerate revenue generation using UiPath API-centric busi...Connector Corner: Accelerate revenue generation using UiPath API-centric busi...
Connector Corner: Accelerate revenue generation using UiPath API-centric busi...DianaGray10
 
Apidays Singapore 2024 - Scalable LLM APIs for AI and Generative AI Applicati...
Apidays Singapore 2024 - Scalable LLM APIs for AI and Generative AI Applicati...Apidays Singapore 2024 - Scalable LLM APIs for AI and Generative AI Applicati...
Apidays Singapore 2024 - Scalable LLM APIs for AI and Generative AI Applicati...apidays
 
Emergent Methods: Multi-lingual narrative tracking in the news - real-time ex...
Emergent Methods: Multi-lingual narrative tracking in the news - real-time ex...Emergent Methods: Multi-lingual narrative tracking in the news - real-time ex...
Emergent Methods: Multi-lingual narrative tracking in the news - real-time ex...Zilliz
 
AWS Community Day CPH - Three problems of Terraform
AWS Community Day CPH - Three problems of TerraformAWS Community Day CPH - Three problems of Terraform
AWS Community Day CPH - Three problems of TerraformAndrey Devyatkin
 
Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...
Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...
Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...Drew Madelung
 
Strategies for Landing an Oracle DBA Job as a Fresher
Strategies for Landing an Oracle DBA Job as a FresherStrategies for Landing an Oracle DBA Job as a Fresher
Strategies for Landing an Oracle DBA Job as a FresherRemote DBA Services
 
Manulife - Insurer Transformation Award 2024
Manulife - Insurer Transformation Award 2024Manulife - Insurer Transformation Award 2024
Manulife - Insurer Transformation Award 2024The Digital Insurer
 
TrustArc Webinar - Unlock the Power of AI-Driven Data Discovery
TrustArc Webinar - Unlock the Power of AI-Driven Data DiscoveryTrustArc Webinar - Unlock the Power of AI-Driven Data Discovery
TrustArc Webinar - Unlock the Power of AI-Driven Data DiscoveryTrustArc
 
Strategize a Smooth Tenant-to-tenant Migration and Copilot Takeoff
Strategize a Smooth Tenant-to-tenant Migration and Copilot TakeoffStrategize a Smooth Tenant-to-tenant Migration and Copilot Takeoff
Strategize a Smooth Tenant-to-tenant Migration and Copilot Takeoffsammart93
 
2024: Domino Containers - The Next Step. News from the Domino Container commu...
2024: Domino Containers - The Next Step. News from the Domino Container commu...2024: Domino Containers - The Next Step. News from the Domino Container commu...
2024: Domino Containers - The Next Step. News from the Domino Container commu...Martijn de Jong
 
A Year of the Servo Reboot: Where Are We Now?
A Year of the Servo Reboot: Where Are We Now?A Year of the Servo Reboot: Where Are We Now?
A Year of the Servo Reboot: Where Are We Now?Igalia
 
Polkadot JAM Slides - Token2049 - By Dr. Gavin Wood
Polkadot JAM Slides - Token2049 - By Dr. Gavin WoodPolkadot JAM Slides - Token2049 - By Dr. Gavin Wood
Polkadot JAM Slides - Token2049 - By Dr. Gavin WoodJuan lago vázquez
 
presentation ICT roal in 21st century education
presentation ICT roal in 21st century educationpresentation ICT roal in 21st century education
presentation ICT roal in 21st century educationjfdjdjcjdnsjd
 
Powerful Google developer tools for immediate impact! (2023-24 C)
Powerful Google developer tools for immediate impact! (2023-24 C)Powerful Google developer tools for immediate impact! (2023-24 C)
Powerful Google developer tools for immediate impact! (2023-24 C)wesley chun
 
Repurposing LNG terminals for Hydrogen Ammonia: Feasibility and Cost Saving
Repurposing LNG terminals for Hydrogen Ammonia: Feasibility and Cost SavingRepurposing LNG terminals for Hydrogen Ammonia: Feasibility and Cost Saving
Repurposing LNG terminals for Hydrogen Ammonia: Feasibility and Cost SavingEdi Saputra
 
"I see eyes in my soup": How Delivery Hero implemented the safety system for ...
"I see eyes in my soup": How Delivery Hero implemented the safety system for ..."I see eyes in my soup": How Delivery Hero implemented the safety system for ...
"I see eyes in my soup": How Delivery Hero implemented the safety system for ...Zilliz
 

Kürzlich hochgeladen (20)

Boost Fertility New Invention Ups Success Rates.pdf
Boost Fertility New Invention Ups Success Rates.pdfBoost Fertility New Invention Ups Success Rates.pdf
Boost Fertility New Invention Ups Success Rates.pdf
 
Apidays Singapore 2024 - Modernizing Securities Finance by Madhu Subbu
Apidays Singapore 2024 - Modernizing Securities Finance by Madhu SubbuApidays Singapore 2024 - Modernizing Securities Finance by Madhu Subbu
Apidays Singapore 2024 - Modernizing Securities Finance by Madhu Subbu
 
Mastering MySQL Database Architecture: Deep Dive into MySQL Shell and MySQL R...
Mastering MySQL Database Architecture: Deep Dive into MySQL Shell and MySQL R...Mastering MySQL Database Architecture: Deep Dive into MySQL Shell and MySQL R...
Mastering MySQL Database Architecture: Deep Dive into MySQL Shell and MySQL R...
 
Connector Corner: Accelerate revenue generation using UiPath API-centric busi...
Connector Corner: Accelerate revenue generation using UiPath API-centric busi...Connector Corner: Accelerate revenue generation using UiPath API-centric busi...
Connector Corner: Accelerate revenue generation using UiPath API-centric busi...
 
Apidays Singapore 2024 - Scalable LLM APIs for AI and Generative AI Applicati...
Apidays Singapore 2024 - Scalable LLM APIs for AI and Generative AI Applicati...Apidays Singapore 2024 - Scalable LLM APIs for AI and Generative AI Applicati...
Apidays Singapore 2024 - Scalable LLM APIs for AI and Generative AI Applicati...
 
Emergent Methods: Multi-lingual narrative tracking in the news - real-time ex...
Emergent Methods: Multi-lingual narrative tracking in the news - real-time ex...Emergent Methods: Multi-lingual narrative tracking in the news - real-time ex...
Emergent Methods: Multi-lingual narrative tracking in the news - real-time ex...
 
AWS Community Day CPH - Three problems of Terraform
AWS Community Day CPH - Three problems of TerraformAWS Community Day CPH - Three problems of Terraform
AWS Community Day CPH - Three problems of Terraform
 
Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...
Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...
Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...
 
Strategies for Landing an Oracle DBA Job as a Fresher
Strategies for Landing an Oracle DBA Job as a FresherStrategies for Landing an Oracle DBA Job as a Fresher
Strategies for Landing an Oracle DBA Job as a Fresher
 
Manulife - Insurer Transformation Award 2024
Manulife - Insurer Transformation Award 2024Manulife - Insurer Transformation Award 2024
Manulife - Insurer Transformation Award 2024
 
TrustArc Webinar - Unlock the Power of AI-Driven Data Discovery
TrustArc Webinar - Unlock the Power of AI-Driven Data DiscoveryTrustArc Webinar - Unlock the Power of AI-Driven Data Discovery
TrustArc Webinar - Unlock the Power of AI-Driven Data Discovery
 
Strategize a Smooth Tenant-to-tenant Migration and Copilot Takeoff
Strategize a Smooth Tenant-to-tenant Migration and Copilot TakeoffStrategize a Smooth Tenant-to-tenant Migration and Copilot Takeoff
Strategize a Smooth Tenant-to-tenant Migration and Copilot Takeoff
 
2024: Domino Containers - The Next Step. News from the Domino Container commu...
2024: Domino Containers - The Next Step. News from the Domino Container commu...2024: Domino Containers - The Next Step. News from the Domino Container commu...
2024: Domino Containers - The Next Step. News from the Domino Container commu...
 
A Year of the Servo Reboot: Where Are We Now?
A Year of the Servo Reboot: Where Are We Now?A Year of the Servo Reboot: Where Are We Now?
A Year of the Servo Reboot: Where Are We Now?
 
Polkadot JAM Slides - Token2049 - By Dr. Gavin Wood
Polkadot JAM Slides - Token2049 - By Dr. Gavin WoodPolkadot JAM Slides - Token2049 - By Dr. Gavin Wood
Polkadot JAM Slides - Token2049 - By Dr. Gavin Wood
 
+971581248768>> SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHA...
+971581248768>> SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHA...+971581248768>> SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHA...
+971581248768>> SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHA...
 
presentation ICT roal in 21st century education
presentation ICT roal in 21st century educationpresentation ICT roal in 21st century education
presentation ICT roal in 21st century education
 
Powerful Google developer tools for immediate impact! (2023-24 C)
Powerful Google developer tools for immediate impact! (2023-24 C)Powerful Google developer tools for immediate impact! (2023-24 C)
Powerful Google developer tools for immediate impact! (2023-24 C)
 
Repurposing LNG terminals for Hydrogen Ammonia: Feasibility and Cost Saving
Repurposing LNG terminals for Hydrogen Ammonia: Feasibility and Cost SavingRepurposing LNG terminals for Hydrogen Ammonia: Feasibility and Cost Saving
Repurposing LNG terminals for Hydrogen Ammonia: Feasibility and Cost Saving
 
"I see eyes in my soup": How Delivery Hero implemented the safety system for ...
"I see eyes in my soup": How Delivery Hero implemented the safety system for ..."I see eyes in my soup": How Delivery Hero implemented the safety system for ...
"I see eyes in my soup": How Delivery Hero implemented the safety system for ...
 

Complex Sites with Silex

  • 1. Complex Sites with Silex Chris Tankersley ZendCon 2014
  • 2. 2 Who Am I? ● A PHP Developer for 10 Years ● Lots of projects no one uses, and a few some do ● https://github.com/dragonmantank
  • 3. 3 What is Silex?
  • 4. 4 Microframework ● Built off of the Symfony2 Components ● Micro Service Container ● Provides Routing ● Provides Extensions
  • 5. 5 Why to Not Use Silex ● You have to piece things together ● Not great for large projects
  • 6. 6 Why use Silex? ● You can use the libraries you want ● Great for small/medium projects ● Prototypers best friend
  • 7. 7 Sites Change over time
  • 8. 8 Silex Allows Us To Adapt
  • 10. 10 Set up Silex composer require “silex/silex”:”~1.2”
  • 11. 11 Sample Silex App <?php require_once __DIR__ . '/../vendor/autoload.php'; $app = new SilexApplication(); $app->get('/', function() use ($app) { return 'Hello World'; }); $app->run();
  • 13. 13 And you add more pages <?php require_once __DIR__ . '/../vendor/autoload.php'; $app = new SilexApplication(); $app->get('/', function() use ($app) {/*...*/}); $app->get('/about, function() use ($app) {/*...*/}); $app->get('/contact', function() use ($app) {/*...*/}); $app->get('/services', function() use ($app) {/*...*/}); $app->run();
  • 15. 15 What do they do? ● Allows code reuse ● For both services and controllers
  • 16. 16 Service Providers ● Twig ● URL Generator ● Session ● Validator ● Form ● HTTP Cache ● HTTP Fragments ● Security ● Remember Me ● Switftmailer ● Monolog ● Translation ● Serializer ● Doctrine ● Controllers as Services
  • 17. 17 And you add templating <?php require_once __DIR__ . '/../vendor/autoload.php'; $app = new SilexApplication(); $app->register(new SilexProviderTwigServiceProvider(), array( 'twig.path' => __DIR__.'/../views', )); $app->get('/', function() use ($app) { return $app['twig']->render('homepage.html.twig'); }); $app->get('/about, function() use ($app) {/*...*/}); $app->get('/contact', function() use ($app) {/*...*/}); $app->get('/services', function() use ($app) {/*...*/}); $app->run();
  • 18. 18 Adding a Form ●We need to generate the form ●We need to accept the form ●We need to e-mail the form
  • 19. 19 Now we add a form <?php require_once __DIR__ . '/../vendor/autoload.php'; use SymfonyComponentHttpFoundationRequest; $app = new SilexApplication(); $app->register(new SilexProviderTwigServiceProvider(), array( 'twig.path' => __DIR__.'/views', )); $app->register(new SilexProviderFormServiceProvider()); $app->register(new SilexProviderTranslationServiceProvider()); $app->get('/', function() use ($app) {/*...*/}); $app->get('/about, function() use ($app) {/*...*/}); $app->get('/contact', function(Request $request) use ($app) { $form = $app['form.factory']->createBuilder('form') ->add('name') ->add('email') ->getForm() ; $form->handleRequest($request); if($form->isValid()) { $data = $form->getData(); // Mail it } return $app['twig']->render('contact.html.twig', array('form' => $form->createView())); }); $app->get('/services', function() use ($app) {/*..}); $app->run();
  • 20. 20 HTTP Method Routing
  • 22. 22 Allow Both Methods <?php require_once __DIR__ . '/../vendor/autoload.php'; use SymfonyComponentHttpFoundationRequest; $app = new SilexApplication(); $app->register(new SilexProviderTwigServiceProvider(), array( 'twig.path' => __DIR__.'/views', )); $app->register(new SilexProviderFormServiceProvider()); $app->register(new SilexProviderTranslationServiceProvider()); $app->get('/', function() use ($app) {/*...*/}); $app->get('/about, function() use ($app) {/*...*/}); $app->match('/contact', function(Request $request) use ($app) { $form = $app['form.factory']->createBuilder('form') ->add('name') ->add('email') ->getForm() ; $form->handleRequest($request); if($form->isValid()) { $data = $form->getData(); // Mail it } return $app['twig']->render('contact.html.twig', array('form' => $form->createView())); })->method('GET|POST'); $app->get('/services', function() use ($app) {/*..}); $app->run();
  • 23. 23 We've silently introduced a problem into our code
  • 24. 24 Code Quickly Turns to Spaghetti http://commons.wikimedia.org/wiki/File:Spaghetti-prepared.jpg
  • 25. 25 Pretend to be a big framework
  • 26. 26 What do we need to do? ● Consistent File Layout ● Separate out functionality ● Make things more testable
  • 27. 27 Clean up our file structure
  • 28. 28 Break up that big file ● File for bootstrapping the app ● File for routes ● File for running the app
  • 29. 29 web/index.php require_once __DIR__ . '/../vendor/autoload.php'; $app = new SilexApplication(); require_once __DIR__ . '/../app/bootstrap.php'; require_once __DIR__ . '/../app/routes.php'; $app->run();
  • 30. 30 app/bootstrap.php $config = include_once 'config/config.php'; $app['config'] = $config; $app->register(new SilexProviderTwigServiceProvider(), array( 'twig.path' => $app['config']['template_path'], )); $app->register(new SilexProviderFormServiceProvider()); $app->register(new SilexProviderTranslationServiceProvider());
  • 31. 31 app/routes.php $app->get('/', function() use ($app) {/*...*/}); $app->get('/about, function() use ($app) {/*...*/}); $app->match('/contact', function(Request $request) use ($app) { $form = $app['form.factory']->createBuilder('form') ->add('name') ->add('email') ->getForm() ; $form->handleRequest($request); if($form->isValid()) { $data = $form->getData(); // Mail it } return $app['twig']->render('contact.html.twig', array('form' => $form->createView())); })->method('GET|POST'); $app->get('/services', function() use ($app) {/*..});
  • 33. 33 For Services ● Implement SilexServiceProviderInterface ● Implement boot() and register()
  • 34. 34 For Controllers ● Implement SilexControllerProviderInterface ● Implement connect()
  • 35. 35 Sample Controller use SilexApplication; use SilexControllerProviderInterface; class BlogController implements ControllerProviderInterface { public function connect(Application $app) { $controllers = $app['controllers_factory']; $controllers->get('/', array($this, 'index')); $controllers->get('/inline', function() use ($app) { /* Logic Here */ }); } protected function index(Application $app) { /* Logic Here */ } } $app->mount('/blog', new BlogController());
  • 36. 36 Controllers as Services ● Ships with Silex ● Allows attaching objects to routes ● Favors dependency injection ● Framework Agnostic
  • 37. 37 Create our Object // src/MyApp/Controller/IndexController.php namespace MyAppController; use SilexApplication; use SymfonyComponentHttpFoundationRequest; class IndexController { public function indexAction(Application $app) { return $app['twig']->render('Index/index.html.twig'); } public function aboutAction(Application $app) { /* … */ } public function servicesAction(Application $app) { /* … */ } public function contactAction(Application $app) { /* … */ } }
  • 38. 38 Register Controllers as Services $config = include_once 'config/config.php'; $app['config'] = $config; $app->register(new SilexProviderTwigServiceProvider(), array( 'twig.path' => $app['config']['template_path'], )); $app->register(new SilexProviderFormServiceProvider()); $app->register(new SilexProviderTranslationServiceProvider()); $app['controller.index'] = $app->share(function() use ($app) { return new MyAppControllerIndexController(); });
  • 39. 39 Register our routes $app->get('/', 'controller.index:indexAction')->bind('homepage'); $app->get('/about', 'controller.index:aboutAction')->bind('about'); $app->get('/services', 'controller.index:servicesAction')->bind('services'); $app->match('/contact', 'controller.index:contactAction')->bind('contact')->method('GET|POST');
  • 40. 40 A Little Planning goes a Long Way
  • 42. 42 Thanks! ● http://joind.in/talk/view/12080 ● @dragonmantank ● chris@ctankersley.com