SlideShare a Scribd company logo
Creating your own framework using
Symfony2 Components
Contents
• Symfony Introduction
• Going OOP with the HttpFoundation Component
• Front controller Pattern
• Routing Component
• HttpKernel Component : The Controller Resolver
• EventDispatcher Component
• HttpKernel Component : TheHttpKernel class
• Dependency-Injection Component
• Technical facts about Symfony
• Final code of our framework
• Resources
Symfony Introduction
Symfony is a reusable set of standalone, decoupled and cohesive PHP components
that solve common web development problems.
Symfony Components
• HttpFoundation
• Routing
• HttpKernel
• Debug
• EventDispatcher
• DependencyInjection
• Security
• Validator
• Yaml
.
Fun to Install !!
{
"require": {
"symfony/http-foundation": "^3.0”,
"symfony/routing": "^3.0",
"symfony/http-kernel": "^3.0",
"symfony/debug": "^3.0“,
"symfony/event-dispatcher": "^3.0“,
"symfony/dependency-injection": "^3.0",
"symfony/yaml": "^3.0",
}
}composer install;
<?php
require_once __DIR__.'/../vendor/autoload.php';
Going OOP with the HttpFoundation Component
$_GET
$_POST
$_COOKIE
$_SESSION
$_FILES
Request
Response
Cookie
Session
UploadedFile
RedirectResponse
documentation
$input = isset($_GET['name']) ? $_GET['name'] : 'World';
header('Content-Type: text/html; charset=utf-8');
printf('Hello %s', htmlspecialchars($input, ENT_QUOTES, 'UTF-8'));
Starting with our framwork
// simplex/index.php
require_once __DIR__.'/vendor/autoload.php';
use SymfonyComponentHttpFoundationRequest;
use SymfonyComponentHttpFoundationResponse;
$request = Request::createFromGlobals();
$input = $request->get('name', 'World');
$response = new Response(sprintf('Hello %s', htmlspecialchars($input, ENT_QUOTES, 'UTF-8')));
$response->send();
$ composer require symfony/http-foundation
Lets see code snippet after HttpFoundation (pt1)
Utilising “Front Controller” design pattern
• src/ : source code for our app (not publiciy accessible)
vendor/ : any third-party Libraries
web/ : use to house anything that needs to be accessible from the web server,
including assets & out front controller
Lets see code snippet after implementing FrontController
(pt2)
Mapping URLs with Routing Component
Routing component documentation
Basic parts of routing system
• RouteCollection – contains the route definitions (instances of class Route)
• RequestContext - contains information about Request
• UrlMatcher – performs the mapping of the Request to a single Route
$ composer require symfony/routing
New version of our framework using Routing Component
// simplex/web/index.php
require_once __DIR__.'/vendor/autoload.php';
$request = Request::createFromGlobals();
$routes = include __DIR__.'/../src/routes.php';
$context = new RoutingRequestContext();
$context->fromRequest($request);
$matcher = new RoutingMatcherUrlMatcher($routes, $context);
try {
extract($matcher->match($request->getPathInfo()), EXTR_SKIP);
ob_start();
include sprintf(__DIR__.'/../src/pages/%s.php', $_route);
$response = new Response(ob_get_clean());
} catch (RoutingExceptionResourceNotFoundException $e) {
$response = new Response('Not Found', 404);
} catch (Exception $e) {
$response = new Response('An error occurred', 500);
}
$response->send();
// example.com/src/routes.php
use SymfonyComponentRouting;
$routes = new RoutingRouteCollection();
$routes->add('hello', new RoutingRoute('/hello
array('name' => 'World')));
$routes->add('bye', new RoutingRoute('/bye'))
return $routes;
Lets see code snippet after implementing
RoutingComponent (pt3)
Introducing Controllers in Routing Component
// simplex/src/routes.php
$routes->add('hello', new RoutingRoute('/hello/{name}', array(
'name' => 'World',
'_controller' => function ($request) {
// $foo will be available in the template
$request->attributes->set('foo', 'bar');
$response = render_template($request);
// change some header
$response->headers->set('Content-Type', 'text/plain');
return $response;
}
)));
// simplex/web/index.php
try {
$request->attributes->add($matcher->match($request->getPathInfo()));
} catch (RoutingExceptionResourceNotFoundException $e) {
$response = new Response('Not Found', 404);
} catch (Exception $e) {
$response = new Response('An error occurred', 500);
}
$response = call_user_func($request->attributes->get('_controller'), request);
'_controller' => function ($request) {
// $foo will be available in the template
$request->attributes->set('foo', 'bar');
$response = render_template($request);
// change some header
$response->headers->set('Content-Type','text/plain');
return $response;
}
Lets see code snippet after implementing Controller in
RoutingComponent (pt4)
Introducing Controllers in Routing Component (continued)
$ composer require symfony/http-kernel
A non-desirable side-effect , noticed it ??
HttpKernel Component : The Controller Resolver
Using Controller Resolver
public function indexAction(Request $request)
// won't work
public function indexAction($request)
// will inject request and year attribute from request
public function indexAction(Request $request, $year)
Integrating Controller Resolver in our framework
Lets see code snippet after implementing
ControllerResolver (pt5)
This completes part 1 of series
Projects using Symfony2 components
Downsides in our framework code
• We need to copy front.php each time we create a new website.
It would be nice if we could wrap this code into a proper class
Separation of concerns (MVC)
Separation of concerns (MVC) continued..
Separation of concerns (MVC) continued..
Updating routes.php
Lets see code snippet (part - 6)
Separation of concerns (MVC) continued..
Our application has now Four Different layers :
1. web/front.php: The front controller, initializes our application
2. src/Simplex: The reusable framework class
3. src/Calendar: Application specific code (controllers & models)
4. src/routes.php: application specific route configurations
EventDispatcher Component
documentation
On Response Listener
Registering Listener and dispatching event from front controller
Moved listener code into its own class
Lets see code snippet (part - 7)
Using Subscribers instead of Listeners
Registering subscriber
Creating Subscriber
The HttpKernel component
Code implementation part 8
Creating your own framework on top of Symfony2 Components
Resources
Resources:
▸ Symfony2 official page https://symfony.com/
▸ Symfony Book
▸ Symfony2 Components
▸ Knp University
▸ Create Framework using Symfony2 components blog
▸ BeIntent Project code structure
THANKS!
Any questions?

