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?

6
  • Well, what's the url you're using to test the 2nd route? Commented Oct 16, 2014 at 17:41
  • @DamienPirsy http://domain.com/dev/test?id=123. I added this to the question. Commented Oct 16, 2014 at 18:02
  • What does dd(Input::all()); show you? Commented Oct 16, 2014 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. Commented Oct 16, 2014 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?? Commented Oct 16, 2014 at 21:09

3 Answers 3

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

Sign up to request clarification or add additional context in comments.

2 Comments

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.
@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
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

Comments

0

Try this:

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

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

Hope this is helpful.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.