Ebooks

Symfony Request tutorial

Symfony request tutorial shows how to work with request objects in Symfony. We show several ways how to create request objects in Symfony.

Symfony

Symfony is a set of reusable PHP components and a PHP framework for web projects. Symfony was published as free software in 2005. The original author of Symfony is Fabien Potencier. Symfony was heavily inspired by the Spring Framework.

HttpFoundation component

Symfony HttpFoundation component defines an object-oriented layer for the HTTP specification. The component represents the request/response process in an object-oriented manner. On the lowest-level, we have PHP global variables such as $_GET, $_POST, or $_FILES. These are represented by a Request object. And the response is represented by a Response object.

Symfony request example

In the following example, we create three different requests using links.

$ composer create-project symfony/skeleton symrequest

With composer, we create a new Symfony skeleton project.

$ cd symrequest

We go to the project directory.

$ composer require annotations
$ composer require maker
$ composer require twig

We install three modules: annotations, maker, and twig.

$ composer require server --dev

We install the development web server.

$ bin/console make:controller HomeController

A HomeController is created.

src/Controller/HomeController.php
namespace App\Controller;

use Symfony\Component\Routing\Annotation\Route;
use Symfony\Bundle\FrameworkBundle\Controller\Controller;

class HomeController extends Controller
{
    /**
     * @Route("/", name="home")
     */
    public function index()
    {
        return $this->render('home/index.html.twig');
    }
}

The HomeController returns a home page that contains the anchor tags.

templates/home/index.html.twig
{% extends 'base.html.twig' %}

{% block title %}Home page{% endblock %}

{% block body %}

<ul>
<li><a href="/myapp?colour=yellow&day=Saturday">First request</a></li>
<li><a href="/myapp2?colour=green&day=Sunday">Second request</a></li>

<li><a href="/myapp3?colour=red&day=Monday">Third request</a></li>
</ul>

{% endblock %}

The HomeController returns a home page that contains three links. Each of the links has two query parameters. They point to different controller methods.

{% extends 'base.html.twig' %}

The template inherits from the base.html.twig file, which has base markup that will be shared.

templates/base.html.twig
<!DOCTYPE html>
<html>
    <head>
        <meta charset="UTF-8">
        <title>{% block title %}Welcome!{% endblock %}</title>
        {% block stylesheets %}{% endblock %}
    </head>
    <body>
        {% block body %}{% endblock %}
    </body>
</html>

The base.html.twig template contains code that is shared by other template files. It defines blocks that will be replaced in children templates.

$ bin/console make:controller MyappController

A MyappController is created.

src/Controller/MyappController.php
<?php

namespace App\Controller;

use Symfony\Component\Routing\Annotation\Route;
use Symfony\Bundle\FrameworkBundle\Controller\Controller;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Response;


class MyappController extends Controller
{
    /**
     * @Route("/myapp", name="myapp")
     */
    public function process()
    {
        $request = Request::createFromGlobals();
        $col = $request->get("colour");
        $day = $request->get("day");

        $content = "Colour: $col, day: $day";

        return new Response($content);
    }

    /**
     * @Route("/myapp2", name="myapp2")
     */
    public function process2()
    {
        $request = new Request(
            $_GET,
            $_POST,
            array(),
            $_COOKIE,
            $_FILES,
            $_SERVER
        );

        $col = $request->get("colour");
        $day = $request->get("day");

        $content = "Colour: $col, day: $day";

        return new Response($content);
    }    

    /**
     * @Route("/myapp3", name="myapp3")
     */
    public function process3(Request $request)
    {
        $data = $request->query->all();

        $col = $data["colour"];
        $day = $data["day"];

        $content = "Colour: $col, day: $day";

        return new Response($content);        
    }    
}

The MyappController processes the three GET requests created by the links.

$request = Request::createFromGlobals();
$col = $request->get("colour");
$day = $request->get("day");

The request object is created with Request::createFromGlobals(). The GET parameters are retrieved with the get() method.

$request = new Request(
    $_GET,
    $_POST,
    array(),
    $_COOKIE,
    $_FILES,
    $_SERVER
);

In the second case, the request is created with a new keyword. It is passed the PHP global variables.

public function process3(Request $request)
{
    $data = $request->query->all();
...    

In the third case, the request object is injected using Symfony's dependency injection component. We get all parameters from the request with the all() method.

$col = $data["colour"];
$day = $data["day"];

From the array, we get the values.

$content = "Colour: $col, day: $day";

return new Response($content);  

We build the content and return a Response object.

In this tutorial we have worked with requests in Symfony.

You might also be interested in the following related tutorials: Introduction to Symfony, Symfony form tutorial, PHP tutorial.