Laravel 5.2验证不工作 [英] Laravel 5.2 Auth not Working

查看:190
本文介绍了Laravel 5.2验证不工作的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

至于你们知道Laravel 5.2发布前几天。我想这个新版本。我使用的命令行接口下面的命令做了一个新的项目:

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 new testapp

根据认证快速入门的文档,我也跟着下面的命令来脚手架路线和看法认证

As per documentation of Authentication Quickstart, I followed the following command to scaffold routes and views of authentication:

php artisan make:auth

这工作得很好。报名工作正常。但我在登录面临的问题。登录后我测试按照route.php文件:

It worked fine. Registration is working fine. But I am facing problem in Login. After login I tested following in route.php file:

   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.

下面是完整的 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?

推荐答案

Laravel 5.2引入了 中间件集团 理念:您可以指定一个或多个中间件属于一个组,你可以申请一个中间件组到一个或多个路线

Laravel 5.2 introduces the middleware groups concept: 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定义了一个名为组网​​站,用于组中间件处理会话和其他HTTP工具:

By default Laravel 5.2 defines a group named web, used to group the middleware handling session and other http utilities:

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 group for 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() );
    });       
});

更新LARAVEL版本> = 5.2.27

由于Laravel的 5.2.27 版本,在 routes.php文件中定义的所有路由都使用默认网​​站中间件组。即在达到应用程序/供应商/ RouteServiceProvider.php

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 :

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.

总之,如果你想使用默认的身份验证的路由,你仍然需要绑定 AUTH 中间件路线

Anyhow, if you want to use the default authentication for a route, you still need bind the auth middleware to the route

这篇关于Laravel 5.2验证不工作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