- 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 - Controllers
Controller is responsible for handpng each request that comes into Symfony apppcation. Controller reads an information from the request. Then, creates and returns a response object to the cpent.
According to Symfony, DefaultController class is located at “src/AppBundle/Controller”. It is defined as follows.
DefaultController.php
<?php namespace AppBundleController; use SymfonyBundleFrameworkBundleControllerController; use SymfonyComponentHttpFoundationResponse; class DefaultController extends Controller { }
Here, the HttpFoundation component defines an object-oriented layer for the HTTP specification, and the FrameworkBundle contains most of the “base” framework functionapty.
Request Object
The Request class is an object-oriented representation of the HTTP request message.
Creating a Request Object
Request can be created using createFromGlobals() method.
use SymfonyComponentHttpFoundationRequest; $request = Request::createFromGlobals();
You can simulate a request using Globals. Instead of creating a request based on the PHP globals, you can also simulate a request.
$request = Request::create( /student , GET , array( name => student1 ) );
Here, the create() method creates a request based on a URI, a method, and some parameters.
Overriding a Request Object
You can override the PHP global variables using the overrideGlobals() method. It is defined as follows.
$request->overrideGlobals();
Accessing a Request Object
Request of a web page can be accessed in a controller (action method) using getRequest() method of the base controller.
$request = $this->getRequest();
Identifying a Request Object
If you want to identify a request in your apppcation, “PathInfo" method will return the unique identity of the request url. It is defined as follows.
$request->getPathInfo();
Response Object
The only requirement for a controller is to return a Response object. A Response object holds all the information from a given request and sends it back to the cpent.
Following is a simple example.
Example
use SymfonyComponentHttpFoundationResponse; $response = new Response(‘Default .$name, 10);
You can define the Response object in JSON as follows.
$response = new Response(json_encode(array( name => $name))); $response->headers->set( Content-Type , apppcation/json );
Response Constructor
The constructor contains three arguments −
The response content
The status code
An array of HTTP headers
Following is the basic syntax.
use SymfonyComponentHttpFoundationResponse; $response = new Response( Content , Response::HTTP_OK, array( content-type => text/html ) );
For example, you can pass the content argument as,
$response->setContent(’Student details’);
Similarly, you can pass other arguments as well.
Sending Response
You can send a response to the cpent using the send() method. It is defined as follows.
$response->send();
To redirect the cpent to another URL, you can use the RedirectResponse class.
It is defined as follows.
use SymfonyComponentHttpFoundationRedirectResponse; $response = new RedirectResponse( http://tutorialspoint.com/ );
FrontController
A single PHP file that handles every request coming into your apppcation. FrontController executes the routing of different URLs to internally different parts of the apppcation.
Following is the basic syntax for FrontController.
use SymfonyComponentHttpFoundationRequest; use SymfonyComponentHttpFoundationResponse; $request = Request::createFromGlobals(); $path = $request->getPathInfo(); // the URI path being requested if (in_array($path, array( , / ))) { $response = new Response(’Student home page. ); } elseif (‘/about’ === $path) { $response = new Response(’Student details page’); } else { $response = new Response( Page not found. , Response::HTTP_NOT_FOUND); } $response->send();
Here, the in_array() function searches an array for a specific value.
Advertisements