1

I am having an issue trying to make a GET request to a route and process the parameters passed in via the URL. Here are two routes I created in routes.php:

$router->get('/', function() {
    $test = \Input::get('id');
    dd($test);
});

$router->get('test', function() {
    $test = \Input::get('id');
    dd($test);
});

When I go to the first URL/route ('/') and pass in some data, 123 prints to the screen:

http://domain.com/dev/?id=123

When I go to the second ('test') NULL prints to the screen (I've tried '/test' in the routes.php file as well).

http://domain.com/dev/test?id=123

A few things to note here:

  • This installation of Laravel is in a subfolder.
  • We are using Laravel 5.

Any idea why one would work and the other would not?

Marcin Nabiałek
  • 109,655
  • 42
  • 258
  • 291
Joe Fearnley
  • 63
  • 1
  • 6
  • Well, what's the url you're using to test the 2nd route? – Damien Pirsy Oct 16 '14 at 17:41
  • @DamienPirsy `http://domain.com/dev/test?id=123`. I added this to the question. – Joe Fearnley Oct 16 '14 at 18:02
  • What does `dd(Input::all());` show you? – ceejayoz Oct 16 '14 at 19:24
  • @ceejayoz the first route ('/') displays `array(1) { ["id"]=> string(3) "123" }` and the second ('/test') displays `array(1) { ["test"]=> string(0) ""}`. From what I understand, this should be correct for restful routes. I'm confused on why the first would for and second would ignore the $_GET parameters. – Joe Fearnley Oct 16 '14 at 20:12
  • 1
    Ehm...beware that you wrote `['test'] => string ....` which suggest you're using `\Input::get('test')`, not `\Input::get('id')`... Is the mistake here or in your code?? – Damien Pirsy Oct 16 '14 at 21:09
  • @DamienPirsy No, it is not a mistake. I think because the url is http://domain.com/dev/test it is reading "test" as a url parameter, in a restful way. – Joe Fearnley Oct 17 '14 at 12:58

3 Answers3

2

First thing - Laravel 5 is still under active development so you cannot rely on it too much at this moment.

Second thing is caching and routes. Are you sure you are running code from this routes?

Change this code into:

$router->get('/', function() {

   $test = \Input::get('id');
   var_dump($test);
   echo "/ route";

});

$router->get('test', function() {
   $test = \Input::get('id');
   var_dump($test);
   echo "test route";
});

to make sure messages also appear. This is because if you have annotations with the same verbs and urls they will be used and not the routes you showed here and you may dump something else. I've checked it in fresh Laravel 5 install and for me it works fine. In both cases I have id value displayed

Marcin Nabiałek
  • 109,655
  • 42
  • 258
  • 291
  • Yeah, I'm not sure what is going on. I downloaded a fresh copy of Laravel 5, tested this and it worked fine (`id` value was displayed properly in both cases). Another thing to note is the the version I am running is out of date. I'm going to try updating and test it then. – Joe Fearnley Oct 17 '14 at 13:01
  • @JoeFearnley It's quite possible there was a bug, there a lot of changes all the time in Laravel 5, so something that worked 2 days ago might not work in fresh update and vice versa – Marcin Nabiałek Oct 17 '14 at 13:13
0

You can use

Request::path()

to retrieve the Request URI or you can use

Request::url()

to get the current Request URL.You can check the details from Laravel 5 Documentation in here : http://laravel.com/docs/5.0/requests#other-request-information and when you did these process you can get the GET parameters and use with this function :

function getRequestGET($url){
  $parts = parse_url($url);
  parse_str($parts['query'], $query);
  return $query;
}

For this function thanks to @ruel with this answer : https://stackoverflow.com/a/11480852/2246294

Community
  • 1
  • 1
Emre Doğan
  • 137
  • 2
  • 11
0

Try this:

Route::get('/{urlParameter}', function($urlParameter)
{
    echo $urlParameter; 
});

Go to the URL/route ('/ArtisanBay'):

Hope this is helpful.

ArtisanBay
  • 1,001
  • 11
  • 16