More Related Content

What's hot (20)

Rest api titouan benoit
Rest api titouan benoit
Titouan BENOIT
 
Some tips to improve developer experience with Symfony
Some tips to improve developer experience with Symfony
tyomo4ka
 
Laravel 5
Laravel 5
Sudip Simkhada
 
Laravel for Web Artisans
Laravel for Web Artisans
Raf Kewl
 
Ecto and Phoenix: Doing Web With Elixir
Ecto and Phoenix: Doing Web With Elixir
Yurii Bodarev
 
170517 damien gérard framework facebook
170517 damien gérard framework facebook
Geeks Anonymes
 
Symfony3 w duecie z Vue.js
Symfony3 w duecie z Vue.js
X-Coding IT Studio
 
Laravel Beginners Tutorial 2
Laravel Beginners Tutorial 2
Vikas Chauhan
 
Workshop: Symfony2 Intruduction: (Controller, Routing, Model)
Workshop: Symfony2 Intruduction: (Controller, Routing, Model)
Antonio Peric-Mazar
 
Adventures in Laravel 5 SunshinePHP 2016 Tutorial
Adventures in Laravel 5 SunshinePHP 2016 Tutorial
Joe Ferguson
 
Laravel 5 In Depth
Laravel 5 In Depth
Kirk Bushell
 
Phoenix demysitify, with fun
Phoenix demysitify, with fun
Tai An Su
 
Elefrant [ng-Poznan]
Elefrant [ng-Poznan]
Marcos Latorre
 
Introduction to laravel framework
Introduction to laravel framework
Ahmad Fatoni
 
