在Laravel中从URL中检索get参数

时间:2016-05-07 18:33:30

标签: php laravel

我创建了一个URL字符串:

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

路线:

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

和控制器:

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)) {
    }

但结果并非我的预期。 URL字符串中的参数未使用控制器进行解析。我是以错误的方式解析它们吗?

我尝试使用dd($request->all());转储输入,但输出为NULL。由于URL发送正确,我想知道数据丢失的位置。我的路线文件不正确吗?

更新 可能是我的nginx配置??

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;
    }
}

4 个答案:

答案 0 :(得分:1)

试试这个:

$inputs = $request->all();

然后:

$latitude = $inputs['latitude']

$longitude = $inputs['longitude']

答案 1 :(得分:0)

这应该有效,并根据您的需要进行调整。

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

}

答案 2 :(得分:0)

最常见的方式是

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

答案 3 :(得分:0)

你的nginx配置中有拼写错误。在location定义中,您的query_string变量缺少前导$。因此,您的原始查询字符串将使用query_string的纯文本重写。这就是您转储请求数据时显示array:1 [▼ "query_string" => "" ]的原因。

将您的配置更新为:

location / {
    try_files $uri $uri/ /index.php?$query_string;
}
相关问题