2015-09-09 61 views
3

Laravel 5推出了一个不错的Auth脚手架,其中包括用于注册和验证用户的所有路由/控制器/视图。但是我最近开始使用Laravel 5.1,并且注意到身份验证不再内置,我怎样才能添加它?如何将验证返回给Laravel 5.1?

回答

7

Laravel确实已经有关于building authentication into your Laravel 5.1 app的文档。不过,我会去通过这一点更详细...

安装Laravel

首先,确保你有一个新的安装Laravel的。这里是我的教程Installing Laravel 5.1 on OSX with MAMP

加入Twitter的引导

downloading bootstrap后增添bootstrap.css文件到public/css目录。 (您可能需要创建CSS目录。

而且在引导的fonts目录到你的应用程序的public目录复制。

添加验证路由

以下途径加入到app/Http/routes.php文件。

// Authentication routes... 
Route::get('auth/login', 'Auth\[email protected]'); 
Route::post('auth/login', 'Auth\[email protected]'); 
Route::get('auth/logout', 'Auth\[email protected]'); 

// Registration routes... 
Route::get('auth/register', 'Auth\[email protected]'); 
Route::post('auth/register', 'Auth\[email protected]'); 

// Password reset link request routes... 
Route::get('password/email', 'Auth\[email protected]'); 
Route::post('password/email', 'Auth\[email protected]'); 

// Password reset routes... 
Route::get('password/reset/{token}', 'Auth\[email protected]'); 
Route::post('password/reset', 'Auth\[email protected]'); 

添加认证视图

首先让我们创建一个刀片模板用于所有其他视图。我们可以通过创建一个resources/views/auth/app.blade.php文件来实现。并复制/粘贴此处显示的代码:https://github.com/laravel/laravel/blob/5.0/resources/views/app.blade.php

创建一个新的resources/views/auth目录。在该目录中,创建以下文件。

对于“忘记密码”电子邮件,请创建resources/views/emails目录,并将以下文件放入其中。

创建数据库表&

为了让这个我们实际上可以注册一个用户,然后登陆,我们必须创建正确的数据库表。幸运的是,这已经可以通过迁移获得。

首先,创建一个新的数据库表,并在.env文件中定义它的连接。

DB_HOST=localhost 
DB_DATABASE=name 
DB_USERNAME=root 
DB_PASSWORD=xxxxxxx 

触发使用以下命令迁移:

php artisan migrate 

由于我使用的是甲基苯丙胺,我试图迁移时得到这个错误。

[PDOException]          

SQLSTATE [HY000] [2002]没有这样的文件或目录

的解决方案是add the unix_socket key with a value of the path that the mysql.sock resides in MAMP

'mysql' => [ 
     'driver' => 'mysql', 
     'host'  => env('DB_HOST', 'localhost'), 
     'database' => env('DB_DATABASE', 'forge'), 
     'username' => env('DB_USERNAME', 'forge'), 
     'password' => env('DB_PASSWORD', ''), 
     'charset' => 'utf8', 
     'collation' => 'utf8_unicode_ci', 
     'prefix' => '', 
     'strict' => false, 
     'unix_socket' => '/Applications/MAMP/tmp/mysql/mysql.sock', 
    ], 

设置SMTP服务器

Laravel 5.1默认为mailtrap.io。我第一次尝试这个,实际上很简单!第一步是到setup mailtrap.io account

更新.ENV文件,SMTP设置(注册后提供)从地址

MAIL_DRIVER=smtp 
MAIL_HOST=mailtrap.io 
MAIL_PORT=2525 
MAIL_USERNAME=xxxxxx 
MAIL_PASSWORD=xxxxxxx 
MAIL_ENCRYPTION=null 

更新在配置/ mail.php文件。

'from' => ['address' => '[email protected]', 'name' => 'test'], 

创建信息中心

添加仪表盘路线

Route::get('dashboard', 'Dash\[email protected]'); 

仪表控制器添加到app/Http/Controllers/Dash/DashboardController.php

<?php 

namespace App\Http\Dash\Controllers; 

use Illuminate\Http\Request; 
    use App\Http\Controllers\Controller; 

class DashboardController extends Controller 
{ 
    public function home(Request $request) 
    { 
     return view('dashboard/home'); 
    } 
} 

注意使用use App\Http\Controllers\Controller;。这很重要,因为我们的仪表板使用了不同的命名空间。

而在resources/views/dashboard/home.blade.php的观点:

@extends('app') 

@section('content') 
<div class="container"> 
    <div class="row"> 
     <div class="col-md-10 col-md-offset-1"> 
      <div class="panel panel-default"> 
       <div class="panel-heading">Dashboard</div> 

       <div class="panel-body"> 
        You are logged in! 
       </div> 
      </div> 
     </div> 
    </div> 
</div> 
@endsection 

更新登录重定向:

  • 更新app/Http/Middleware/RedirectIfAuthenticated

有:

return redirect('/dashboard'); 

添加到Auth/PasswordController.phpAuth/AuthController.php文件。

protected $redirectTo = '/dashboard'; 

身份验证控制板

要限制在那些记录在访问到仪表板中,我们可以DDD以下控制板控制器

public function __construct() 
{ 
    $this->middleware('auth'); 
} 
0
从文档

除了通过如指出马蒂托马斯,你也可以尝试使用这个package auth scaffolding。

+0

雅......我也给这个软件包试了一下。我只是一个控制怪胎,并且想知道到底发生了什么,并将控制器/视图全部组织在同一个地方。但是这个软件包确实工作得很好。 –