Laravel 控制器中一种特定方法的中间件
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/45479864/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
middleware for one specific method in controller in Laravel
提问by Angielski Uzet
I have middleware Auth
that is in App\Http\Middleware\
我有中间件Auth
是在App\Http\Middleware\
In my kernel i added him like:
在我的内核中,我添加了他:
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,
'Groups' => \App\Http\Middleware\Groups::class,
'Auth' => \App\Http\Middleware\Auth::class,
];
This middleware contains
这个中间件包含
<?php
namespace App\Http\Middleware;
use Closure;
class Auth
{
/**
* Handle an incoming request.
*
* @param \Illuminate\Http\Request $request
* @param \Closure $next
* @return mixed
*/
public function handle($request, Closure $next)
{
if( !auth()->check() )
{
return abort(404);
}
return $next($request);
}
}
And in my controller method I use
在我的控制器方法中我使用
$this->middleware('Auth');
$this->middleware('Auth');
But this doesn't work at all.
但这根本行不通。
And when I take my 'Auth' => \App\Http\Middleware\Auth::class,
and place it into protected $middlewareGroups
like \App\Http\Middleware\Auth::class,
it works. But for every single page. So when I am not logged it all time abort404.
当我把'Auth' => \App\Http\Middleware\Auth::class,
它放进去时,它protected $middlewareGroups
就像\App\Http\Middleware\Auth::class,
它的工作一样。但是对于每一页。所以当我没有记录它时,它总是 abort404。
What is wrong here? I can't see, it looks fine to me.
这里有什么问题?我看不出来,我觉得还好。
But this middleware doesn't work for this method in which I have $this->middleware('Auth');
但是这个中间件不适用于我拥有的这种方法 $this->middleware('Auth');
I am not logged in but page appears as normal.
我没有登录,但页面显示正常。
And there should be abort404 fired cuz I am not logged
并且应该有 abort404 被解雇,因为我没有登录
What made I wrong?
是什么让我错了?
回答by Maraboc
The problem that you have when you added the middleware to $middlewareGroups
is explained in the doc
文档中$middlewareGroups
解释了添加中间件时遇到的问题
Out of the box, the web middleware group is automatically applied to your routes/web.php file by the RouteServiceProvider.
开箱即用,Web 中间件组由 RouteServiceProvider 自动应用到您的 routes/web.php 文件。
And if you want the middleware for just one action in the cotroller you can bind the middleware to the route :
如果您只想要控制器中的一个操作的中间件,您可以将中间件绑定到路由:
Route::get('yourRout', 'YourController@actionX')->middleware('auth');
Or you can add it in constructor of your controller :
或者您可以将其添加到控制器的构造函数中:
public function __construct()
{
$this->middleware('auth', ['only' => ['edit']]);
}
Or you can try ( Since Laravel 5.3) :
或者你可以尝试(从 Laravel 5.3 开始):
public function __construct()
{
$this->middleware('auth')->only(['edit']);
}