Laravel 5推出了一个不错的Auth脚手架,其中包括用于注册和验证用户的所有路由/控制器/视图。但是我最近开始使用Laravel 5.1,并且注意到身份验证不再内置,我怎样才能添加它?如何将验证返回给Laravel 5.1?
回答
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
目录。在该目录中,创建以下文件。
- login.blade.php(https://github.com/laravel/laravel/blob/5.0/resources/views/auth/login.blade.php)
- password.blade.php(https://github.com/laravel/laravel/blob/5.0/resources/views/auth/password.blade.php)
- register.blade.php(https://github.com/laravel/laravel/blob/5.0/resources/views/auth/register.blade.php)
- reset.blade.php(https://github.com/laravel/laravel/blob/5.0/resources/views/auth/reset.blade.php)
对于“忘记密码”电子邮件,请创建resources/views/emails
目录,并将以下文件放入其中。
- password.blade.php(https://github.com/laravel/laravel/blob/5.0/resources/views/emails/password.blade.php)
创建数据库表&
为了让这个我们实际上可以注册一个用户,然后登陆,我们必须创建正确的数据库表。幸运的是,这已经可以通过迁移获得。
首先,创建一个新的数据库表,并在.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.php
和Auth/AuthController.php
文件。
protected $redirectTo = '/dashboard';
身份验证控制板
要限制在那些记录在访问到仪表板中,我们可以DDD以下控制板控制器
public function __construct()
{
$this->middleware('auth');
}
除了通过如指出马蒂托马斯,你也可以尝试使用这个package auth scaffolding。
- 1. 发送Laravel 5.1验证回复给Smarty模板
- 2. 如何在验证返回laravel 5
- 3. 为什么验证::尝试总是返回false在Laravel 5.1
- 4. Laravel 5.1 - 返回第一个错误从FormRequest验证
- 5. Laravel 5.1,嵌套验证
- 6. 问:Laravel 5.1身份验证
- 7. laravel验证,如果验证通过它会返回什么?
- 8. 如何将401验证错误返回给RestSharp客户端?
- 9. ServiceStack Razor - 如何将验证错误返回给表单?
- 10. laravel 5.1表单验证与foreach
- 11. 自定义验证在Laravel规则5.1
- 12. laravel 5.1验证错误默显示`吨
- 13. 验证在Laravel创建用户5.1
- 14. 问题在验证用户:laravel 5.1
- 15. Laravel 5.1身份验证 - 令牌错误
- 16. Laravel 5.1身份验证视图
- 17. 在Laravel 5.1上断言验证错误
- 18. Laravel 5.1嘲讽'唯一'验证规则
- 19. Laravel 5.1。索引数组的验证
- 20. 提交与Laravel 5.1文件输入返回验证错误形式。*
- 21. 返回Laravel验证错误Ajax调用
- 22. laravel保存模型返回json验证
- 23. Laravel表单验证返回下拉值
- 24. Laravel验证返回空错误数组
- 25. laravel 5.1验证尝试登录验证码
- 26. Laravel 5.1返回错误的配置值
- 27. laravel 5.1 auth登录返回错误
- 28. Laravel 5.1哨兵::的getUser()返回null
- 29. Laravel 5.1找雄辩返回null
- 30. Auth :: user()返回null Laravel 5.1 oauth 2.0
雅......我也给这个软件包试了一下。我只是一个控制怪胎,并且想知道到底发生了什么,并将控制器/视图全部组织在同一个地方。但是这个软件包确实工作得很好。 –