Brief Introduction to Ember
Brief Introduction to Ember
Vinay B
 
Rails web api 开发
Rails web api 开发
shaokun
 
More to RoC weibo
More to RoC weibo
shaokun
 
Laravel Design Patterns
Laravel Design Patterns
Bobby Bouwmann
 
Laravel5 Introduction and essentials
Laravel5 Introduction and essentials
Pramod Kadam
 
Red5 - PHUG Workshops
Red5 - PHUG Workshops
Brendan Sera-Shriar
 
Rest api titouan benoit
Rest api titouan benoit
Titouan BENOIT
 
Some tips to improve developer experience with Symfony
Some tips to improve developer experience with Symfony
tyomo4ka
 
Laravel for Web Artisans
Laravel for Web Artisans
Raf Kewl
 
Ecto and Phoenix: Doing Web With Elixir
Ecto and Phoenix: Doing Web With Elixir
Yurii Bodarev
 
170517 damien gérard framework facebook
170517 damien gérard framework facebook
Geeks Anonymes
 
Laravel Beginners Tutorial 2
Laravel Beginners Tutorial 2
Vikas Chauhan
 
Workshop: Symfony2 Intruduction: (Controller, Routing, Model)
Workshop: Symfony2 Intruduction: (Controller, Routing, Model)
Antonio Peric-Mazar
 
Adventures in Laravel 5 SunshinePHP 2016 Tutorial
Adventures in Laravel 5 SunshinePHP 2016 Tutorial
Joe Ferguson
 
Laravel 5 In Depth
Laravel 5 In Depth
Kirk Bushell
 
Phoenix demysitify, with fun
Phoenix demysitify, with fun
Tai An Su
 
Introduction to laravel framework
Introduction to laravel framework
Ahmad Fatoni
 
Brief Introduction to Ember
Brief Introduction to Ember
Vinay B
 
Rails web api 开发
Rails web api 开发
shaokun
 
More to RoC weibo
More to RoC weibo
shaokun
 
Laravel Design Patterns
Laravel Design Patterns
Bobby Bouwmann
 
Laravel5 Introduction and essentials
Laravel5 Introduction and essentials
Pramod Kadam
 

Viewers also liked (16)

The Peoples Party!
The Peoples Party!
msdanielleg
 
Presupuesto publico
Presupuesto publico
ajuzcategui
 
How i am going to act on feedback
How i am going to act on feedback
SamanthaWilsonn
 
Enhancing Life Skill for Learning to Live Together
Enhancing Life Skill for Learning to Live Together
University of Hyderabad, Telangana, India
 
Times Square
Times Square
Ulrico Montefiore
 
Swachha salila mohapatra
Swachha salila mohapatra
University of Hyderabad, Telangana, India
 
trabajo diapositivas cassava
trabajo diapositivas cassava
Laura Figueroa
 
Font Research
Font Research
SamanthaWilsonn
 
Skills developed whilst creating the website
Skills developed whilst creating the website
SamanthaWilsonn
 
Power system volume-1
Power system volume-1
HEMANTH SAI KENGAM
 
Pankajini pani
Pankajini pani
University of Hyderabad, Telangana, India
 
Why We Disagree About Climate Change - Book Review
Why We Disagree About Climate Change - Book Review
Rose K.
 
Mirror
Mirror
carlos hernandez
 
Evo slides fabio_lo_savio
Evo slides fabio_lo_savio
Fabio Savio
 
Ad

Similar to Creating your own framework on top of Symfony2 Components (20)

Symfony 2 (PHP Quebec 2009)
Symfony 2 (PHP Quebec 2009)
Fabien Potencier
 
Symfony 2 (PHP day 2009)
Symfony 2 (PHP day 2009)
Fabien Potencier
 
Drupal symfony
Drupal symfony
Tuz Valeriy
 
симфони это не страшно
симфони это не страшно
DrupalCamp Kyiv Рысь
 
Symfony 2.0
Symfony 2.0
GrUSP
 
