Routing, Controllers & Templates
Route● A route is a map from a URL path to acontroller.● defined in the app/config/routing.ymlconfiguration file.● When yo...
Basic Route Configuration¶
● Symfony2 chooses the controller based on the_controller value from the routing configuration:● AcmeDemoBundle:Welcome:in...
Inside:src/Acme/DemoBundle/Controller/WelcomeController.php
Routing: Under the Hood
Other Examples
Routing with PlaceholdersFor example, suppose you want to match anyURL like:/blog/my-postor/blog/all-about-symfony
Required and Optional Placeholders
Possible Problem
Adding RequirementsFor other requirement options, please refer to:http://symfony.com/doc/current/book/routing.html
Using annotations
Including External RoutingResources¶
Prefixing Imported Routes
Visualizing & Debugging Routes● $ php app/console router:debug●● $ php app/console router:debug article_show●● $ php app/c...
Controllers
Controller● A controller is a PHP function you create thattakes information from the HTTP request andconstructs and return...
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 ...
The Request as a ControllerArgument
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 generateany text-based format (HTML, XML, CSV,LaTeX …)● Commonly-used...
Template Locations● By default, templates can live in two differentlocations:● app/Resources/views/– The applications view...
Template Suffix● index.html.twig● HTML format, Twig engine● index.html.php● HTML format, PHP engine● index.txt.twig● TXT f...
To render a template in Symfony● Symfony2 uses a bundle:controller:templatestring syntax for templates.●src/Acme/DemoBundl...
PHP template● a text file parsed by PHP that contains a mix oftext and PHP code
Twig templates● Twig allows you to write concise, readabletemplates that are more friendly to webdesigners and, in several...
Twig defines 3 kinds of delimiters:● {{ }}● Prints a variable or the result of an expression;● {% %}● Controls the logic o...
Variables passed to a template canbe strings, arrays, or even objects.
Template Inheritance and Layouts● template inheritance allows you to build a base"layout" template that contains all the c...
simple parent and childNOTE: 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 inthat template;● The more blocks your base templat...
Including other Templates● The best way to share a snippet of codebetween several distinct templates
Another example:
Embedding other Controllers● what if you want to embed the result of anothercontroller 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
Routing
Routing
Routing
Routing
Upcoming SlideShare
Loading in …5
×

Routing

630 views
519 views

Published on

Published in: Technology, Business
0 Comments
0 Likes
Statistics
Notes
  • Be the first to comment

  • Be the first to like this

No Downloads
Views
Total views
630
On SlideShare
0
From Embeds
0
Number of Embeds
2
Actions
Shares
0
Downloads
5
Comments
0
Likes
0
Embeds 0
No embeds

No notes for slide

Routing

  1. 1. Routing, Controllers & Templates
  2. 2. Route● A route is a map from a URL path to acontroller.● defined in the app/config/routing.ymlconfiguration file.● When youre in the dev environment - indicated bythe app_**dev**.php front controller - theapp/config/routing_dev.yml configuration file isalso loaded.
  3. 3. Basic Route Configuration¶
  4. 4. ● 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
  5. 5. Inside:src/Acme/DemoBundle/Controller/WelcomeController.php
  6. 6. Routing: Under the Hood
  7. 7. Other Examples
  8. 8. Routing with PlaceholdersFor example, suppose you want to match anyURL like:/blog/my-postor/blog/all-about-symfony
  9. 9. Required and Optional Placeholders
  10. 10. Possible Problem
  11. 11. Adding RequirementsFor other requirement options, please refer to:http://symfony.com/doc/current/book/routing.html
  12. 12. Using annotations
  13. 13. Including External RoutingResources¶
  14. 14. Prefixing Imported Routes
  15. 15. Visualizing & Debugging Routes● $ php app/console router:debug●● $ php app/console router:debug article_show●● $ php app/console router:match /blog/my-latest-post
  16. 16. Controllers
  17. 17. Controller● A controller is a PHP function you create thattakes information from the HTTP request andconstructs and returns an HTTP response (as aSymfony2 Response object).● The response could be an HTML page, an XMLdocument, a serialized JSON array, an image, aredirect, a 404 error or anything else you can dreamup.
  18. 18. A Simple Controller
  19. 19. Route Parameters as Controller Arguments
  20. 20. Route Parameters as Controller Arguments● The order of the controller arguments does not matter● Each required controller argument must match up with a routingparameter●
  21. 21. The Request as a ControllerArgument
  22. 22. The Base Controller Class
  23. 23. Common Controller Tasks
  24. 24. Redirecting
  25. 25. Forwarding
  26. 26. Rendering Templates¶
  27. 27. Managing Errors and 404 Pages
  28. 28. Generating URLs
  29. 29. Generating Absolute URLs
  30. 30. Generating URLs with Query Strings
  31. 31. Templates
  32. 32. Templates● A template is simply a text file that can generateany text-based format (HTML, XML, CSV,LaTeX …)● Commonly-used in Symfony:● PHP template● Twig template
  33. 33. Template Locations● By default, templates can live in two differentlocations:● 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/viewsdirectory (and subdirectories).
  34. 34. Template Suffix● index.html.twig● HTML format, Twig engine● index.html.php● HTML format, PHP engine● index.txt.twig● TXT format, Twig engine
  35. 35. To render a template in Symfony● Symfony2 uses a bundle:controller:templatestring syntax for templates.●src/Acme/DemoBundle/Resources/views/Demo/hello.html.twig.
  36. 36. PHP template● a text file parsed by PHP that contains a mix oftext and PHP code
  37. 37. Twig templates● Twig allows you to write concise, readabletemplates that are more friendly to webdesigners and, in several ways, more powerfulthan PHP templates
  38. 38. Twig defines 3 kinds of delimiters:● {{ }}● Prints a variable or the result of an expression;● {% %}● Controls the logic of the template; it is used toexecute for loops and if statements, for example.● {# #}● Comments can be included inside templates
  39. 39. Variables passed to a template canbe strings, arrays, or even objects.
  40. 40. Template Inheritance and Layouts● template inheritance allows you to build a base"layout" template that contains all the commonelements of your site defined as blocks.● A child template can extend the base layoutand override any of its blocks.
  41. 41. simple parent and childNOTE: Body content is not allowed in the child.
  42. 42. complex parent
  43. 43. complex child
  44. 44. sample result:
  45. 45. Tips● If you use {% extends %} in a template, it must be the first tag inthat template;● The more blocks your base templates have, the more flexibleyour layout will be;●● If you need to get the content of a block from the parenttemplate, you can use the {{ parent() }} function.
  46. 46. Including other Templates● The best way to share a snippet of codebetween several distinct templates
  47. 47. Another example:
  48. 48. Embedding other Controllers● what if you want to embed the result of anothercontroller in a template?
  49. 49. Includee: Controller + template
  50. 50. Includer: template
  51. 51. Creating Links between Pages● href=”/”● FULL PATH●href=”http://localhost/Symfony/web/app_dev.php”
  52. 52. Creating Links between Pages● href=”/article/this-is-a-sample-title”
  53. 53. Including Assets: images,JavaScripts, and stylesheets
  54. 54. Convention: parent
  55. 55. Convention: child
  56. 56. Back to you RC

×