在我的web.php中,我有一条路线
Route::get('summary_average_fee', 'Summary@AverageFee')->middleware('CheckParams@dateLimits');
我正尝试在CheckParams类中引用dateLimits函数
我的CheckParams类,另存为Middleware文件夹中的CheckParams.php
<?php
namespace App\Http\Middleware;
use Closure;
class CheckParams
{
/**
* Handle an incoming request.
*
* @param \Illuminate\Http\Request $request
* @param \Closure $next
* @return mixed
*/
public function dateLimits($request, Closure $next)
{
isEmpty($request->input('startDate'), 'NO_START_DATE');
isEmpty($request->input('endDate'), 'NO_END_DATE');
return $next($request);
}
private function isEmpty($value, $error, $status)
{
if(empty($value))
{
return response()->json($error, 422);
}
}
}
在kernal.php文件中,将其添加到routeMiddleware数组
'CheckParams' => \App\Http\Middleware\CheckParams::class
当它运行时,我收到错误,提示Class CheckParams @ dateLimits不存在
答案 0 :(得分:2)
在我看来,您的中间件应该重写并更新用法:
use App\Http\Middleware\CheckParams;
Route::get('summary_average_fee', 'Summary@AverageFee')->middleware(CheckParams::class);
中间件:
namespace App\Http\Middleware;
use Closure;
class CheckParams
{
/**
* Handle an incoming request.
*
* @param \Illuminate\Http\Request $request
* @param \Closure $next
* @return mixed
*/
public function handle($request, Closure $next)
{
$this->isEmpty($request->input('startDate'), 'NO_START_DATE');
$this->isEmpty($request->input('endDate'), 'NO_END_DATE');
return $next($request);
}
private function isEmpty($value, $error, $status)
{
if(empty($value))
{
return response()->json($error, 422);
}
}
}