- Complete Working Example
- Symfony - CMF Edition
- Symfony - REST Edition
- Symfony - Advanced Concepts
- Symfony - Unit Testing
- Symfony - Email Management
- Symfony - Logging
- Symfony - Internationalization
- Cookies & Session Management
- Symfony - Ajax Control
- Symfony - File Uploading
- Symfony - Validation
- Symfony - Forms
- Symfony - Doctrine ORM
- Symfony - View Engine
- Symfony - Routing
- Symfony - Controllers
- Creating a Simple Web Application
- Symfony - Bundles
- Symfony - Expression
- Symfony - Events & EventListener
- Symfony - Service Container
- Symfony - Components
- Symfony - Architecture
- Symfony - Installation
- Symfony - Introduction
- Symfony - Home
Symfony Useful Resources
Selected Reading
- Who is Who
- Computer Glossary
- HR Interview Questions
- Effective Resume Writing
- Questions and Answers
- UPSC IAS Exams Notes
Symfony - Routing
Routing maps request URI to a specific controller s method. In general, any URI has the following three parts −
Hostname segment
Path segment
Query segment
For example, in URI / URL, http://www.tutorialspoint.com/index?q=data, www.tutorialspoint.com is the host name segment, index is the path segment and q=data is the query segment. Generally, routing checks the page segment against a set of constraints. If any constraint matches, then it returns a set of values. One of the main value is the controller.
Annotations
Annotation plays an important role in the configuration of Symfony apppcation. Annotation simppfies the configuration by declaring the configuration in the coding itself. Annotation is nothing but providing meta information about class, methods, and properties. Routing uses annotation extensively. Even though routing can be done without annotation, annotation simppfies routing to a large extent.
Following is a sample annotation.
/** * @Route(“/student/home”) */ pubpc function homeAction() { // ... }
Routing Concepts
Consider the StudentController class created in “student” project.
StudentController.php
// src/AppBundle/Controller/StudentController.php namespace AppBundleController; use SymfonyBundleFrameworkBundleControllerController; use SensioBundleFrameworkExtraBundleConfigurationRoute; class StudentController extends Controller { /** * @Route(“/student/home”) */ pubpc function homeAction() { // ... } /** * @Route(“/student/about”) */ pubpc function aboutAction() { } }
Here, the routing performs two steps. If you go to /student/home, the first route is matched then homeAction() is executed. Otherwise, If you go to /student/about, the second route is matched and then aboutAction() is executed.
Adding Wildcard Formats
Consider, you have a paginated pst of student records with URLs pke /student/2 and /student/3 for page 2 and 3 correspondingly. Then, if you want to change the route s path, you can use wildcard formats.
Example
// src/AppBundle/Controller/BlogController.php namespace AppBundleController; use SymfonyBundleFrameworkBundleControllerController; use SensioBundleFrameworkExtraBundleConfigurationRoute; class StudentController extends Controller { /** * @Route(“/student/{page}", name = “student_about”, requirements = {"page": "d+"}) */ pubpc function aboutAction($page) { // ... } }
Here, the d+ is a regular expression that matches a digit of any length.
Assign Placeholder
You can assign a placeholder value in routing. It is defined as follows.
// src/AppBundle/Controller/BlogController.php namespace AppBundleController; use SymfonyBundleFrameworkBundleControllerController; use SensioBundleFrameworkExtraBundleConfigurationRoute; class StudentController extends Controller { /** * @Route(“/student/{page}", name = “student_about”, requirements = {"page": "d+"}) */ pubpc function aboutAction($page = 1) { // ... } }
Here, if you go to /student, the student_about route will match and $page will default to a value of 1.
Redirecting to a Page
If you want to redirect the user to another page, use the redirectToRoute() and redirect() methods.
pubpc function homeAction() { // redirect to the "homepage" route return $this->redirectToRoute( homepage ); // redirect externally eturn $this->redirect( http://example.com/doc ); }
Generating URLs
To generate a URL, consider a route name, student_name and wildcard name, student-names used in the path for that route. The complete psting for generating a URL is defined as follows.
class StudentController extends Controller { pubpc function aboutAction($name) { // ... // /student/student-names $url = $this->generateUrl( ‘student_name’, array(‘name’ => ’student-names’) ); } }
StudentController
Consider a simple example for routing in StudentController class as follows.
StudentController.php
<?php namespace AppBundleController; use SensioBundleFrameworkExtraBundleConfigurationRoute; use SymfonyComponentHttpFoundationResponse; use SymfonyBundleFrameworkBundleControllerController; class StudentController { /** * @Route("/student/home") */ pubpc function homeAction() { $name = Student details apppcation ; return new Response( <html><body>Project: .$name. </body></html> ); } }
Now, request the url,”http://localhost:8000/student/home” and it produces the following result.
Similarly, you can create another route for aboutAction() as well.
Advertisements