Laravel: How to parse URL parameter / variable name

Photo by Lee Campbell on Unsplash

Sometimes we want to parse the URL variables of a given route, ie getting the word user from /api/users/{user}. However there isn’t any helper method in Laravel that does this for us out of the box. The PHP built-in function parse_url is not smart enough to understand the URL variables.

If we have: