laravel 5 用户管理
路由配置
在路由中添加如下定义 查看是否存在 默认是已添加的
Route::get("auth/login","AuthAuthController@getLogin");
Route::post("auth/login","AuthAuthController@postLogin");
Route::get("auth/register","AuthAuthController@getRegister");
Route::post("auth/register","AuthAuthController@postRegister");
Route::get("auth/logout","AuthAuthController@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 IlluminateFoundationAuth;
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命令重新生成表
模型中添加
控制器中添加
声明:该文观点仅代表作者本人,牛骨文系教育信息发布平台,牛骨文仅提供信息存储空间服务。
- 上一篇: MapReduce启动的Map/Reduce子任务简要分析
- 下一篇: 比特币常见问答(Q/A)