Skip to content
  • Homepage
  • HTML
  • CSS
  • Symfony
  • PHP
  • How to
  • Contact
  • Donate

Teach Developer

Articles, Guides & Tips

How to Symfony Request

Home  »  How to • Symfony   »   How to Symfony Request
Posted on September 10, 2022September 17, 2022
638

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 an 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 a 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 the template contains code that is shared by other template files. It defines blocks that will be replaced in children’s 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.

How to, Symfony Tags:PHP

Post navigation

Previous Post: How to Change SSH Port in Linux
Next Post: How to Symfony parameters and environment variables use

Related Posts

  • Here are 10 platforms where you can find your next remote job as a developer.
  • How to check PHP version using the command line
  • How to fix getFullyear() is not a function with JavaScript?
  • How to check the PHP version
  • How to Recover Deleted WhatsApp Messages
  • How to PHP Get Max Value From Array

Categories

  • Codeigniter (3)
  • CSS (11)
  • eCommerce (1)
  • Framework (1)
  • Git (3)
  • How to (43)
  • HTML (5)
  • JavaScript (15)
  • Jquery (7)
  • Laravel (1)
  • Linux (4)
  • Magento-2 (1)
  • Node js (4)
  • Others (2)
  • PHP (11)
  • React (13)
  • Server (1)
  • SSH (3)
  • Symfony (6)
  • Tips (16)
  • Top Tutorials (10)
  • Ubuntu (3)
  • Vue (1)
  • Wordpress (7)

Latest Posts

  • What is SSH in Linux?
  • How to Delete Files in Ubuntu Command Line
  • How to Deploy a React application on a cPanel
  • How to use events listeners and Event Subscriber in Symfony
  • How to Convert PHP CSV to JSON

WEEKLY TAGS

AJAX (1) Codeigniter (1) Javascript (11) JQuery (1) PHP (16) Programming (1) React (3) Symfony (1)

Random Post

How to increase browser zoom level on page load with CSS?
Common Mistakes with Conditionals
How to use events listeners and Event Subscriber in Symfony
PHP 8.1: read-only properties
How to use setTimeout and setInterval methods in JavaScript

Quick Navigation

  • About
  • Contact
  • Privacy Policy

© Teach Developer 2021. All rights reserved