noob990
noob990

Reputation: 21

Laravel passing URL into a controller

I need to pass a URL into a controller like so:

Route::get('vote/scraper?s={url}', 'VoteController@getUrlData');

But when I pass a url as such:

mysite.com/vote/scraper?s=http://www.example.com/fastrack-wayfarer-sunglasses/p/itmdx7z4hgjgp5st

Laravel thinks the other slashes in the URL as different views and throws NotFoundHttpException.

How do I do this ?

Upvotes: 1

Views: 2057

Answers (4)

Loren
Loren

Reputation: 12711

Transforming your code mysite.com/vote/scraper?s=http://www.example.com/fastrack-wayfarer-sunglasses/p/itmdx7z4hgjgp5st should be mysite.com/vote/scraper?s=http%3A%2F%2Fwww.example.com%2Ffastrack-wayfarer-sunglasses%2Fp%2Fitmdx7z4hgjgp5st

You can get the encoded url with urlencode in php or encodeURIComponent in javascript (http://www.w3schools.com/jsref/jsref_encodeuricomponent.asp)

Then, your route should be Route::get('vote/scraper', 'VoteController@getUrlData');

Lastly, in getUrlData use $url = \Input::get('s');

Or in Laravel 5.1

public function getUrlData(Request $request)
{
    $url = $request->input('s');
}

Upvotes: 1

noob990
noob990

Reputation: 21

This works !

Route::get('add/{encoded_url}', function($encoded_url) { return 'The URL is: '.rawurldecode($encoded_url); })->where('encoded_url', '.*');

Upvotes: 1

anwerj
anwerj

Reputation: 2488

Remove query from route:

Route::get('vote/scraper', 'VoteController@getUrlData');

And try to get url from Input::get() like

$url = Input::get('url');

PS: Encoding URL in query is a good and recommended practice.

Upvotes: 1

Romain Lanz
Romain Lanz

Reputation: 885

You need to encode your URL with urlencode().

https://secure.php.net/manual/en/function.urlencode.php

Upvotes: 0

Related Questions