laravel 5 用户管理
2015-11-13 14:18
645 查看
路由配置
在路由中添加如下定义 查看是否存在 默认是已添加的Route::get("auth/login",'Auth\AuthController@getLogin'); Route::post("auth/login",'Auth\AuthController@postLogin'); Route::get("auth/register",'Auth\AuthController@getRegister'); Route::post("auth/register",'Auth\AuthController@postRegister'); Route::get("auth/logout",'Auth\AuthController@getLogout'); Route::get("user",'UserController@index'); //测试页面用于登录成功 注册成功后的跳转
模板
在resouces/views/下添加auth/login.blade.php auth/register.blade.php
为何如此可跟踪
AuthController
AuthenticatesAndRegistersUsers
AuthenticatesUsers
login.blade.php中添加类似表单
{!! Form::open(["url"=>"auth/login"]) !!} <input type="text" name="email"> <br> <input type="text" name="password"> <input type="submit"> {!! Form::close() !!} @if($errors->any()) @foreach($errors->all() as $err) {!! $err !!} @endforeach @endif
注:提交地址
email password即可
默认是使用email做用户名验证的
$errors 是错误的提示
register.blade.php
<input type="text" name="name"> <br> <input type="text" name="email"> <br> <input type="text" name="password"> <br> <input type="text" name="password_confirmation"> <input type="submit"> {!! Form::close() !!} @if($errors->any()) @foreach($errors->all() as $err) {!! $err !!} @endforeach @endif
password_confirmation两次密码验证
测试
正常 访问 http://localhost:8000/auth/register应该没问题了,注册成功后url会找不到http://localhost:8000/home
因默认成功后返回到/home,项目中没有,
更改默认提交后地址
<?php namespace Illuminate\Foundation\Auth; trait RedirectsUsers { /** * Get the post register / login redirect path. * * @return string */ public function redirectPath() { if (property_exists($this, 'redirectPath')) { return $this->redirectPath; } return property_exists($this, 'redirectTo') ? $this->redirectTo : '/home'; } }
从代码中可以看到如果定义了redirectPath属性则会就使用redirectPath的值了。
在authController.php下添加 redirectPath
class AuthController extends Controller { /* |-------------------------------------------------------------------------- | Registration & Login Controller |-------------------------------------------------------------------------- | | This controller handles the registration of new users, as well as the | authentication of existing users. By default, this controller uses | a simple trait to add these behaviors. Why don't you explore it? | */ use AuthenticatesAndRegistersUsers, ThrottlesLogins; protected $redirectPath="/user"; //注册后的页面 protected $redirectAfterLogout="/user"; //注销后的页 getLogout
查看当前用户的登录信息
// dd(Auth::user()); //当前登录的用户信息 dd(User::all());
Auth::user()->name 即取出当前的用户名
使用用户名登录验证
从AuthenticatesUsers的以下方法中可看出public function loginUsername() { return property_exists($this, 'username') ? $this->username : 'email'; }
如果有username属性就使用username作为登录名否则使用email
protected $username="name"; //使用用户登录 从AuthenticatesUsers中的loginUsername找到的
用户信息添加其它信息
数据库添加字段,添加migrate命令重新生成表
模型中添加
控制器中添加
相关文章推荐
- PHP程序对象、数组串行化(序列化)
- 使用FileZilla Server轻松搭建个人FTP服务器
- ThinkPHP自动验证
- PHP解析XML
- 【转】MAC下打开FTP服务
- ThinkPHP框架搭建应用
- ThinkPHP去除url中的index.php
- editplus快捷键大全
- PHP json_encode
- Thinkphp3.2 PHPMailer 发送 QQ邮箱 163邮箱
- PHP - 验证用户名
- 8.php File操作相关补充
- 在IE10中以文件夹的形式打开FTP
- 淘宝奇门接口PHP例子
- ftp上传工具类
- FFmpeg SDK开发手册(以output_example为示例)
- php限制手机号码发送次数
- PHP 九种预定义超全局变量
- tp框架插入数据之add用法
- 乐视云视频PHP接口操作视频上传,编辑,查询以及删除