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

More Related Content

PDF
Silex: From nothing to an API
PDF
Silex and Twig (PHP Dorset talk)
PPTX
Intro to Silex
PDF
How to develop modern web application framework
PDF
Keeping it Small: Getting to know the Slim Micro Framework
PDF
Avinash Kundaliya: Javascript and WordPress
PDF
優しいWAFの作り方
PDF
Keeping it small - Getting to know the Slim PHP micro framework
Silex: From nothing to an API
Silex and Twig (PHP Dorset talk)
Intro to Silex
How to develop modern web application framework
Keeping it Small: Getting to know the Slim Micro Framework
Avinash Kundaliya: Javascript and WordPress
優しいWAFの作り方
Keeping it small - Getting to know the Slim PHP micro framework

What's hot (19)

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

Viewers also liked (13)

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

Similar to Complex Sites with Silex (20)

PDF
Silex Cheat Sheet
PDF
What's New In Laravel 5
PDF
Doctrine For Beginners
PDF
WCMTL 15 - Create your own shortcode (Fr)
PDF
Introduction to angular js
PPTX
REST API for your WP7 App
ODP
Creating REST Applications with the Slim Micro-Framework by Vikram Vaswani
KEY
How and why i roll my own node.js framework
PDF
Angular server side rendering - Strategies & Technics
PDF
NestJS
ODP
Pyramid deployment
PDF
Pyramid Deployment and Maintenance
ODP
Zend Framework 1.9 Setup & Using Zend_Tool
PDF
Hexagonal architecture
PDF
Refresh Austin - Intro to Dexy
PDF
Into the ZF2 Service Manager
PDF
symfony on action - WebTech 207
PPT
ZFConf 2010: Zend Framework & MVC, Model Implementation (Part 2, Dependency I...
PDF
A gently introduction to AngularJS
PDF
Mojolicious. Веб в коробке!
Silex Cheat Sheet
What's New In Laravel 5
Doctrine For Beginners
WCMTL 15 - Create your own shortcode (Fr)
Introduction to angular js
REST API for your WP7 App
Creating REST Applications with the Slim Micro-Framework by Vikram Vaswani
How and why i roll my own node.js framework
Angular server side rendering - Strategies & Technics
NestJS
Pyramid deployment
Pyramid Deployment and Maintenance
Zend Framework 1.9 Setup & Using Zend_Tool
Hexagonal architecture
Refresh Austin - Intro to Dexy
Into the ZF2 Service Manager
symfony on action - WebTech 207
ZFConf 2010: Zend Framework & MVC, Model Implementation (Part 2, Dependency I...
A gently introduction to AngularJS
Mojolicious. Веб в коробке!

More from Chris Tankersley (20)

PDF
8 Rules for Better Applications - PHP Tek 2025
PDF
The Art of API Design - PHP Tek 2025, Chris Tankersley
PDF
Docker is Dead: Long Live Containers
PDF
Bend time to your will with git
PDF
Using PHP Functions! (Not those functions, Google Cloud Functions)
PDF
Dead Simple APIs with OpenAPI
PDF
Killer Docker Workflows for Development
PDF
You Got Async in my PHP!
ODP
Docker for Developers - PHP Detroit 2018
ODP
Docker for Developers
ODP
They are Watching You
ODP
BASHing at the CLI - Midwest PHP 2018
PDF
You Were Lied To About Optimization
ODP
Docker for PHP Developers - php[world] 2017
ODP
Docker for PHP Developers - Madison PHP 2017
ODP
Docker for Developers - php[tek] 2017
ODP
Why Docker? Dayton PHP, April 2017
PPTX
OOP Is More Then Cars and Dogs - Midwest PHP 2017
PPTX
From Docker to Production - SunshinePHP 2017
PPTX
Docker for Developers - Sunshine PHP
8 Rules for Better Applications - PHP Tek 2025
The Art of API Design - PHP Tek 2025, Chris Tankersley
Docker is Dead: Long Live Containers
Bend time to your will with git
Using PHP Functions! (Not those functions, Google Cloud Functions)
Dead Simple APIs with OpenAPI
Killer Docker Workflows for Development
You Got Async in my PHP!
Docker for Developers - PHP Detroit 2018
Docker for Developers
They are Watching You
BASHing at the CLI - Midwest PHP 2018
You Were Lied To About Optimization
Docker for PHP Developers - php[world] 2017
Docker for PHP Developers - Madison PHP 2017
Docker for Developers - php[tek] 2017
Why Docker? Dayton PHP, April 2017
OOP Is More Then Cars and Dogs - Midwest PHP 2017
From Docker to Production - SunshinePHP 2017
Docker for Developers - Sunshine PHP

Recently uploaded (20)

PPTX
Internet of Everything -Basic concepts details
PPTX
Module 1 Introduction to Web Programming .pptx
PDF
Taming the Chaos: How to Turn Unstructured Data into Decisions
PPTX
AI-driven Assurance Across Your End-to-end Network With ThousandEyes
PDF
giants, standing on the shoulders of - by Daniel Stenberg
PDF
Flame analysis and combustion estimation using large language and vision assi...
DOCX
search engine optimization ppt fir known well about this
PDF
CXOs-Are-you-still-doing-manual-DevOps-in-the-age-of-AI.pdf
PDF
Consumable AI The What, Why & How for Small Teams.pdf
PPTX
Training Program for knowledge in solar cell and solar industry
PDF
“A New Era of 3D Sensing: Transforming Industries and Creating Opportunities,...
PDF
Produktkatalog für HOBO Datenlogger, Wetterstationen, Sensoren, Software und ...
PPTX
Custom Battery Pack Design Considerations for Performance and Safety
PDF
NewMind AI Weekly Chronicles – August ’25 Week IV
PDF
Enhancing plagiarism detection using data pre-processing and machine learning...
PDF
The influence of sentiment analysis in enhancing early warning system model f...
PDF
Accessing-Finance-in-Jordan-MENA 2024 2025.pdf
DOCX
Basics of Cloud Computing - Cloud Ecosystem
PDF
Co-training pseudo-labeling for text classification with support vector machi...
PDF
4 layer Arch & Reference Arch of IoT.pdf
Internet of Everything -Basic concepts details
Module 1 Introduction to Web Programming .pptx
Taming the Chaos: How to Turn Unstructured Data into Decisions
AI-driven Assurance Across Your End-to-end Network With ThousandEyes
giants, standing on the shoulders of - by Daniel Stenberg
Flame analysis and combustion estimation using large language and vision assi...
search engine optimization ppt fir known well about this
CXOs-Are-you-still-doing-manual-DevOps-in-the-age-of-AI.pdf
Consumable AI The What, Why & How for Small Teams.pdf
Training Program for knowledge in solar cell and solar industry
“A New Era of 3D Sensing: Transforming Industries and Creating Opportunities,...
Produktkatalog für HOBO Datenlogger, Wetterstationen, Sensoren, Software und ...
Custom Battery Pack Design Considerations for Performance and Safety
NewMind AI Weekly Chronicles – August ’25 Week IV
Enhancing plagiarism detection using data pre-processing and machine learning...
The influence of sentiment analysis in enhancing early warning system model f...
Accessing-Finance-in-Jordan-MENA 2024 2025.pdf
Basics of Cloud Computing - Cloud Ecosystem
Co-training pseudo-labeling for text classification with support vector machi...
4 layer Arch & Reference Arch of IoT.pdf

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 ● [email protected]