Laravel - 中间件自定义类型请求参数
Laravel - Middleware custom typed Request parameter
我想使用 VacancyListRequest
传递中间件参数并使用其规则在控制器操作之前验证它们。我知道中间件充当管道模式,但是有人知道如何使用除默认 \Illuminate\Http\Request
之外的任何自定义类型吗?
中间件
public function handle(VacancyListRequest $request, Closure $next)
{
$request = $this->analizeQuery($request);
$request = $this->formatValues($request);
$request = $this->prepareParams($request);
return $next($request);
}
控制器
public function index(VacancyListRequest $request, bool $asQuery = false)
错误
App\Http\Middleware\Vacancy\BeforeVacancyIndexRequestMiddleware::handle():
Argument #1 ($request) must be of type
App\Http\Requests\Vacancy\VacancyListRequest, Illuminate\Http\Request
given,
您收到该错误是因为您将 $request
作为类型为 VacancyListRequest
而不是类型 Illuminate\Http\Request
的中间件 handle 方法的参数传递。
你应该把中间件改成这样
use Illuminate\Http\Request;
use Closure;
class VacancyListRequest
{
/**
* Handle an incoming request.
*
* @param \Illuminate\Http\Request $request
* @param \Closure $next
* @return mixed
*/
public function handle(Request $request, Closure $next)
{
$request = $this->analizeQuery($request);
$request = $this->formatValues($request);
$request = $this->prepareParams($request);
return $next($request);
}
}
但是在你的controller中应该是这样的
// At the top of you controller file you add this
use App\Http\Requests\Vacancy\VacancyListRequest;
// And define the controller method like this
public function index(VacancyListRequest $request, bool $asQuery = false)
It's a must for the first parameter of the handle
method to be of type \Illuminate\Http\Request
like it defined in the docs # Defining Middleware
我想使用 VacancyListRequest
传递中间件参数并使用其规则在控制器操作之前验证它们。我知道中间件充当管道模式,但是有人知道如何使用除默认 \Illuminate\Http\Request
之外的任何自定义类型吗?
中间件
public function handle(VacancyListRequest $request, Closure $next)
{
$request = $this->analizeQuery($request);
$request = $this->formatValues($request);
$request = $this->prepareParams($request);
return $next($request);
}
控制器
public function index(VacancyListRequest $request, bool $asQuery = false)
错误
App\Http\Middleware\Vacancy\BeforeVacancyIndexRequestMiddleware::handle(): Argument #1 ($request) must be of type App\Http\Requests\Vacancy\VacancyListRequest, Illuminate\Http\Request given,
您收到该错误是因为您将 $request
作为类型为 VacancyListRequest
而不是类型 Illuminate\Http\Request
的中间件 handle 方法的参数传递。
你应该把中间件改成这样
use Illuminate\Http\Request;
use Closure;
class VacancyListRequest
{
/**
* Handle an incoming request.
*
* @param \Illuminate\Http\Request $request
* @param \Closure $next
* @return mixed
*/
public function handle(Request $request, Closure $next)
{
$request = $this->analizeQuery($request);
$request = $this->formatValues($request);
$request = $this->prepareParams($request);
return $next($request);
}
}
但是在你的controller中应该是这样的
// At the top of you controller file you add this
use App\Http\Requests\Vacancy\VacancyListRequest;
// And define the controller method like this
public function index(VacancyListRequest $request, bool $asQuery = false)
It's a must for the first parameter of the
handle
method to be of type\Illuminate\Http\Request
like it defined in the docs # Defining Middleware