• 3 min read
Symfony Route Parameters: Everything You Need to Know
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.
Verb | Path | Action | Route Name | Description |
---|---|---|---|---|
GET | /movies | index | movies.index | Get all movies |
GET | /movies/create | create | movies.create | Get new created movies |
POST | /movies | store | movies.store | Create a new movie |
GET | /movies/{movie} | show | movies.show | Get data of specific movie |
GET | /movies/{movie}/edit | edit | movies.edit | Edit specific movie |
PUT/PATCH | /movies/{movie} | update | movies.update | Update a specific movies |
DELETE | /movies{movie} | destroy | movies.destroy | Delete 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.