您的位置:首页 > 编程语言 > PHP开发

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命令重新生成表



模型中添加



控制器中添加
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: