I need to pass a full url as a route parameter on my laravel 5 application, the thing is as follows. I'm creating a thumbnail service that having the url of the original photo resizes it and streams it back to the browser. For example, I need the img source to have an src like this:
<img src="http://host.com/generate-thumb/300x300/http://host.com/images/media1.png"/>
So in the controller I can get the original image url like this:
public function thumbAction(Request $request, $size, $original_url){
dd($size, $original_url);
}
And my route definition as follows:
Route::get('/generate-thumb/{size}/{original_url}', array(
'as' => 'thumb_hook',
'uses' => 'ThumbController@thumbAction'
));
I have done it that way but the server responds with a 404 error.
I have also specified an url pattern in the where method of the route definition.
Does anybody knows how to do that?
The issue that you're running into is that the url parameters are, by default, delimited by forward slashes (/). Since one of your parameters will contain forward slashes, you run into a special situation.
Given your current route definition, and given the url http://host.com/generate-thumb/300x300/http://host.com/images/media1.png, what happens is that your size parameter gets 300x300, your original_url parameter gets http:, and then there are a bunch more parameters. Since you don't have a route defined like this, you get a 404.
In order to allow a parameter to accept forward slashes, you will need to define a regex constraint for that parameter. You need to define your route like this:
Route::get('/generate-thumb/{size}/{original_url}', array(
'as' => 'thumb_hook',
'uses' => 'ThumbController@thumbAction'
))
->where('original_url', '.*');
This .* constraint will allow this parameter to accept any character, including forward slashes. Note that when you do this, this parameter will need to be the last defined parameter, since it eats all the forward slashes and you won't be able to delimit any other route parameters.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With