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

Fathom Analytics | Fast, simple and privacy-focused website analytics. Fathom Analytics | Fast, simple and privacy-focused website analytics.
Achieve superior email deliverability with ToastMail! Our AI-driven tool warms up inboxes, monitors reputation, and ensures emails reach their intended destination. Sign up today for a spam-free future. Achieve superior email deliverability with ToastMail! Our AI-driven tool warms up inboxes, monitors reputation, and ensures emails reach their intended destination. Sign up today for a spam-free future.
Community Partners