Symfony - Controllers


Advertisements

Controller is responsible for handling each request that comes into Symfony application. Controller reads an information from the request. Then, creates and returns a response object to the client.

According to Symfony, DefaultController class is located at “src/AppBundle/Controller”. It is defined as follows.

DefaultController.php

<?php 
namespace AppBundle\Controller; 
use Symfony\Bundle\FrameworkBundle\Controller\Controller; 
use Symfony\Component\HttpFoundation\Response;  

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 functionality.

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 Symfony\Component\HttpFoundation\Request; 
$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 application, “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 client.

Following is a simple example.

Example

use Symfony\Component\HttpFoundation\Response; 
$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', 'application/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 Symfony\Component\HttpFoundation\Response;  
$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 client using the send() method. It is defined as follows.

$response->send();

To redirect the client to another URL, you can use the RedirectResponse class.

It is defined as follows.

use Symfony\Component\HttpFoundation\RedirectResponse;  
$response = new RedirectResponse('http://howcodex.com/'); 

FrontController

A single PHP file that handles every request coming into your application. FrontController executes the routing of different URLs to internally different parts of the application.

Following is the basic syntax for FrontController.

use Symfony\Component\HttpFoundation\Request; 
use Symfony\Component\HttpFoundation\Response;  

$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