Ad

How To Route All Url With Same Prefix To One Action

- 1 answer

I want to route all the URL with the same prefix to the same action like:

http://host/pic/with/bird

http://host/pic/with/bird/with/fish

http://host/pic/with/pig/theme/sad/resolution/high

all to the [email protected]

I tried to write the route like

Route::get("/pic/*","[email protected]");

to route all the url start with /pic/ to the same function. but the route with * not work.

Ad

Answer

If you take a look at Illuminate\Routing\Route, it has a method called where() for this. The comment on that method says "Set a regular expression requirement on the route."

That would look like this:

Route::get('/pic/{section}', '[email protected]')->where(['section' => '.*']);

Essentially meaning it will take anything after the /pic/ part and pass it as a variable to the showPic() method. Now, parsing that on the method should be as easy as a simple explode() on the forward slash assuming you have some sort of pattern or are just trying to get each of the values.

Ad
source: stackoverflow.com
Ad