SlideShare uma empresa Scribd logo
1 de 60
Routing, Controllers & Templates
Route
● A route is a map from a URL path to a
controller.
● defined in the app/config/routing.yml
configuration file.
● When you're in the dev environment - indicated by
the app_**dev**.php front controller - the
app/config/routing_dev.yml configuration file is
also loaded.
Basic Route Configuration¶
● Symfony2 chooses the controller based on the
_controller value from the routing configuration:
● AcmeDemoBundle:Welcome:index
– controller logical name
● indexAction method from
– AcmeDemoBundleControllerWelcomeController.php class
– can also be written as:
● AcmeDemoBundleControllerWelcomeController::indexAction
Inside:
src/Acme/DemoBundle/Controller/W
elcomeController.php
Routing: Under the Hood
Other Examples
Routing with Placeholders
For example, suppose you want to match any
URL like:
/blog/my-post
or
/blog/all-about-symfony
Required and Optional Placeholders
Possible Problem
Adding Requirements
For other requirement options, please refer to:
http://symfony.com/doc/current/book/routing.html
Using annotations
Including External Routing
Resources¶
Prefixing Imported Routes
Visualizing & Debugging Routes
● $ php app/console router:debug
●
● $ php app/console router:debug article_show
●
● $ php app/console router:match /blog/my-latest-
post
Controllers
Controller
● A controller is a PHP function you create that
takes information from the HTTP request and
constructs and returns an HTTP response (as a
Symfony2 Response object).
● The response could be an HTML page, an XML
document, a serialized JSON array, an image, a
redirect, a 404 error or anything else you can dream
up.
A Simple Controller
Route Parameters as Controller Arguments
Route Parameters as Controller Arguments
● The order of the controller arguments does not matter
● Each required controller argument must match up with a routing
parameter
●
The Request as a Controller
Argument
The Base Controller Class
Common Controller Tasks
Redirecting
Forwarding
Rendering Templates¶
Managing Errors and 404 Pages
Generating URLs
Generating Absolute URLs
Generating URLs with Query Strings
Templates
Templates
● A template is simply a text file that can generate
any text-based format (HTML, XML, CSV,
LaTeX …)
● Commonly-used in Symfony:
● PHP template
● Twig template
Template Locations
● By default, templates can live in two different
locations:
● app/Resources/views/
– The applications views directory can contain application-
wide base templates
● path/to/bundle/Resources/views/(controller)
– Each bundle houses its templates in its Resources/views
directory (and subdirectories).
Template Suffix
● index.html.twig
● HTML format, Twig engine
● index.html.php
● HTML format, PHP engine
● index.txt.twig
● TXT format, Twig engine
To render a template in Symfony
● Symfony2 uses a bundle:controller:template
string syntax for templates.
●
src/Acme/DemoBundle/Resources/views/Demo/hello.html.twig.
PHP template
● a text file parsed by PHP that contains a mix of
text and PHP code
Twig templates
● Twig allows you to write concise, readable
templates that are more friendly to web
designers and, in several ways, more powerful
than PHP templates
Twig defines 3 kinds of delimiters:
● {{ }}
● Prints a variable or the result of an expression;
● {% %}
● Controls the logic of the template; it is used to
execute for loops and if statements, for example.
● {# #}
● Comments can be included inside templates
Variables passed to a template can
be strings, arrays, or even objects.
Template Inheritance and Layouts
● template inheritance allows you to build a base
"layout" template that contains all the common
elements of your site defined as blocks.
● A child template can extend the base layout
and override any of its blocks.
simple parent and child
NOTE: Body content is not allowed in the child.
complex parent
complex child
sample result:
Tips
● If you use {% extends %} in a template, it must be the first tag in
that template;
● The more blocks your base templates have, the more flexible
your layout will be;
●
● If you need to get the content of a block from the parent
template, you can use the {{ parent() }} function.
Including other Templates
● The best way to share a snippet of code
between several distinct templates
Another example:
Embedding other Controllers
● what if you want to embed the result of another
controller in a template?
Includee: Controller + template
Includer: template
Creating Links between Pages
● href=”/”
● FULL PATH
●
href=”http://localhost/Symfony/web/app_dev.php”
Creating Links between Pages
● href=”/article/this-is-a-sample-title”
Including Assets: images,
JavaScripts, and stylesheets
Convention: parent
Convention: child
Back to you RC

Mais conteúdo relacionado

Mais procurados

Object Oriented Programming with Laravel - Session 3
Object Oriented Programming with Laravel - Session 3Object Oriented Programming with Laravel - Session 3
Object Oriented Programming with Laravel - Session 3
Shahrzad Peyman
 
JSP Processing
JSP ProcessingJSP Processing
JSP Processing
Sadhana28
 

Mais procurados (20)

Building a website with angular 2
Building a website with angular 2Building a website with angular 2
Building a website with angular 2
 
Object Oriented Programming with Laravel - Session 3
Object Oriented Programming with Laravel - Session 3Object Oriented Programming with Laravel - Session 3
Object Oriented Programming with Laravel - Session 3
 
Microsoft C# programming basics
Microsoft C# programming basics  Microsoft C# programming basics
Microsoft C# programming basics
 
servlets and jsp
servlets and jspservlets and jsp
servlets and jsp
 
JSP Processing
JSP ProcessingJSP Processing
JSP Processing
 
OpenCms Days 2014 - OpenCms content editor and pdf extensions
OpenCms Days 2014 - OpenCms content editor and pdf extensionsOpenCms Days 2014 - OpenCms content editor and pdf extensions
OpenCms Days 2014 - OpenCms content editor and pdf extensions
 
Cleveland Meetup July 15,2021 - Advanced Batch Processing Concepts
Cleveland Meetup July 15,2021 - Advanced Batch Processing ConceptsCleveland Meetup July 15,2021 - Advanced Batch Processing Concepts
Cleveland Meetup July 15,2021 - Advanced Batch Processing Concepts
 
Fundamentals of Extending Magento 2 - php[world] 2015
Fundamentals of Extending Magento 2 - php[world] 2015Fundamentals of Extending Magento 2 - php[world] 2015
Fundamentals of Extending Magento 2 - php[world] 2015
 
VS Saturday 2019 - Xamarin.Forms 4.x
VS Saturday 2019 - Xamarin.Forms 4.xVS Saturday 2019 - Xamarin.Forms 4.x
VS Saturday 2019 - Xamarin.Forms 4.x
 
Siebel Open UI Debugging (Siebel Open UI Training, Part 7)
Siebel Open UI Debugging (Siebel Open UI Training, Part 7)Siebel Open UI Debugging (Siebel Open UI Training, Part 7)
Siebel Open UI Debugging (Siebel Open UI Training, Part 7)
 
Plsql overview
Plsql overviewPlsql overview
Plsql overview
 
Writing extensible applications
Writing extensible applicationsWriting extensible applications
Writing extensible applications
 
Oracle PL/SQL exception handling
Oracle PL/SQL exception handlingOracle PL/SQL exception handling
Oracle PL/SQL exception handling
 
Angular 4 and TypeScript
Angular 4 and TypeScriptAngular 4 and TypeScript
Angular 4 and TypeScript
 
Adapter Design Pattern
Adapter Design PatternAdapter Design Pattern
Adapter Design Pattern
 
Laravel & Composer presentation - WebHostFace
Laravel & Composer presentation - WebHostFace Laravel & Composer presentation - WebHostFace
Laravel & Composer presentation - WebHostFace
 
Adapter Design Pattern
Adapter Design PatternAdapter Design Pattern
Adapter Design Pattern
 
INTRODUCTION TO JSP,JSP LIFE CYCLE, ANATOMY OF JSP PAGE AND JSP PROCESSING
INTRODUCTION TO JSP,JSP LIFE CYCLE, ANATOMY OF JSP PAGE  AND JSP PROCESSINGINTRODUCTION TO JSP,JSP LIFE CYCLE, ANATOMY OF JSP PAGE  AND JSP PROCESSING
INTRODUCTION TO JSP,JSP LIFE CYCLE, ANATOMY OF JSP PAGE AND JSP PROCESSING
 
Customizing the Presentation Model and Physical Renderer in Siebel Open UI
Customizing the Presentation Model and Physical Renderer in Siebel Open UICustomizing the Presentation Model and Physical Renderer in Siebel Open UI
Customizing the Presentation Model and Physical Renderer in Siebel Open UI
 
Bealls florida automation overview
Bealls florida automation overviewBealls florida automation overview
Bealls florida automation overview
 

Semelhante a Routing

Semelhante a Routing (20)

Moodle Development Best Pracitces
Moodle Development Best PracitcesMoodle Development Best Pracitces
Moodle Development Best Pracitces
 
Angular js 1.3 presentation for fed nov 2014
Angular js 1.3 presentation for fed   nov 2014Angular js 1.3 presentation for fed   nov 2014
Angular js 1.3 presentation for fed nov 2014
 
Building Web Applications with Zend Framework
Building Web Applications with Zend FrameworkBuilding Web Applications with Zend Framework
Building Web Applications with Zend Framework
 
Overview of MVC Framework - by software outsourcing company india
Overview of MVC Framework - by software outsourcing company indiaOverview of MVC Framework - by software outsourcing company india
Overview of MVC Framework - by software outsourcing company india
 
13th Sep, Drupal 7 advanced training by TCS
13th Sep, Drupal 7 advanced training by TCS 13th Sep, Drupal 7 advanced training by TCS
13th Sep, Drupal 7 advanced training by TCS
 
Workshop: Symfony2 Intruduction: (Controller, Routing, Model)
Workshop: Symfony2 Intruduction: (Controller, Routing, Model)Workshop: Symfony2 Intruduction: (Controller, Routing, Model)
Workshop: Symfony2 Intruduction: (Controller, Routing, Model)
 
Drupal 8 - Core and API Changes
Drupal 8 - Core and API ChangesDrupal 8 - Core and API Changes
Drupal 8 - Core and API Changes
 
Ez platform meetup, madrid 21 marzo 2018 english
Ez platform meetup, madrid 21 marzo 2018   englishEz platform meetup, madrid 21 marzo 2018   english
Ez platform meetup, madrid 21 marzo 2018 english
 
Mvc
MvcMvc
Mvc
 
EuroPython 2013 - Python3 TurboGears Training
EuroPython 2013 - Python3 TurboGears TrainingEuroPython 2013 - Python3 TurboGears Training
EuroPython 2013 - Python3 TurboGears Training
 
Strategies and Tips for Building Enterprise Drupal Applications - PNWDS 2013
Strategies and Tips for Building Enterprise Drupal Applications - PNWDS 2013Strategies and Tips for Building Enterprise Drupal Applications - PNWDS 2013
Strategies and Tips for Building Enterprise Drupal Applications - PNWDS 2013
 
AtoM feature development
AtoM feature developmentAtoM feature development
AtoM feature development
 
Into to drupal8
Into to drupal8Into to drupal8
Into to drupal8
 
sveltekit-en.pdf
sveltekit-en.pdfsveltekit-en.pdf
sveltekit-en.pdf
 
Applying software engineering to configuration management
Applying software engineering to configuration managementApplying software engineering to configuration management
Applying software engineering to configuration management
 
Drupal Best Practices
Drupal Best PracticesDrupal Best Practices
Drupal Best Practices
 
An Overview of Angular 4
An Overview of Angular 4 An Overview of Angular 4
An Overview of Angular 4
 
Getting Started with Zend Framework
Getting Started with Zend FrameworkGetting Started with Zend Framework
Getting Started with Zend Framework
 
Overview of the AngularJS framework
Overview of the AngularJS framework Overview of the AngularJS framework
Overview of the AngularJS framework
 
Django by rj
Django by rjDjango by rj
Django by rj
 

Último

Cloud Frontiers: A Deep Dive into Serverless Spatial Data and FME
Cloud Frontiers:  A Deep Dive into Serverless Spatial Data and FMECloud Frontiers:  A Deep Dive into Serverless Spatial Data and FME
Cloud Frontiers: A Deep Dive into Serverless Spatial Data and FME
Safe Software
 
+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...
?#DUbAI#??##{{(☎️+971_581248768%)**%*]'#abortion pills for sale in dubai@
 
Cloud Frontiers: A Deep Dive into Serverless Spatial Data and FME
Cloud Frontiers:  A Deep Dive into Serverless Spatial Data and FMECloud Frontiers:  A Deep Dive into Serverless Spatial Data and FME
Cloud Frontiers: A Deep Dive into Serverless Spatial Data and FME
Safe Software
 
Modular Monolith - a Practical Alternative to Microservices @ Devoxx UK 2024
Modular Monolith - a Practical Alternative to Microservices @ Devoxx UK 2024Modular Monolith - a Practical Alternative to Microservices @ Devoxx UK 2024
Modular Monolith - a Practical Alternative to Microservices @ Devoxx UK 2024
Victor Rentea
 
Finding Java's Hidden Performance Traps @ DevoxxUK 2024
Finding Java's Hidden Performance Traps @ DevoxxUK 2024Finding Java's Hidden Performance Traps @ DevoxxUK 2024
Finding Java's Hidden Performance Traps @ DevoxxUK 2024
Victor Rentea
 
Why Teams call analytics are critical to your entire business
Why Teams call analytics are critical to your entire businessWhy Teams call analytics are critical to your entire business
Why Teams call analytics are critical to your entire business
panagenda
 

Último (20)

Cloud Frontiers: A Deep Dive into Serverless Spatial Data and FME
Cloud Frontiers:  A Deep Dive into Serverless Spatial Data and FMECloud Frontiers:  A Deep Dive into Serverless Spatial Data and FME
Cloud Frontiers: A Deep Dive into Serverless Spatial Data and FME
 
+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...
 
Corporate and higher education May webinar.pptx
Corporate and higher education May webinar.pptxCorporate and higher education May webinar.pptx
Corporate and higher education May webinar.pptx
 
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...
 
Biography Of Angeliki Cooney | Senior Vice President Life Sciences | Albany, ...
Biography Of Angeliki Cooney | Senior Vice President Life Sciences | Albany, ...Biography Of Angeliki Cooney | Senior Vice President Life Sciences | Albany, ...
Biography Of Angeliki Cooney | Senior Vice President Life Sciences | Albany, ...
 
Apidays New York 2024 - Accelerating FinTech Innovation by Vasa Krishnan, Fin...
Apidays New York 2024 - Accelerating FinTech Innovation by Vasa Krishnan, Fin...Apidays New York 2024 - Accelerating FinTech Innovation by Vasa Krishnan, Fin...
Apidays New York 2024 - Accelerating FinTech Innovation by Vasa Krishnan, Fin...
 
Exploring the Future Potential of AI-Enabled Smartphone Processors
Exploring the Future Potential of AI-Enabled Smartphone ProcessorsExploring the Future Potential of AI-Enabled Smartphone Processors
Exploring the Future Potential of AI-Enabled Smartphone Processors
 
Apidays New York 2024 - The Good, the Bad and the Governed by David O'Neill, ...
Apidays New York 2024 - The Good, the Bad and the Governed by David O'Neill, ...Apidays New York 2024 - The Good, the Bad and the Governed by David O'Neill, ...
Apidays New York 2024 - The Good, the Bad and the Governed by David O'Neill, ...
 
Cloud Frontiers: A Deep Dive into Serverless Spatial Data and FME
Cloud Frontiers:  A Deep Dive into Serverless Spatial Data and FMECloud Frontiers:  A Deep Dive into Serverless Spatial Data and FME
Cloud Frontiers: A Deep Dive into Serverless Spatial Data and FME
 
Modular Monolith - a Practical Alternative to Microservices @ Devoxx UK 2024
Modular Monolith - a Practical Alternative to Microservices @ Devoxx UK 2024Modular Monolith - a Practical Alternative to Microservices @ Devoxx UK 2024
Modular Monolith - a Practical Alternative to Microservices @ Devoxx UK 2024
 
FWD Group - Insurer Innovation Award 2024
FWD Group - Insurer Innovation Award 2024FWD Group - Insurer Innovation Award 2024
FWD Group - Insurer Innovation Award 2024
 
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...
 
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
 
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
 
Apidays New York 2024 - The value of a flexible API Management solution for O...
Apidays New York 2024 - The value of a flexible API Management solution for O...Apidays New York 2024 - The value of a flexible API Management solution for O...
Apidays New York 2024 - The value of a flexible API Management solution for O...
 
Finding Java's Hidden Performance Traps @ DevoxxUK 2024
Finding Java's Hidden Performance Traps @ DevoxxUK 2024Finding Java's Hidden Performance Traps @ DevoxxUK 2024
Finding Java's Hidden Performance Traps @ DevoxxUK 2024
 
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
 
MS Copilot expands with MS Graph connectors
MS Copilot expands with MS Graph connectorsMS Copilot expands with MS Graph connectors
MS Copilot expands with MS Graph connectors
 
Why Teams call analytics are critical to your entire business
Why Teams call analytics are critical to your entire businessWhy Teams call analytics are critical to your entire business
Why Teams call analytics are critical to your entire business
 
Ransomware_Q4_2023. The report. [EN].pdf
Ransomware_Q4_2023. The report. [EN].pdfRansomware_Q4_2023. The report. [EN].pdf
Ransomware_Q4_2023. The report. [EN].pdf
 

Routing