Create Your Own Framework by Fabien Potencier
Create Your Own Framework by Fabien Potencier
Himel Nag Rana
 
Symfony2 San Francisco Meetup 2009
Symfony2 San Francisco Meetup 2009
Fabien Potencier
 
Hands-on with the Symfony2 Framework
Hands-on with the Symfony2 Framework
Ryan Weaver
 
Silex Cheat Sheet
Silex Cheat Sheet
Andréia Bohner
 
Silex Cheat Sheet
Silex Cheat Sheet
Andréia Bohner
 
Drupal users group_symfony2
Drupal users group_symfony2
Brian Zitzow
 
Symfony
Symfony
Артём Курапов
 
Symfony quick tour_2.3
Symfony quick tour_2.3
Frédéric Delorme
 
Symfony components in the wild, PHPNW12
Symfony components in the wild, PHPNW12
Jakub Zalas
 
Symfony Live 09 Symfony 2
Symfony Live 09 Symfony 2
narkoza
 
Symfony 2.0 on PHP 5.3
Symfony 2.0 on PHP 5.3
Fabien Potencier
 
Forget about Index.php and build you applications around HTTP - PHPers Cracow
Forget about Index.php and build you applications around HTTP - PHPers Cracow
Kacper Gunia
 
Symfony2 - Request to Response
Symfony2 - Request to Response
Palko Lenard
 
Introduction to Symfony Components and helper components in Drupal 8
Introduction to Symfony Components and helper components in Drupal 8
Ankit Babbar
 
Introduction to Symfony Components and helper components in Drupal 8
Introduction to Symfony Components and helper components in Drupal 8
Ankit Babbar
 
Symfony 2 (PHP Quebec 2009)
Symfony 2 (PHP Quebec 2009)
Fabien Potencier
 
Symfony 2.0
Symfony 2.0
GrUSP
 
Create Your Own Framework by Fabien Potencier
Create Your Own Framework by Fabien Potencier
Himel Nag Rana
 
Symfony2 San Francisco Meetup 2009
Symfony2 San Francisco Meetup 2009
Fabien Potencier
 
Hands-on with the Symfony2 Framework
Hands-on with the Symfony2 Framework
Ryan Weaver
 
Drupal users group_symfony2
Drupal users group_symfony2
Brian Zitzow
 
Symfony components in the wild, PHPNW12
Symfony components in the wild, PHPNW12
Jakub Zalas
 
Symfony Live 09 Symfony 2
Symfony Live 09 Symfony 2
narkoza
 
Forget about Index.php and build you applications around HTTP - PHPers Cracow
Forget about Index.php and build you applications around HTTP - PHPers Cracow
Kacper Gunia
 
Symfony2 - Request to Response
Symfony2 - Request to Response
Palko Lenard
 
Introduction to Symfony Components and helper components in Drupal 8
Introduction to Symfony Components and helper components in Drupal 8
Ankit Babbar
 
Introduction to Symfony Components and helper components in Drupal 8
Introduction to Symfony Components and helper components in Drupal 8
Ankit Babbar
 
Ad

Recently uploaded (20)

Viral>Wondershare Filmora 14.5.18.12900 Crack Free Download
Viral>Wondershare Filmora 14.5.18.12900 Crack Free Download
Puppy jhon
 
FIDO Seminar: Perspectives on Passkeys & Consumer Adoption.pptx
FIDO Seminar: Perspectives on Passkeys & Consumer Adoption.pptx
FIDO Alliance
 
Edge-banding-machines-edgeteq-s-200-en-.pdf
Edge-banding-machines-edgeteq-s-200-en-.pdf
AmirStern2
 
FIDO Seminar: New Data: Passkey Adoption in the Workforce.pptx
FIDO Seminar: New Data: Passkey Adoption in the Workforce.pptx
FIDO Alliance
 
“Why It’s Critical to Have an Integrated Development Methodology for Edge AI,...
“Why It’s Critical to Have an Integrated Development Methodology for Edge AI,...
Edge AI and Vision Alliance
 
