REST Controllers and Routing
Controllers, Routes, Responses
Table of Contents
• Controller
• Routing
• Request and Response
• REST
2
CONTROLLER
Controller
• Only concerned with Controlling
• Not responsible for the doing
• Takes care of web specific stuff like authentication,
cookies, request variables, query string
4
Controller
php bin/console make:controller
5
Controller
Location: src/Controller directory
6
Controller
• Controller names should end with "Controller"
• Controller action names should end with "Action"
7
Controller
• Must always return Response:
• 5-10-20 rule:
– 5 or less class variables
– 10 or less actions
– 20 or less lines of code for each action
8
Controller
• Requests, Controller, Response Lifecycle
9
Controller
• Symfony’s Base Controller
• Provides access to helper methods and the service
container
• Won’t change your controller workflow
10
Controller
• Symfony’s Base Controller - helper methods example
• Symfony’s Base Controller - services example
11
Controller
• Example
12
Routing
Routes and Mapping to Controller
Routing
• Process of breaking the URL into components and
deciding what script to call
14
Routing
• Four ways to define routes:
– Annotations
– YAML
– XML
– PHP
• Only one type of configuration is allowed
• No difference in execution time
15
Routing
• Annotation: @Route
16
Routing
• Configure different HTTP methods for the route
– Annotation: @Method
17
Routing
• Route Parameters
• Annotation
18
Routing
• Route Parameters
• Additional parameters are converted to query parameters
• Parameters names passed to the controller must match route
parameters names
• Parameters and route parameters names must be the same
19
Request & Response
Request & Response
• Symfony Application Lifecycle
21
Request & Response
Symfony Application Lifecycle
• Client sends an HTTP Request
• For each request the front controller is executed Index file
(app.php or app_dev.php)
• Router component matches the request and execute
controller
• Response object is returned
22
Request & Response
• Request • Import the Request Object in controller class
• To use it in your controller type-hint it with the Request
class
23
Request & Response
• Request Examples:
24
Request & Response
• Response
• Import the Response Object in controller class
25
Request & Response
JsonResponse
26
Request & Response
• Response Content-Types
– json - application/json
– xml - application/xml
– png image - image/png
– html - text/html
– full list of content types: developer.mozilla.org
27
Request & Response
Set Conten-type Examples
• Set Content-type Examples
28
REST
• What is REST?
– REpresentational State Transfer
• Interface from one program to another
• REST is stateless
• REST is resource based
29
REST & HTTP
• Request methods:
– GET - Get resource
– POST - Create resource
– PUT - Update resource
– PATCH - Partly update resource
– DELETE - Delete resource
– HEAD - Get headers for resource
• Response: json, xml
30
Summary
Controllers – Create controllers, actions,
5-10-20 rule
Routing – Define routes, configuration,
route parameters
Request & Response – Request methods,
Response types
REST – Rest concepts, REST URIs,
Responses
31