Has*_*qib 18 php authentication laravel laravel-5 laravel-5.2
你们知道Laravel 5.2几天前发布了.我正在尝试这个新版本.我在CLI上使用以下命令创建了一个新项目:
laravel new testapp
Run Code Online (Sandbox Code Playgroud)
根据Authentication Quickstart的文档,我按照以下命令来构建身份验证的路由和视图:
php artisan make:auth
Run Code Online (Sandbox Code Playgroud)
它工作正常.注册工作正常.但我在登录时遇到问题.登录后我在route.php文件中测试了以下内容:
Route::get('/', function () {
dd( Auth::user());
return view('welcome');
});
Run Code Online (Sandbox Code Playgroud)
Auth::user()是返回null,也Auth::check()和Auth::guest()没有适当工作.我通过制作新项目一次又一次地尝试了两次,但无法得到正确的结果.
以下是完整的 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');
});
Run Code Online (Sandbox Code Playgroud)
谁能帮我?或是否有人面临同样的问题?我该如何解决?
Mop*_*ppo 29
Laravel 5.2引入了中间件组概念:您可以指定一个或多个中间件属于一个组,并且可以将中间件组应用于一个或多个路由
默认情况下,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,
],
Run Code Online (Sandbox Code Playgroud)
因此,如果要进行会话处理,则应将此中间件组用于要使用身份验证的所有路由:
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() );
});
});
Run Code Online (Sandbox Code Playgroud)
LARAVEL VERSION的更新> = 5.2.27
从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');
});
}
Run Code Online (Sandbox Code Playgroud)
因此,您不再需要手动将web中间件组添加到路由中.
无论如何,如果要对路由使用默认身份验证,仍需要将auth中间件绑定到路由
| 归档时间: |
|
| 查看次数: |
22476 次 |
| 最近记录: |