简体   繁体   English

laravel 5.3 auth始终重定向到登录页面

[英]laravel 5.3 auth always redirect to login page

I want to create authentication on laravel, but i have a problem. 我想在laravel上创建身份验证,但是我遇到了问题。 when i want to access another page, the page has redirected to login page. 当我想访问另一个页面时,该页面已重定向到登录页面。

Here is my routes 这是我的路线

Route::auth();
Route::get('/news/getid/{id_category}', 'NewsController@getid');
Route::group(['middleware' => ['web', 'auth']], function () {
    Route::resource('news', 'NewsController', ['except' => ['getid']]);
    Route::resource('category', 'CategoryController');
});

and here is my LoginController 这是我的LoginController

protected $redirectTo = '/news';

public function username()
{
    return 'username';
}

/**
 * Create a new controller instance.
 *
 * @return void
 */
public function __construct()
{
    $this->middleware('guest', ['except' => 'logout']);
}

You have to comment $this->middleware('auth'); 您必须评论$ this-> middleware('auth'); line from constructor of your "NewsController". “ NewsController”的构造函数中的一行。 It should be 它应该是

public function __construct()
{
// $this->middleware('auth');
}

Note: By commenting this line, Never check authentication for all view page that return from this controller. 注意:通过注释此行,从不检查从此控制器返回的所有视图页面的身份验证。

Route::auth(); should be in web middleware group as auth routes use session to remember logged in user. 应该位于web中间件组中,因为身份验证路由使用会话来记住已登录的用户。

You need to remove this line in every page that you don't want to be protected by the authentication: 您需要在每个不想由身份验证保护的页面中删除此行:

$this->middleware('auth');

so basically remove it from the "another page"s controller that you mentioned above. 因此基本上将其从上面提到的“另一页”控制器中删除。

In your Route file(Maybe web.php). 在您的Route文件中(也许是web.php)。 Remove the auth middleware from the group Like this: 从组中删除身份验证中间件,如下所示:

Route::group(['middleware' => ['web' , 'auth']], function () {
Route::resource('news', 'NewsController', ['except' => ['getid']]);
Route::resource('category', 'CategoryController');
});

to become like this: 变成这样:

Route::group('middleware' => 'web', function () {
Route::resource('news', 'NewsController', ['except' => ['getid']]);
Route::resource('category', 'CategoryController');
});

But be sure you really don't need to protect that route. 但是请确保您确实不需要保护该路线。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM