简体   繁体   中英

Laravel 5, have full url as a route parameter

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.

Pass the original URL like this...

<img src="http://host.com/generate-thumb/300x300?image=http%3A%2F%2Fhost.com%2Fimages%2Fmedia1.png"/>

Note that I've used urlencode on the path to make it URL safe.

Then in your controller, you can simply use $original= Input::get('image');

You're going to need to do something like this:

<?php $url = sprintf('http://example.com/%s?image=%s', $size, url_encode($original_url); ?> 
<img src='{{ $url }}'>

url_encode() will make that URL safe to pass as a GET parameter.

Edit for comment:

This doesn't work because of the way Laravel is parsing the URL and it's parameters. It sees 300x300/http:// and finds an empty parameter. If you can remove the http:// and still have your functionality then it will work just fine. urlencode is not going to fix this problem.

If you want to generate :

<img src="http://host.com/generate-thumb/300x300/http://host.com/images/media1.png"/>

Use the route() helper, then pass an array for the parameters.

Route.php

 Route::get('/generate-thumb/{size}/{original_url}', array(
       'as' => 'thumb_hook',
       'uses' => 'ThumbController@thumbAction'
    ));

on your blade file :

<img src="{{ route('thumb_hook',[ 
    'size' => '300x300' ,
    'original_url' => 'http://host.com/images/media1.png'
 ]) }}"/>

https://laravel.com/docs/5.1/helpers#method-route

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM