Laravel身份验证重定向错误 [英] Laravel authentication redirect error
问题描述
我是Laravel的新手,所以请原谅我的无知.我正在阅读初学者教程,并被困在内置的身份验证系统中...
I am new to Laravel so please excuse my ignorance. I am going through the beginner tutorials and have gotten stuck on the built in authentication system...
我创建了一个新应用,并遵循了有关设置身份验证的文档,我搜索了堆栈溢出并解决了一个问题(我必须将身份验证路由放入中间件组中),但是现在无论我做什么,它都会重定向到根"/"路径...甚至当我手动转到auth/logout然后再进行auth/login ...有人可以帮忙吗?
I have created a new app and followed the docs on setting up authentication, I searched through stack overflow and overcame one issue (I had to put the auth routes in the middleware group), however now no matter what I do it redirects to the root "/" path...even when I manually go to auth/logout and then auth/login...can someone please help?
推荐答案
在运行Laravel 5.2标准后
In Laravel 5.2 after running their standard
> php artisan make:auth
假设我们要在转到/admin
路由时确保用户身份验证.
let's assume we want to ensure user authentication when going to /admin
route.
在routes.php
中将有一个像这样的条目:
In the routes.php
there will be an entry like this:
Route::group(['middleware' => ['web', 'auth']], function() {
// Only authenticated users may enter...
Route::get('/admin', [
'as' => 'admin', 'uses' => 'AdminController@index'
]);
});
,并且在AuthController.php
中必须添加其他方法:
and in AuthController.php
an additional method must be added:
class AuthController extends Controller
{
...
public function authenticated()
{
return redirect()->intended();
}
}
因此,每当未经身份验证的用户尝试访问/admin
URL时,它将被重定向到/login
页面,如果身份验证成功,他将能够访问/admin
页面.
As a result every time when unauthenticated user tries to access /admin
URL it will be redirected to some /login
page and if authentication succeeds he will be able to access /admin
page.
上面的代码中有几点要注意:
A few points to notice in the code above:
- 同时需要
web
和auth
中间件组(没有web
的auth
将不具有会话支持,因此url.intended
不会保存在会话中,并且整个重定向机制无法正常工作) - AuthController中的方法名称为
authenticated
,而在Laravel文档中未提及authenticate
(一旦验证通过,即称为该名称)
- both
web
andauth
middleware groups are required (auth
withoutweb
won't have session support and as a resulturl.intended
is not saved in the session and the whole redirect mechanism does not work) - the method name in AuthController is
authenticated
and notauthenticate
mentioned in Laravel documentation (it's called once authentication is verified)
这篇关于Laravel身份验证重定向错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!