Laravel: How to parse URL parameter / variable name

Sam Ngu
2 min readAug 25, 2020
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:

parse_url('https://test.com/api/users/{user}?page=4&s…

--

--