Category: Laravel

Sometimes we want to parse the URL variables of a given route, ie getting the word user from /api/users/{user}. If we have: We get the following, which is not what we want unfortunately: Let’s take a look at how Lavavel router does this behind the scene.

In /src/illuminate/routing/route the toSymfonyRoute() function, we see: That looks very scary, but for typically use cases, we can simplify it to: The $route variable is an instance of SymfonyRoute . Once we got that, we can run the compile method, ie $route->compile() to get a compileRoute instance where we can get all the URL variables as an array. This is how the compiledRoute instance looks like: We can call the getVariables() method to get all the URL parameters / variables as an array.
Newsletter

Get the latest Laravel/PHP jobs, events and curated articles straight to your inbox, once a week

Glimpse streamlines Laravel development by seamlessly deploying GitHub pull requests to preview environments with the help of Laravel Forge. Glimpse streamlines Laravel development by seamlessly deploying GitHub pull requests to preview environments with the help of Laravel Forge.
Fathom Analytics | Fast, simple and privacy-focused website analytics. Fathom Analytics | Fast, simple and privacy-focused website analytics.
Shirts painstakingly handcrafted by under-caffeinated developers. Shirts painstakingly handcrafted by under-caffeinated developers.
Community Partners