laravel 5 用户管理

来源:互联网 发布:如何上淘宝网买东西 编辑:程序博客网 时间:2024/06/05 06:59

路由配置

在路由中添加如下定义 查看是否存在 默认是已添加的

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,项目中没有,

更改默认提交后地址

<?phpnamespace 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命令重新生成表
这里写图片描述
模型中添加
这里写图片描述
控制器中添加

0 0