ZetCode

Symfony Request

last modified July 12, 2020

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. The development of the framework is sponsored by a Frech company Sensio Labs.

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

$ symfony new symreq

With composer, we create a new Symfony skeleton project.

$ cd symreq

We go to the project directory.

$ composer req annot twig

We install modules annotations and twig.

$ composer req maker --dev

We install the maker component.

$ php bin/console make:controller HomeController

A HomeController is created.

src/Controller/HomeController.php
<?php

namespace App\Controller;

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

class HomeController extends AbstractController
{
    /**
     * @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.

$ php bin/console make:controller MyappController

A MyappController is created.

src/Controller/MyappController.php
<?php

namespace App\Controller;

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

class MyappController extends AbstractController
{
   /**
     * @Route("/myapp", name="myapp")
     */
    public function process()
    {
        $request = Request::createFromGlobals();
        $col = $request->query->get("colour");
        $day = $request->query->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->query->get("colour");
        $day = $request->query->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->query->get("colour");
$day = $request->query->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. 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.

$ symfony serve

We start the web server and locate to http://localhost:8000.

In this tutorial we have worked with requests in Symfony.

List all Symfony tutorials.