3 min read

Symfony Route Parameters: Everything You Need to Know

This blog post explains how to use route parameters in Symfony to render dynamic content on a single page. The post provides examples of how to define and use route parameters in your code, and includes a table of available HTTP methods for a resource. The author also shares a command that displays all routes available within your Symfony project. Overall, this post is a useful resource for developers looking to improve their understanding of Symfony and its routing capabilities.

Routes with parameters are a common topic in Symfony, as well as in other frameworks or programming languages. These routes allow you to render a single page with dynamic content, meaning that the content of the page will change based on the content stored in the database.

In the previous chapter, we defined a basic route to /movies. However, it would be impractical to define a route for each individual movie, such as /movies/inception, /movies/titanic, and so on.

#[Route('/movies/inception', name: 'movies')]

The answer is no. However, we can use route parameters to solve this issue. A route parameter is a variable inside the URL structure of a route. By making it a variable, we can dynamically change its value based on the names we have in our database.

To define a route parameter, wrap your variable inside curly braces right after the actual endpoint.

#[Route('/movies/{name}, name: 'movies')]

Note that we are passing the name of a specific movie as a variable, which can represent an ID, name, or slug.

If we navigate to 127.0.0.1:8000/movies/inception in the browser, we can see that this works fine.

When working on large web applications, you will likely use numerous endpoints to showcase pages to users. Symfony has a command that displays all routes available within your project.

symfony console debug:router

In addition to the name parameter inside the route, you can add the HTTP verb that you want to use for the route.

#[Route('/movie', name: 'movies', methods:[‘GET’, ‘HEAD’)]

Available HTTP methods on a resource

The table shows the available HTTP methods on a resource in a Symfony project, along with the corresponding path, action, route name, and description. The HTTP methods include GET, POST, PUT/PATCH, and DELETE, and the table provides examples of how they can be used to interact with the /movies resource. For instance, the GET method on /movies can be used to retrieve all movies, while the POST method on /movies can be used to create a new movie. The table is a useful reference for developers who are building web applications with Symfony and need to understand how to structure their routes and interact with resources.

VerbPathActionRoute NameDescription
GET/moviesindexmovies.indexGet all movies
GET/movies/createcreatemovies.createGet new created movies
POST/moviesstoremovies.storeCreate a new movie
GET/movies/{movie}showmovies.showGet data of specific movie
GET/movies/{movie}/editeditmovies.editEdit specific movie
PUT/PATCH/movies/{movie}updatemovies.updateUpdate a specific movies
DELETE/movies{movie}destroymovies.destroyDelete a specific movie

Conclusion 🚀

I hope this article has demonstrated how easy it is to add route parameters to your Symfony project.

If you enjoyed reading this post and would like me to cover a specific topic in an article, please send an email to info@codewithdary.com.