====== Créer un Controleur ====== ====== Prérequis ====== * ''symfony/maker-bundle'' doit être installé (sinon voir section **Problème** ci-dessous) ===== 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 [[:controlles|controller]] lui méme dans ''src/Controller/''. On peut constater qu'il y associe aussi une [[:annotation_route|route en Annotation]] ''* @Route("/test", name="test")'' 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 %}

Hello {{:: controller_name }}! ✅

This friendly message is coming from:
{% 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 : - ''composer require symfony/maker-bundle --dev'' - ''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