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?
谁能帮我?或是否有人面临同样的问题?我该如何解决?
1 个解决方案
#1
29
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引入了中间件组概念:您可以指定一个或多个中间件属于一个组,并且可以将中间件组应用于一个或多个路由
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 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
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
:
从Laravel 5.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中间件绑定到路由
#1
29
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引入了中间件组概念:您可以指定一个或多个中间件属于一个组,并且可以将中间件组应用于一个或多个路由
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 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
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
:
从Laravel 5.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中间件绑定到路由