在 Laravel 中,中间件在 ajax 请求中被忽略?
In Laravel, Middleware is ignored in ajax request?
这是控制器中的方法。 x-editable ajax.
请求
/**
* Update base info such as status.
*
* @param Request $request
* @return mixed
*/
public function postUpdateInfo(Request $request)
{
$this->middleware('recruit');
dd('passed');
$recruit = Recruit::find($request->get('pk'));
list($key, $value) = array($request->get('name'), $request->get('value'));
if ($recruit->update([$key => $value])) {
return Response::json(['success' => 1]);
}
}
在中间件中,代码如下:
/**
* Handle an incoming request.
*
* @param \Illuminate\Http\Request $request
* @param \Closure $next
* @return mixed
*/
public function handle($request, Closure $next, $permission)
{
die();
if (Entrust::can($permission)) {
return $next($request);
}
$this->belongsToMe($request, $this->instance);
return $next($request);
}
但是在chrome,我在网络中查看响应。
那么,我认为在 ajax 请求中忽略了中间件?太厉害了
非常感谢。
您应该在控制器的构造函数中附加您的中间件:
public function __construct()
{
//assign the middleware to all the methods of the controller
$this->middleware('recruit');
}
或者,如果您只想将它附加到控制器的某个方法,请执行以下操作:
//assign the middleware only to the postUpdateInfo method
$this->middleware('recruit', [ 'only '=> 'postUpdateInfo' ] ]);
您可以为 404 错误设置回退路由:
Route::fallback('{ CONTROLLER PATH }@error404');
然后,您可以将此方法放入您的控制器中:
public function error404() {
return view('views.errors.404');
}
这是控制器中的方法。 x-editable ajax.
请求/**
* Update base info such as status.
*
* @param Request $request
* @return mixed
*/
public function postUpdateInfo(Request $request)
{
$this->middleware('recruit');
dd('passed');
$recruit = Recruit::find($request->get('pk'));
list($key, $value) = array($request->get('name'), $request->get('value'));
if ($recruit->update([$key => $value])) {
return Response::json(['success' => 1]);
}
}
在中间件中,代码如下:
/**
* Handle an incoming request.
*
* @param \Illuminate\Http\Request $request
* @param \Closure $next
* @return mixed
*/
public function handle($request, Closure $next, $permission)
{
die();
if (Entrust::can($permission)) {
return $next($request);
}
$this->belongsToMe($request, $this->instance);
return $next($request);
}
但是在chrome,我在网络中查看响应。
那么,我认为在 ajax 请求中忽略了中间件?太厉害了
非常感谢。
您应该在控制器的构造函数中附加您的中间件:
public function __construct()
{
//assign the middleware to all the methods of the controller
$this->middleware('recruit');
}
或者,如果您只想将它附加到控制器的某个方法,请执行以下操作:
//assign the middleware only to the postUpdateInfo method
$this->middleware('recruit', [ 'only '=> 'postUpdateInfo' ] ]);
您可以为 404 错误设置回退路由:
Route::fallback('{ CONTROLLER PATH }@error404');
然后,您可以将此方法放入您的控制器中:
public function error404() {
return view('views.errors.404');
}