php Laravel 5.2 身份验证不起作用
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/34548061/
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.2 Auth not Working
提问by Hassan Saqib
As you guys know Laravel 5.2 was released a few days ago. I am trying this new version. I made a new project using the following command on CLI:
众所周知,Laravel 5.2 几天前发布了。我正在尝试这个新版本。我在 CLI 上使用以下命令创建了一个新项目:
laravel new testapp
As per documentation of Authentication Quickstart, I followed the following command to scaffold routes and views of authentication:
根据Authentication Quickstart 的文档,我按照以下命令构建身份验证的路由和视图:
php artisan make:auth
It worked fine. Registration is working fine. But I am facing problem in Login. After login I tested following in route.php file:
它工作得很好。注册工作正常。但我在登录时遇到问题。登录后,我在 route.php 文件中测试了以下内容:
Route::get('/', function () {
dd( Auth::user());
return view('welcome');
});
Auth::user()
is returning null
and also Auth::check()
and Auth::guest()
are not working appropriately. I have tried same thing again and again two three times by making new projects but couldn't get the correct results.
Auth::user()
是返回null
,也Auth::check()
和Auth::guest()
没有适当工作。我通过制作新项目一次又一次地尝试了两次相同的事情,但无法获得正确的结果。
Below is the complete route.php
下面是完整的 route.php
<?php
/*
|--------------------------------------------------------------------------
| Routes File
|--------------------------------------------------------------------------
|
| Here is where you will register all of the routes in an application.
| It's a breeze. Simply tell Laravel the URIs it should respond to
| and give it the controller to call when that URI is requested.
|
*/
Route::get('/', function () {
dd( Auth::());
return view('welcome');
});
/*
|--------------------------------------------------------------------------
| Application Routes
|--------------------------------------------------------------------------
|
| This route group applies the "web" middleware group to every route
| it contains. The "web" middleware group is defined in your HTTP
| kernel and includes session state, CSRF protection, and more.
|
*/
Route::group(['middleware' => ['web']], function () {
//
});
Route::group(['middleware' => 'web'], function () {
Route::auth();
Route::get('/home', 'HomeController@index');
});
Can anyone help me? or Is anyone facing the same problem? How can I fix it?
谁能帮我?或者有人面临同样的问题吗?我该如何解决?
回答by Moppo
Laravel 5.2 introduces the middleware groupsconcept: you can specify that one or more middleware belongs to a group, and you can apply a middleware group to one or more routes
Laravel 5.2 引入了中间件组的概念:可以指定一个或多个中间件属于一个组,可以将一个中间件组应用到一个或多个路由
By default Laravel 5.2 defines a group named web
, used to group the middleware handling session and other http utilities:
默认情况下,Laravel 5.2 定义了一个名为 的组web
,用于对中间件处理会话和其他 http 实用程序进行分组:
protected $middlewareGroups = [
'web' => [
\App\Http\Middleware\EncryptCookies::class,
\Illuminate\Cookie\Middleware\AddQueuedCookiesToResponse::class,
\Illuminate\Session\Middleware\StartSession::class,
\Illuminate\View\Middleware\ShareErrorsFromSession::class,
\App\Http\Middleware\VerifyCsrfToken::class,
],
So, if you want session handling, you should use this middleware groupfor all the routes in which you want to use authentication:
所以,如果你想要会话处理,你应该对所有你想要使用身份验证的路由使用这个中间件组:
Route::group( [ 'middleware' => ['web'] ], function ()
{
//this route will use the middleware of the 'web' group, so session and auth will work here
Route::get('/', function () {
dd( Auth::user() );
});
});
UPDATE FOR LARAVEL VERSION >= 5.2.27
Laravel 版本更新 >= 5.2.27
As of Laravel 5.2.27
version, all the routes defined in routes.php
are using by default the web
middleware group. That is achieved in app/Providers/RouteServiceProvider.php
:
在 Laravel5.2.27
版本中,定义的所有路由routes.php
默认使用web
中间件组。这是在app/Providers/RouteServiceProvider.php
以下方面实现的:
protected function mapWebRoutes(Router $router)
{
$router->group([
'namespace' => $this->namespace, 'middleware' => 'web'
], function ($router) {
require app_path('Http/routes.php');
});
}
So you don't need anymore to add manually the web
middleware group to your routes.
因此,您不再需要将web
中间件组手动添加到您的路由中。
Anyhow, if you want to use the default authentication for a route, you still need bind the auth
middleware to the route
无论如何,如果您想对路由使用默认身份验证,您仍然需要将auth
中间件绑定到路由