Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
206 views
in Technique[技术] by (71.8m points)

php - Laravel 5.2 Auth not Working

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

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.

Below is the complete 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?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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

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

protected $middlewareGroups = [
'web' => [
    AppHttpMiddlewareEncryptCookies::class,
    IlluminateCookieMiddlewareAddQueuedCookiesToResponse::class,
    IlluminateSessionMiddlewareStartSession::class,
    IlluminateViewMiddlewareShareErrorsFromSession::class,
    AppHttpMiddlewareVerifyCsrfToken::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() );
    });       
});

UPDATE FOR LARAVEL VERSION >= 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 :

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.

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


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...