laravel5.2 多表验证

之前做了一个单表的验证,今天来说说多表

首先我们来运行一下 php artisan make:auth

然后我们在views目录下建一个admin文件夹,将layouts文件夹包括里边的内容都复制过来,再将home.blade.php文件复制过来,重命名一下dashboard,再建一个auth文件夹,将login和register文件放进去,如下图

laravel5.2 多表验证_第1张图片


我们需要改一下admin下的layouts/app.blade.php文件,在这几个地方,后边会说明为什么改,为了方便看可以把dashboard.blade.php的标题改成Dashboard

laravel5.2 多表验证_第2张图片


还有auth文件夹里login和register表单的action也要改成前面加admin的路径,参照下边的路由


现在写路由文件,加入下面这段

Route::group(['middleware' => ['web']], function () {
    //Login Routes...
    Route::get('/admin/login','AdminAuth\AuthController@showLoginForm');
    Route::post('/admin/login','AdminAuth\AuthController@login');
    Route::get('/admin/logout','AdminAuth\AuthController@logout');

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

    Route::get('/admin', 'AdminController@index');

}); 


有了路由再写Controller和Model吧

运行 php artisan make:controller AdminAuth/AuthControler

middleware($this->guestMiddleware(), ['except' => 'logout']);
    }

    public function showLoginForm()
    {
        if (view()->exists('auth.authenticate')) {
            return view('auth.authenticate');
        }

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

    /**
     * Get a validator for an incoming registration request.
     *
     * @param  array  $data
     * @return \Illuminate\Contracts\Validation\Validator
     */
    protected function validator(array $data)
    {
        return Validator::make($data, [
            'name' => 'required|max:255',
            'email' => 'required|email|max:255|unique:admins',
            'password' => 'required|min:6|confirmed',
        ]);
    }

    /**
     * Create a new user instance after a valid registration.
     *
     * @param  array  $data
     * @return User
     */
    protected function create(array $data)
    {
        return User::create([
            'name' => $data['name'],
            'email' => $data['email'],
            'password' => bcrypt($data['password']),
        ]);
    }
}


php artisan make:controller AdminController

middleware('auth:admin');
    }

    /**
     * Show the application dashboard.
     *
     * @return \Illuminate\Http\Response
     */
    public function index()
    {
        return view('admin.dashboard');
    }
}


php artisan make:model Admin



php artisan make:migration create_admins_table

increments('id');
            $table->string('name');
            $table->string('email')->unique();
            $table->string('password');
            $table->rememberToken();
            $table->timestamps();
        });
    }

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

生成数据库迁移php artisan migrate


然后再对Middleware/Authenticate文件进行修改

guest()) {
            if ($request->ajax() || $request->wantsJson()) {
                return response('Unauthorized.', 401);
            } else {
                if($guard == 'admin'){
                    return redirect('admin/login');
                }
                return redirect()->guest('login');
            }
        }

        return $next($request);
    }
}


这样基本的内容都好了,由于我们想要的是管理员账号的登录,一般不存在注册,所以我们抛弃了注册功能,使用laravel的seed来写

运行 php artisan make:seed AdminsTableSeeder

insert([
            'name' => 'admin',
            'email' => '[email protected]',
            'password' => bcrypt('secret'),
        ]);
    }
}


在database里的seeds,更改DatabaseSeeder

call(AdminsTableSeeder::class);
    }
}

我们运行php artisan db:seed

会在admins数据库写入一个登录账号是[email protected],密码是secret的用户


这样我们就可以直接尝试登录了,本人亲测有用,做这个结合了stackoverflow和youtobe的视频穷屌丝没钱买laravist

不懂留言或加好友

qq 405947712

你可能感兴趣的:(PHP)