流明$ request-> input()在中间件中返回null

时间:2019-03-21 13:48:06

标签: php lumen

我正在Lumen内构建RESTful服务器,但是当我向其传递查询参数时,$request->input("something")甚至$request->all()总是返回空数组或null。所以,问题是-我在做什么错了,如何通过$ request-> input()或$ request-> all()获取参数?

routes / web.php

<?php

use Illuminate\Support\Facades\Input;
use Illuminate\Http\Request;

$router->group(['prefix' => 'accounts/{old}'], function () use ($router) {
    $router->get('register', function ($old)    {
        return 'You are OK! More than 18! '.$old.' years';
    });
});

app / Http / Middleware / OldMiddleware.php

<?php

namespace App\Http\Middleware;

use Closure;

class OldMiddleware
{
    /**
     * Check user old and if it was less than 18 return something and else continue.
     *
     * @param  \Illuminate\Http\Request  $request
     * @param  \Closure  $next
     * @return mixed
     */
    public function handle($request, Closure $next)
    {
        if ($request->input('old') < 18) { 
            return "You are under 18!";
        }
        return $next($request);
    }
}

bootstap / app.php

$app->middleware([
    App\Http\Middleware\OldMiddleware::class
]);

请求示例:

curl http://server.loc/accounts/20/register

在OldMiddleware.php中,$ request-> input()返回null,我无法访问中间件中的旧传递值(以验证用户年龄)

1 个答案:

答案 0 :(得分:0)

我找到了解决方案。这样看起来很疯狂!

$request->route('old');

别忘了导入请求(在PHP开始标记后的第二行):

use Illuminate\Http\Request;