laravel 5.2中自定义身份验证的错误

时间:2016-05-21 03:52:15

标签: php laravel

我创建一个名为学生的新表

public function up()
    {
        Schema::create('students', function (Blueprint $table) {
            //   $table->increments('id');
            $table->engine = 'InnoDB';

            $table->string('roll_no');
            $table->string('name');
            $table->string('father_name');
            $table->string('address');
            $table->string('cnic');
            $table->string('phone_no');
            $table->string('father_phone_no');
            $table->string('email')->unique();
            $table->string('password');
//            $table->string('dept_id');
            $table->timestamps();

            $table->primary('roll_no');
//            $table->foreign('dept_id')->references('dept_id')->on('departments')->onDelete('cascade');
        });
    }

然后我创建一个空的学生模型

class student extends Authenticatable
{
    //
}

然后我在auth.php文件中进行更改

<?php

return [


    'defaults' => [
        'guard' => 'web',
        'passwords' => 'users',
    ],

    'guards' => [
        'web' => [
            'driver' => 'session',
            'provider' => 'users',
        ],
        'student' => [
            'driver' => 'session',
            'provider' => 'students',
        ],

        'api' => [
            'driver' => 'token',
            'provider' => 'users',
        ],
    ],

    'providers' => [
        'users' => [
            'driver' => 'eloquent',
            'model' => App\User::class,
        ],
        'student' => [
            'driver' => 'eloquent',
            'model' => App\student::class,
        ],
    ],

    'passwords' => [
        'users' => [
            'provider' => 'users',
            'email' => 'auth.emails.password',
            'table' => 'password_resets',
            'expire' => 60,
        ],
        'students' => [
            'provider' => 'students',
            'email' => 'student.auth.emails.password',
            'table' => 'password_resets',
            'expire' => 60,
        ],
    ],

];

然后我添加了一个中间件RedirectifNotStudent

public function handle($request, Closure $next, $guard = 'student')
    {
        if (!Auth::guard($guard)->check()) {
            return redirect('/student/login');
        }

        return $next($request);
    }

然后我将此行添加到kernel.php文件

'student' => \App\Http\Middleware\RedirectIfNotstudent::class,

之后我在该目录中添加登录,注销,注册功能StudentAuth / AuthController

    protected $redirectTo = '/student';
        protected $guard = 'student';
public function showLoginForm()
    {
        if (Auth::guard('student')->check())
        {
            return redirect('/student');
        }

        return view('student.auth.login');
    }

    public function showRegistrationForm()
    {
        return view('student.auth.register');
    }

    public function resetPassword()
    {
        return view('student.auth.passwords.email');
    }

    public function logout(){
        Auth::guard('student')->logout();
        return redirect('/student/login');
    }

之后我在学生/员工中添加员工控制器,就像这样

    class Employee extends Controller
{
    public function __construct(){
        $this->middleware('student');
    }

    public function index(){
        return view('Student.home');
    }
}

这是我的路线文件

    Route::get('/student/login','StudentAuth\AuthController@showLoginForm');
Route::post('/student/login','StudentAuth\AuthController@login');
Route::get('/student/password/reset','StudentAuth\PasswordController@resetPassword');

Route::group(['middleware' => ['student']], function () {
    //Login Routes...
    Route::get('/student/logout','StudentAuth\AuthController@logout');

    // Registration Routes...
    Route::get('student/register', 'StudentAuth\AuthController@showRegistrationForm');
    Route::post('student/register', 'StudentAuth\AuthController@register');

    Route::get('/student', 'Student\Employee@index');
});



Route::group(['middleware' => 'web'], function () {
//    Route::auth();
    Route::get('/home', 'HomeController@index');

内置身份验证工作正常,但我想要另一个学生,并发生此错误

Authentication user provider [] is not defined.  

1 个答案:

答案 0 :(得分:1)

Laravel模型假设默认主键为Id,默认表名称为Model类名。在您的表格中,您已设置&#39; roll_no&#39;作为主键,表名为&#39;学生&#39;。

所以你必须告诉laravel模型表和主键值。

在学生模型中添加此行。

<child.Component>