Table des matières

Créer un Controleur

Prérequis

Commande

La commande symfony console make:controller %NomDuController% créé la base d'un nouveau controller dans la structure du projet Symfony.

thierry@obi103:/var/www/html/xxx/yyy$ php bin/console make:controller
 
 Choose a name for your controller class (e.g. FierceElephantController):
 > TestController
 
 created: src/Controller/TestController.php
 created: templates/test/index.html.twig
 
 
  Success!
 
 
 Next: Open your new controller class and add some pages!
Si la commande symfony console make:controller %NomDuController% ne fonctionne pas, voir le paragraphe Problèmes, ci dessous

Résultat

Comme indiqué en réponse de la commande, il crée donc deux fichiers :

Le Fichier Controller

Le controller lui méme dans src/Controller/.

On peut constater qu'il y associe aussi une route en Annotation * @Route(“/test”, name=“test”)

<?php
 
namespace App\Controller;
 
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
use Symfony\Component\Routing\Annotation\Route;
 
class TestController extends AbstractController
{
    /**
     * @Route("/test", name="test")
     */
    public function index()
    {
        return $this->render('test/index.html.twig', [
            'controller_name' => 'TestController',
        ]);
    }
}

Le Fichier Template

Le template twig dans templates/test/

{% extends 'base.html.twig' %}
 
{% block title %}Hello {{:: controller_name }}!{% endblock %}
 
{% block body %}
<style>
    .example-wrapper { margin: 1em auto; max-width: 800px; width: 95%; font: 18px/1.5 sans-serif; }
    .example-wrapper code { background: #F5F5F5; padding: 2px 6px; }
</style>
 
<div class="example-wrapper">
    <h1>Hello {{:: controller_name }}! ✅</h1>
 
    This friendly message is coming from:
    <ul>
        <li>Your controller at <code><a href="{{:: 'src/Controller/TestController.php'|file_link(0) }}">src/Controller/TestController.php</a></code></li>
        <li>Your template at <code><a href="{{:: 'templates/test/index.html.twig'|file_link(0) }}">templates/test/index.html.twig</a></code></li>
    </ul>
</div>
{% endblock %}

Résultat dans un navigateur

hellotestcontroller.jpg

Problèmes

Si la commande symfony console make:controller ne fonctionne pas c'est peut etre parce que le bundle maker-bundle n'est pas installé, dans ce cas il faut l'installer avec les commande :

  1. composer require symfony/maker-bundle –dev
  2. composer require doctrine/annotations
>composer require symfony/maker-bundle --dev
Info from https://repo.packagist.org: 
Cannot use symfony/maker-bundle's latest version v1.45.0 as it requires php >=8.0 which is not satisfied by your platform.
Using version ^1.43 for symfony/maker-bundle
./composer.json has been updated
Running composer update symfony/maker-bundle
Loading composer repositories with package information
Updating dependencies
Lock file operations: 3 installs, 0 updates, 0 removals
  - Locking doctrine/inflector (2.0.5)
  - Locking nikic/php-parser (v4.15.1)
  - Locking symfony/maker-bundle (v1.43.0)
Writing lock file
Installing dependencies from lock file (including require-dev)
Package operations: 3 installs, 0 updates, 0 removals
  - Downloading nikic/php-parser (v4.15.1)
  - Downloading doctrine/inflector (2.0.5)
  - Installing nikic/php-parser (v4.15.1): Extracting archive
  - Installing doctrine/inflector (2.0.5): Extracting archive
  - Installing symfony/maker-bundle (v1.43.0): Extracting archive
Generating optimized autoload files
38 packages you are using are looking for funding.
Use the `composer fund` command to find out more!

Symfony operations: 1 recipe (7f92de602bee2d9edd782e4cc4d1bd2a)
  - Configuring symfony/maker-bundle (>=1.0): From github.com/symfony/recipes:main
Executing script cache:clear [OK]
Executing script assets:install public [OK]

 What's next? 


Some files have been created and/or updated to configure your new packages.
Please review, edit and commit them: these files are yours.

No security vulnerability advisories found
>composer require doctrine/annotations
Using version ^1.13 for doctrine/annotations
./composer.json has been updated
Running composer update doctrine/annotations
Loading composer repositories with package information
Updating dependencies
Lock file operations: 2 installs, 0 updates, 0 removals
  - Locking doctrine/annotations (1.13.3)
  - Locking doctrine/lexer (1.2.3)
Writing lock file
Installing dependencies from lock file (including require-dev)
Package operations: 2 installs, 0 updates, 0 removals
  - Installing doctrine/lexer (1.2.3): Extracting archive
  - Installing doctrine/annotations (1.13.3): Extracting archive
Generating optimized autoload files
39 packages you are using are looking for funding.
Use the `composer fund` command to find out more!

Symfony operations: 1 recipe (0beb45a1df42235ccf408079cf154a9d)
  - Configuring doctrine/annotations (>=1.0): From github.com/symfony/recipes:main
Executing script cache:clear [OK]
Executing script assets:install public [OK]

 What's next? 


Some files have been created and/or updated to configure your new packages.
Please review, edit and commit them: these files are yours.

No security vulnerability advisories found