这有点奇怪,因为我跟踪了网上关于在中间件中获取路由参数的一切,并且所有这些参数都返回NULL或错误,这就是我已经尝试过的内容并且没有#39工作:
public function handle($request, Closure $next)
{
$token = $request->access_token;
// Do something with $token
}
这个返回NULL。
public function handle($request, Closure $next)
{
$request->route('parameter_name');
// Do something with $token
}
上面的这个重新发布了NULL。
public function handle($request, Closure $next)
{
$request->route()->parameters();
// Do something with $token
}
以上这个回归:
CheckOwner.php第22行中的FatalThrowableError:呼叫成员 函数参数()on null
我想要的是检查当前尝试编辑产品的用户是否是该产品的所有者,这是基于来自' id'网址的参数和' user_id'在数据库中的产品行中。那是我的代码:
中间件:class CheckOwner
{
/**
* Handle an incoming request.
*
* @param \Illuminate\Http\Request $request
* @param \Closure $next
* @return mixed
*/
public function handle($request, Closure $next)
{
//Get product from $id to edit
$product = Product::find($request->parameter('product'));
if ($product->user_id != Auth::user()->id) {
return redirect('home');
}
return $next($request);
}
}
那是我的路线,它是一条资源路线:
//Products controller show, store, edit, update, destroy
Route::resource('products', 'ProductsController');
以及我在内核页面中注册我的中间件的方式:
class Kernel extends HttpKernel
{
/**
* The application's global HTTP middleware stack.
*
* These middleware are run during every request to your application.
*
* @var array
*/
protected $middleware = [
\Illuminate\Foundation\Http\Middleware\CheckForMaintenanceMode::class,
\App\Http\Middleware\CheckOwner::class,
];
/**
* The application's route middleware groups.
*
* @var array
*/
protected $middlewareGroups = [
'web' => [
\App\Http\Middleware\EncryptCookies::class,
\Illuminate\Cookie\Middleware\AddQueuedCookiesToResponse::class,
\Illuminate\Session\Middleware\StartSession::class,
\Illuminate\View\Middleware\ShareErrorsFromSession::class,
\App\Http\Middleware\VerifyCsrfToken::class,
\Illuminate\Routing\Middleware\SubstituteBindings::class,
],
'api' => [
'throttle:60,1',
'bindings',
],
];
/**
* The application's route middleware.
*
* These middleware may be assigned to groups or used individually.
*
* @var array
*/
protected $routeMiddleware = [
'auth' => \Illuminate\Auth\Middleware\Authenticate::class,
'auth.basic' => \Illuminate\Auth\Middleware\AuthenticateWithBasicAuth::class,
'bindings' => \Illuminate\Routing\Middleware\SubstituteBindings::class,
'can' => \Illuminate\Auth\Middleware\Authorize::class,
'guest' => \App\Http\Middleware\RedirectIfAuthenticated::class,
'throttle' => \Illuminate\Routing\Middleware\ThrottleRequests::class,
/**** OTHER MIDDLEWARE ****/
'localize' => \Mcamara\LaravelLocalization\Middleware\LaravelLocalizationRoutes::class,
'localizationRedirect' => \Mcamara\LaravelLocalization\Middleware\LaravelLocalizationRedirectFilter::class,
'localeSessionRedirect' => \Mcamara\LaravelLocalization\Middleware\LocaleSessionRedirect::class
// REDIRECTION MIDDLEWARE
];
}
现在根据上面的代码返回此错误:
Macroable.php第74行中的BadMethodCallException:方法参数 不存在。
更新:我在控制器的构造函数中使用中间件的地方:
public function __construct()
{
//Kick him/her out if he is not logged in
$this->middleware('auth', ['except' => ['show']]);
$this->middleware('checkowner', ['except' => ['create', 'store', 'show', 'update', 'delete']]);
}
答案 0 :(得分:3)
全局中间件在路由解决之前运行。
如果您需要访问路由参数,请将中间件用作路由中间件:
protected $middlewareGroups = [
'web' => [
\App\Http\Middleware\CheckOwner::class,
// the rest of your middleware...
],
];