如果用户未使用中间件进行身份验证,Laravel 5.4 将重定向到特定页面
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/43607097/
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
Laravel 5.4 redirect to specific page if user is not authenticated using middleware
提问by Antonios Tsimourtos
I want to redirect user, if not authenticated, to my index page (which is the login page)
我想将用户(如果未通过身份验证)重定向到我的索引页面(即登录页面)
Can't seem to make it work and i really got confused with the routing.
似乎无法让它工作,我真的对路由感到困惑。
HomeController
家庭控制器
class HomeController extends Controller
{
/**
* Show the application dashboard.
*
* @return \Illuminate\Http\Response
*/
public function index()
{
return redirect()->guest('/');
}
}
Routing
路由
// Index
Route::get('/', [
'as' => 'index',
'uses' => 'UserController@index'
]);
UserController
用户控制器
The routing as you see redirects to a User Controllerat indexfunction, which is the below :
您看到的路由重定向到索引函数处的用户控制器,如下所示:
*has __construct()so it uses the middleware 'auth'.
*有__construct()所以它使用中间件 'auth'。
public function __construct()
{
$this->middleware('auth');
}
public function index(){
// If user is logged
if(Auth::check()) {
// If user has NOT submitted information form redirect there, otherwise to categories
if(!Auth::user()->submitted_information)
return redirect()->route('information');
else
return redirect()->route('categories');
}
else
return view('index', ['body_class' => 'template-home']);
}
Handler.php
处理程序
And the unauthenticated functioninside middleware of auth (Exceptions/Handler.php)
以及auth 中间件中的未经身份验证的函数(Exceptions/Handler.php)
protected function unauthenticated($request, AuthenticationException $exception)
{
if ($request->expectsJson()) {
return response()->json(['error' => 'Unauthenticated.'], 401);
}
return redirect()->route('index');
}
The error i get right now is the below :
我现在得到的错误如下:
InvalidArgumentException in UrlGenerator.php line 304:
Route [index] not defined.
This error happens because of the line of
发生此错误的原因是
return redirect()->route('index');
in the above unauthenticatedfunction.
return redirect()->route('index');
在上述未经身份验证的功能中。
What am i missing here? If you need any more information please feel free to ask.
我在这里错过了什么?如果您需要更多信息,请随时询问。
EDIT: Until now, if i remove from UserControllerthe __construct()
method, and insert in web.php
to all the routes what middleware
to use, it works.
编辑:到目前为止,如果我从UserController 中删除该__construct()
方法,并将其插入web.php
到所有middleware
要使用的路由中,它就可以工作。
For example
例如
Route::get('/categories', [
'as' => 'categories',
'uses' => 'UserController@showCategories'
])->middleware('auth');
But i am trying to find, without specifying there what middleware to use, to use it automatically.
但我试图找到,而不指定使用什么中间件,自动使用它。
回答by ismael ansari
Build your route like below code:
像下面的代码一样构建你的路线:
Route::group(['middleware' => ['auth']], function() {
// uses 'auth' middleware
Route::resource('blog','BlogController');
});
Route::get('/mypage', 'HomeController@mypage');
Route::get('/mypage', 'HomeController@mypage');
Open your middleware class named RedirectIfAuthenticated and then in handle fucntion you write below code:
打开名为 RedirectIfAuthenticated 的中间件类,然后在句柄功能中编写以下代码:
if (!Auth::check()) {
return redirect('/mypage'); // redirect to your specific page which is public for all
}
Hope it will work for you.
希望它对你有用。
回答by Shailesh Ladumor
回答by Ketan Akbari
Try
尝试
Route::get('/','UserController@index',['middleware'=>'auth'])->name('index);