Murdledescargadarkweb.pdfvolumen1 100 elementary
Murdledescargadarkweb.pdfvolumen1 100 elementary
JorgeSemperteguiMont
 
High Availability On-Premises FME Flow.pdf
High Availability On-Premises FME Flow.pdf
Safe Software
 
“From Enterprise to Makers: Driving Vision AI Innovation at the Extreme Edge,...
“From Enterprise to Makers: Driving Vision AI Innovation at the Extreme Edge,...
Edge AI and Vision Alliance
 
Artificial Intelligence in the Nonprofit Boardroom.pdf
Artificial Intelligence in the Nonprofit Boardroom.pdf
OnBoard
 
June Patch Tuesday
June Patch Tuesday
Ivanti
 
TrustArc Webinar - 2025 Global Privacy Survey
TrustArc Webinar - 2025 Global Privacy Survey
TrustArc
 
Integration of Utility Data into 3D BIM Models Using a 3D Solids Modeling Wor...
Integration of Utility Data into 3D BIM Models Using a 3D Solids Modeling Wor...
Safe Software
 
FIDO Seminar: Targeting Trust: The Future of Identity in the Workforce.pptx
FIDO Seminar: Targeting Trust: The Future of Identity in the Workforce.pptx
FIDO Alliance
 
FIDO Seminar: Evolving Landscape of Post-Quantum Cryptography.pptx
FIDO Seminar: Evolving Landscape of Post-Quantum Cryptography.pptx
FIDO Alliance
 
Oracle Cloud and AI Specialization Program
Oracle Cloud and AI Specialization Program
VICTOR MAESTRE RAMIREZ
 
Enabling BIM / GIS integrations with Other Systems with FME
Enabling BIM / GIS integrations with Other Systems with FME
Safe Software
 
Oracle Cloud Infrastructure AI Foundations
Oracle Cloud Infrastructure AI Foundations
VICTOR MAESTRE RAMIREZ
 
PyData - Graph Theory for Multi-Agent Integration
PyData - Graph Theory for Multi-Agent Integration
barqawicloud
 
“Addressing Evolving AI Model Challenges Through Memory and Storage,” a Prese...
“Addressing Evolving AI Model Challenges Through Memory and Storage,” a Prese...
Edge AI and Vision Alliance
 
Supporting the NextGen 911 Digital Transformation with FME
Supporting the NextGen 911 Digital Transformation with FME
Safe Software
 
Viral>Wondershare Filmora 14.5.18.12900 Crack Free Download
Viral>Wondershare Filmora 14.5.18.12900 Crack Free Download
Puppy jhon
 
FIDO Seminar: Perspectives on Passkeys & Consumer Adoption.pptx
FIDO Seminar: Perspectives on Passkeys & Consumer Adoption.pptx
FIDO Alliance
 
Edge-banding-machines-edgeteq-s-200-en-.pdf
Edge-banding-machines-edgeteq-s-200-en-.pdf
AmirStern2
 
FIDO Seminar: New Data: Passkey Adoption in the Workforce.pptx
FIDO Seminar: New Data: Passkey Adoption in the Workforce.pptx
FIDO Alliance
 
“Why It’s Critical to Have an Integrated Development Methodology for Edge AI,...
“Why It’s Critical to Have an Integrated Development Methodology for Edge AI,...
Edge AI and Vision Alliance
 
Murdledescargadarkweb.pdfvolumen1 100 elementary
Murdledescargadarkweb.pdfvolumen1 100 elementary
JorgeSemperteguiMont
 
High Availability On-Premises FME Flow.pdf
High Availability On-Premises FME Flow.pdf
Safe Software
 
“From Enterprise to Makers: Driving Vision AI Innovation at the Extreme Edge,...
“From Enterprise to Makers: Driving Vision AI Innovation at the Extreme Edge,...
Edge AI and Vision Alliance
 
Artificial Intelligence in the Nonprofit Boardroom.pdf
Artificial Intelligence in the Nonprofit Boardroom.pdf
OnBoard
 
