ArchenZhang
ArchenZhang

Reputation: 151

How to route all url with same prefix to one action

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 Piccontroller@showPic

I tried to write the route like

Route::get("/pic/*","Piccontroller@showPic");

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

Upvotes: 2

Views: 487

Answers (1)

Jeremy Harris
Jeremy Harris

Reputation: 24549

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}', 'Piccontroller@showPic')->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.

Upvotes: 2

Related Questions