1

I want to receive a GET request like this:

people.php?name=Joe&age=24

For that, I have defined the following route:

Route::get('people.php?name={username}&age={id}', array(
    'as' => 'people/username/age',
    'uses' => 'ExtraController@xfunction',
));

But this doesn't seem to work.

How can I define this route?

2
  • You can do example.com/{name}/{age} in your route if that is what you want? So you'll have an url like domain.com/Joe/24 Commented Sep 6, 2015 at 20:07
  • We want to make an ajax get call, and it seems ajax GET works this way. Commented Sep 6, 2015 at 20:56

2 Answers 2

2

Laravel does not support query string routing.

Create a regular route, then pull the query string arguments from the Input facade:

Route::get('people.php', array(
    'as' => 'people/username/age',
    'uses' => 'ExtraController@xfunction',
));
public function xfunction()
{
    $username = Input::get('name');
    $age      = Input::get('age');
}
Sign up to request clarification or add additional context in comments.

Comments

0

Exactly what Joseph said above. But I want to add a note that all queries should always be handled in a controller method. I'm sure you know, but best practice is obviously to never include any logic outside of routes.

Rather than queries, you can also use optional variables too with Laravel.

So rather than /test/?name=Jonathan, you can use /test/{name?} and you can then view it as /test/ or /test/Jonathan, but in the controller method, you must use

public function ( $name = null; ) { dd($name); }

3 Comments

I use /test/{names?} laravel approach. But I needed this query way for an ajax .get function.
You could still dynamically change the URL for AJAX get so you have the root url, then just use +query or something and have var query = 'something'; before. But at the end of the day, it doesn't matter.
I got it. I didn't know this was possible.

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.