June Patch Tuesday
June Patch Tuesday
Ivanti
 
TrustArc Webinar - 2025 Global Privacy Survey
TrustArc Webinar - 2025 Global Privacy Survey
TrustArc
 
Integration of Utility Data into 3D BIM Models Using a 3D Solids Modeling Wor...
Integration of Utility Data into 3D BIM Models Using a 3D Solids Modeling Wor...
Safe Software
 
FIDO Seminar: Targeting Trust: The Future of Identity in the Workforce.pptx
FIDO Seminar: Targeting Trust: The Future of Identity in the Workforce.pptx
FIDO Alliance
 
FIDO Seminar: Evolving Landscape of Post-Quantum Cryptography.pptx
FIDO Seminar: Evolving Landscape of Post-Quantum Cryptography.pptx
FIDO Alliance
 
Oracle Cloud and AI Specialization Program
Oracle Cloud and AI Specialization Program
VICTOR MAESTRE RAMIREZ
 
Enabling BIM / GIS integrations with Other Systems with FME
Enabling BIM / GIS integrations with Other Systems with FME
Safe Software
 
Oracle Cloud Infrastructure AI Foundations
Oracle Cloud Infrastructure AI Foundations
VICTOR MAESTRE RAMIREZ
 
PyData - Graph Theory for Multi-Agent Integration
PyData - Graph Theory for Multi-Agent Integration
barqawicloud
 
“Addressing Evolving AI Model Challenges Through Memory and Storage,” a Prese...
“Addressing Evolving AI Model Challenges Through Memory and Storage,” a Prese...
Edge AI and Vision Alliance
 
Supporting the NextGen 911 Digital Transformation with FME
Supporting the NextGen 911 Digital Transformation with FME
Safe Software
 

