0

I created an URL String:

http://SERVER/v1/responders?latitude=48.25969809999999&longitude=11.43467940000005

a route:

Route::get('/responders', 'Responders\APIResponderController@index');

and a controller:

public function index(Request $request) {

    // Get Latitude and Longitude from request url
    $latitude = $request["latitude"];
    $longitude = $request["longitude"];

    $responders = new Responder();

    if(!isset($latitude) & !isset($longitude)) {
    }

But the result is not what I expected. The parameters in the URL string are not being parsed withing the controller. Do I parse them in a wrong way?

I tried to dump my input with dd($request->all()); but the output is NULL. As the URL is sent correctly, I wonder where the data gets lost. Is my route file incorrect?

Update Could it be my nginx config??

server {
    listen 80 default_server;
    listen [::]:80 default_server ipv6only=on;

    root /var/www/mfserver/public;
    index index.php index.html index.htm;

    charset utf-8;

    server_name SERVER;

    location / {
        try_files $uri $uri/ /index.php?query_string;
    }

    error_page 404 /index.php;
    error_page 500 502 503 504 /50x.html;
    location = /50x.html {
        root /var/www/mfserver/public;
    }

    location ~ \.php$ {
        fastcgi_split_path_info ^(.+\.php)(/.+)$;
        fastcgi_pass unix:/var/run/php5-fpm.sock;
        fastcgi_index index.php;
        fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
        include fastcgi_params;
        fastcgi_intercept_errors off;
        fastcgi_buffer_size 16k;
        fastcgi_buffers 4 16k;
    }
}
5
  • 1
    Possible duplicate of Getting GET "?" variable in laravel Commented May 7, 2016 at 18:50
  • could you do a dd($request->all()) and show the results? Commented May 7, 2016 at 18:53
  • The result is: array:1 [▼ "query_string" => "" ] Commented May 7, 2016 at 18:53
  • This is strange, as I definitely have the URL as mentioned above which should give the parameters Commented May 7, 2016 at 18:54
  • Sorry didn't find anything in your nginx config. And yeah I asked you to run dd(request()->get('latitude')) request as helper function without $. Commented May 8, 2016 at 2:07

4 Answers 4

1

You have a typo in your nginx config. In your location definition, your query_string variable is missing the leading $. Because of this, your original query string is getting re-written with the plain text of query_string. This is why your request data is showing array:1 [▼ "query_string" => "" ] when you dump it.

Update your config to:

location / {
    try_files $uri $uri/ /index.php?$query_string;
}
Sign up to request clarification or add additional context in comments.

Comments

1

Try this:

$inputs = $request->all();

And then :

$latitude = $inputs['latitude']

$longitude = $inputs['longitude']

8 Comments

When I var_dump the inputs I get: array(1) { ["query_string"]=> string(0)}
Ok try just this dd(request()->all() and tell me what you are getting
The result is: array:1 [▼ "query_string" => "" ]
Strange O.o seriously
Yep... thats what I thought: I tried it out in the browser and in postman. Both the same result
|
0

This should work, adapt it to your needs.

public function index(Request $request) {
    if(!$request->has('latitude') || !$request->has('longitude')) {
        // code if one or both fields are missing
    }

    // Get Latitude and Longitude from request url
    $latitude = (float)$request->input('latitude') ;
    $longitude = (float)$request->input('longitude');

    // other code here

}

1 Comment

when I do this: $latitude = (float)$request->input('latitude'); the result is null
0

The most common way would be

$latitude = $request->input("latitude");

3 Comments

this is strange... the output is NULL
Are you sure latitude was sent? You can see all attributes from the current request like this: dd($request->all());
Interesting.. it is empty! But my URL String contains it http://SERVER/v1/responders?latitude=48.25969809999999&longitude=11.43467940000005 Do I need to change my routes file?

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.