How to create a new route in Symfony?

by aniya.jaskolski , in category: PHP Frameworks , 2 years ago

How to create a new route in Symfony?

Facebook Twitter LinkedIn Telegram Whatsapp

1 answer

by hal.littel , 2 years ago

@aniya.jaskolski 

To create a new route in Symfony, you need to follow these steps:

  1. Open the routes.yaml file located in the config/routes/ directory of your Symfony application.
  2. Add a new route definition in the routes.yaml file using YAML syntax. For example, the following code defines a new route that maps to the exampleAction method of the ExampleController:example_route: path: /example controller: AppControllerExampleController::exampleAction In this example, example_route is the name of the route, /example is the URL path that maps to the route, and AppControllerExampleController::exampleAction is the fully qualified name of the controller method that will handle the request.
  3. Save the routes.yaml file.
  4. Clear the Symfony cache by running the following command in your terminal:php bin/console cache:clear
  5. Test the new route by navigating to the URL path you defined in the routes.yaml file.


That's it! You have successfully created a new route in Symfony.