欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  后端开发

20150720-Laravel登录验证碰到的坑

程序员文章站 2022-04-08 13:06:15
...
记录踩过的坑,以后的坑就会越来越少……

首先 建表:

phpartisanmigrate:make_admin_table

然后在新建的文件里面写上表的各列设置

php

use Illuminate\Database\Schema\Blueprint;
use Illuminate\Database\Migrations\Migration;

class CreateAdminTable extends Migration {

    /**
     * Run the migrations.
     *
     * @return void
     */public function up()
    {
        Schema::create('admin', function($table)
        {
            $table->increments('id');
            $table->string('staff_code', 32)->nullable();           //员工号$table->string('login_name', 32)->nullable();           //登录名$table->string('password', 32)->nullabele();            //登录密码$table->string('mail', 512)->nullable();                //电子邮箱$table->string('staff_name', 32)->nullable();           //员工姓名$table->string('sex', 10)->nullable();                  //性别$table->string('belong_to', 512)->nullable();           //所属部门$table->string('jobs', 512)->nullable();                //岗位$table->string('telephone', 32)->nullable();            //固定电话$table->string('mobile', 32)->nullable();               //手机号
        });
    }

    /**
     * Reverse the migrations.
     *
     * @return void
     */public function down()
    {
        Schema::dropIfExists('admin');
    }

}

建立Model模型Admin:

php ratisan generate modle Admin

再在生成的文件里添加

useIlluminate\Auth\UserTrait;
useIlluminate\Auth\UserInterface;
useIlluminate\Auth\Reminders\RemindableTrait;
useIlluminate\Auth\Reminders\RemindableInterface;

classAdminextends \EloquentimplementsUserInterface, RemindableInterface {useUserTrait, RemindableTrait;

    protected$fillable = [];

    protected$table = 'admin'; // 指定表名protected$primaryKey = 'id'; // 指定主键名protected$hidden = array('password');  //密码字段public$timestamps = false; // 关闭 创建时间 与 更新时间 的自动维护publicfunctiongetRememberToken(){return$this->rememberToken ;
    }

    publicfunctionsetRememberToken($value){$this->rememberToken = $value ;
    }

    publicfunctiongetRememberTokenName(){return$this->reminder ;
    }
}

解释一下,因为需要做登录验证,用的是laravel自带的Auth所以需要添加use和继承UserInterface以及RemindableInterface接口 并重写一些方法
具体的就是这几句

useIlluminate\Auth\UserTrait;
useIlluminate\Auth\UserInterface;
useIlluminate\Auth\Reminders\RemindableTrait;
useIlluminate\Auth\Reminders\RemindableInterface;

classAdminextends \EloquentimplementsUserInterface, RemindableInterface {useUserTrait, RemindableTrait;
    /*******
    以下代码省略
    *******/publicfunctiongetRememberToken(){return$this->rememberToken ;
    }

    publicfunctionsetRememberToken($value){$this->rememberToken = $value ;
    }

    publicfunctiongetRememberTokenName(){return$this->reminder ;
    }
    }

然后我继续找到Auth文件的设定 修改一下需要用到的表
app/config/auth.php
找到如下字段并修改成自己所指定的表

returnarray(

'driver' => 'eloquent', //验证方式,有database和eloquent两种'model' => 'Admin', //所使用的model名'table' => 'admin', //对应的表名'reminder' => array(

        'email' => 'emails.auth.reminder',

        'table' => 'password_reminders',

        'expire' => 60,

    ),

);

然后再添加controller方法:

//获取登录页面public function get_web_login(){

        return View::make('web.web_login');

    }

    //登录验证public function post_login(){
        if (Auth::attempt(array('login_name'=>Input::get('login_name'), 'password'=>Input::get('password')))) {

            Notification::success('登录成功');

            return Redirect::to('/web/index')
            ->with('message', '成功登录');
        } else {

            Notification::warning('用户名密码不正确');

            return Redirect::to('/web/login')
            ->with('message', '用户名密码不正确')
                ->withInput();
        }

    }

然后是视图文件login.blade.php:

@section('title')登录 - @parent@stop@section('nav_1')
    
  • class