Creating your own framework on top of Symfony2 Components

  • 1. Creating your own framework using Symfony2 Components
  • 2. Contents • Symfony Introduction • Going OOP with the HttpFoundation Component • Front controller Pattern • Routing Component • HttpKernel Component : The Controller Resolver • EventDispatcher Component • HttpKernel Component : TheHttpKernel class • Dependency-Injection Component • Technical facts about Symfony • Final code of our framework • Resources
  • 3. Symfony Introduction Symfony is a reusable set of standalone, decoupled and cohesive PHP components that solve common web development problems. Symfony Components • HttpFoundation • Routing • HttpKernel • Debug • EventDispatcher • DependencyInjection • Security • Validator • Yaml . Fun to Install !! { "require": { "symfony/http-foundation": "^3.0”, "symfony/routing": "^3.0", "symfony/http-kernel": "^3.0", "symfony/debug": "^3.0“, "symfony/event-dispatcher": "^3.0“, "symfony/dependency-injection": "^3.0", "symfony/yaml": "^3.0", } }composer install; <?php require_once __DIR__.'/../vendor/autoload.php';
  • 4. Going OOP with the HttpFoundation Component $_GET $_POST $_COOKIE $_SESSION $_FILES Request Response Cookie Session UploadedFile RedirectResponse documentation
  • 5. $input = isset($_GET['name']) ? $_GET['name'] : 'World'; header('Content-Type: text/html; charset=utf-8'); printf('Hello %s', htmlspecialchars($input, ENT_QUOTES, 'UTF-8')); Starting with our framwork // simplex/index.php require_once __DIR__.'/vendor/autoload.php'; use SymfonyComponentHttpFoundationRequest; use SymfonyComponentHttpFoundationResponse; $request = Request::createFromGlobals(); $input = $request->get('name', 'World'); $response = new Response(sprintf('Hello %s', htmlspecialchars($input, ENT_QUOTES, 'UTF-8'))); $response->send(); $ composer require symfony/http-foundation
  • 6. Lets see code snippet after HttpFoundation (pt1)
  • 7. Utilising “Front Controller” design pattern • src/ : source code for our app (not publiciy accessible) vendor/ : any third-party Libraries web/ : use to house anything that needs to be accessible from the web server, including assets & out front controller
  • 8. Lets see code snippet after implementing FrontController (pt2)
  • 9. Mapping URLs with Routing Component Routing component documentation Basic parts of routing system • RouteCollection – contains the route definitions (instances of class Route) • RequestContext - contains information about Request • UrlMatcher – performs the mapping of the Request to a single Route $ composer require symfony/routing
  • 10. New version of our framework using Routing Component // simplex/web/index.php require_once __DIR__.'/vendor/autoload.php'; $request = Request::createFromGlobals(); $routes = include __DIR__.'/../src/routes.php'; $context = new RoutingRequestContext(); $context->fromRequest($request); $matcher = new RoutingMatcherUrlMatcher($routes, $context); try { extract($matcher->match($request->getPathInfo()), EXTR_SKIP); ob_start(); include sprintf(__DIR__.'/../src/pages/%s.php', $_route); $response = new Response(ob_get_clean()); } catch (RoutingExceptionResourceNotFoundException $e) { $response = new Response('Not Found', 404); } catch (Exception $e) { $response = new Response('An error occurred', 500); } $response->send(); // example.com/src/routes.php use SymfonyComponentRouting; $routes = new RoutingRouteCollection(); $routes->add('hello', new RoutingRoute('/hello array('name' => 'World'))); $routes->add('bye', new RoutingRoute('/bye')) return $routes;
  • 11. Lets see code snippet after implementing RoutingComponent (pt3)
  • 12. Introducing Controllers in Routing Component // simplex/src/routes.php $routes->add('hello', new RoutingRoute('/hello/{name}', array( 'name' => 'World', '_controller' => function ($request) { // $foo will be available in the template $request->attributes->set('foo', 'bar'); $response = render_template($request); // change some header $response->headers->set('Content-Type', 'text/plain'); return $response; } ))); // simplex/web/index.php try { $request->attributes->add($matcher->match($request->getPathInfo())); } catch (RoutingExceptionResourceNotFoundException $e) { $response = new Response('Not Found', 404); } catch (Exception $e) { $response = new Response('An error occurred', 500); } $response = call_user_func($request->attributes->get('_controller'), request); '_controller' => function ($request) { // $foo will be available in the template $request->attributes->set('foo', 'bar'); $response = render_template($request); // change some header $response->headers->set('Content-Type','text/plain'); return $response; }
  • 13. Lets see code snippet after implementing Controller in RoutingComponent (pt4)
  • 14. Introducing Controllers in Routing Component (continued)
  • 15. $ composer require symfony/http-kernel A non-desirable side-effect , noticed it ??
  • 16. HttpKernel Component : The Controller Resolver Using Controller Resolver public function indexAction(Request $request) // won't work public function indexAction($request) // will inject request and year attribute from request public function indexAction(Request $request, $year)
  • 18. Lets see code snippet after implementing ControllerResolver (pt5) This completes part 1 of series Projects using Symfony2 components
  • 19. Downsides in our framework code • We need to copy front.php each time we create a new website. It would be nice if we could wrap this code into a proper class
  • 21. Separation of concerns (MVC) continued..
  • 22. Separation of concerns (MVC) continued.. Updating routes.php
  • 23. Lets see code snippet (part - 6)
  • 24. Separation of concerns (MVC) continued.. Our application has now Four Different layers : 1. web/front.php: The front controller, initializes our application 2. src/Simplex: The reusable framework class 3. src/Calendar: Application specific code (controllers & models) 4. src/routes.php: application specific route configurations
  • 26. On Response Listener Registering Listener and dispatching event from front controller Moved listener code into its own class
  • 27. Lets see code snippet (part - 7)
  • 28. Using Subscribers instead of Listeners Registering subscriber Creating Subscriber
  • 29. The HttpKernel component Code implementation part 8
  • 31. Resources Resources: ▸ Symfony2 official page https://symfony.com/ ▸ Symfony Book ▸ Symfony2 Components ▸ Knp University ▸ Create Framework using Symfony2 components blog ▸ BeIntent Project code structure