威尼斯电脑教程-2019福建高职会计考试

首页

AD联系:507867812

威尼斯电脑教程

时间:2019-11-19 01:45:46 作者:2015年新开户体验金 浏览量:66842

威尼斯电脑教程

1.前台登录

直接使用laravel自带的auth

php artisan make:auth

然后可以查看路由文件:

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

执行php artisan migrate

会发现生成了两张表。

2.后台登录

编辑配置文件

config\auth.php

添加guards中的admin和providers中的admins

<?phpreturn [ 'defaults' => [ 'guard' => 'web', 'passwords' => 'users', ], 'guards' => [ 'web' => [ 'driver' => 'session', 'provider' => 'users', ], 'admin' => [ 'driver' => 'session', 'provider' => 'admins', ], 'api' => [ 'driver' => 'token', 'provider' => 'users', ], ], 'providers' => [ 'users' => [ 'driver' => 'eloquent', 'model' => App\User::class, ], 'admins' => [ 'driver' => 'eloquent', 'model' => App\Admin::class, ], ], 'passwords' => [ 'users' => [ 'provider' => 'users', 'email' => 'auth.emails.password', 'table' => 'password_resets', 'expire' => 60, ], ],];

3.创建数据模型

执行php artisan make:model Admin --migration

然后修改database\migrations中的数据表结构,复制user表的就可以

执行php artisan migrate 会发现生成了admin表

4.定义后台路由

这里我直接定义了一个路由组

Route::group(['prefix' => 'admin' ,'middleware' => 'admin'], function () { Route::get('login', 'Admin\AuthController@getLogin'); Route::post('login', 'Admin\AuthController@postLogin'); Route::get('register', 'Admin\AuthController@getRegister'); Route::post('register', 'Admin\AuthController@postRegister'); Route::get('logout', 'Admin\AuthController@logout'); Route::get('/', 'Admin\AdminController@index');});

5.创建控制器

执行

php artisan make:controller Admin/AuthControllerphp artisan make:controller Admin/AdminController

AuthController.php可参照Auth中的AuthController.php

<?phpnamespace App\Http\Controllers\Admin;use App\Admin;use Validator;use App\Http\Controllers\Controller;use Illuminate\Foundation\Auth\ThrottlesLogins;use Illuminate\Foundation\Auth\AuthenticatesAndRegistersUsers;class AuthController extends Controller{ use AuthenticatesAndRegistersUsers, ThrottlesLogins; protected $redirectTo = '/admin'; protected $guard = 'admin'; protected $loginView = 'admin.login'; protected $registerView = 'admin.register'; public function __construct() { $this->middleware('guest:admin', ['except' => 'logout']); } protected function validator(array $data) { return Validator::make($data, [ 'name' => 'required|max:255', 'email' => 'required|email|max:255|unique:admins', 'password' => 'required|confirmed|min:6', ]); } protected function create(array $data) { return Admin::create([ 'name' => $data['name'], 'email' => $data['email'], 'password' => bcrypt($data['password']), ]); }}

AdminController.php

<?phpnamespace App\Http\Controllers\Admin;use Illuminate\Http\Request;use App\Http\Requests;use App\Http\Controllers\Controller;use Auth;class AdminController extends Controller{ public function __construct() { $this->middleware('auth:admin'); } public function index() { $admin = Auth::guard('admin')->user(); return view('admin.home'); }}

6.创建视图

这里直接复制auth中的视图和home.blade.php到一个新的admin文件夹中。

修改login和register中表单的action,添加上admin前缀

修改layouts中的app.blade.php

<ul class="nav navbar-nav navbar-right"> <!-- Authentication Links --> @if (Auth::guest('admin')) <li><a href="{{ url('/login') }}">Login</a></li> <li><a href="{{ url('/register') }}">Register</a></li> @else <li class="dropdown"> <a href="#" class="dropdown-toggle" data-toggle="dropdown" role="button" aria-expanded="false"> {{ Auth::guard('admin')->user()->name }} <span class="caret"></span> </a> <ul class="dropdown-menu" role="menu"> <li><a href="{{ url('/admin/logout') }}"><i class="fa fa-btn fa-sign-out"></i>Logout</a></li> </ul> </li> @endif </ul>

现在试一下前后台分别登录吧!

7.可能遇到的页面跳转问题

如果你遇到了这种情况可以尝试修改一下Middleware\Authenticate.php

return redirect()->guest($guard.'/login');

以上所述是小编给大家介绍的laravel5.2实现区分前后台用户登录的方法,希望对大家有所帮助,如果大家有任何疑问请给我留言,小编会及时回复大家的。在此也非常感谢大家对脚本之家网站的支持!

1.前台登录

直接使用laravel自带的auth

php artisan make:auth

然后可以查看路由文件:

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

执行php artisan migrate

会发现生成了两张表。

2.后台登录

编辑配置文件

config\auth.php

添加guards中的admin和providers中的admins

<?phpreturn [ 'defaults' => [ 'guard' => 'web', 'passwords' => 'users', ], 'guards' => [ 'web' => [ 'driver' => 'session', 'provider' => 'users', ], 'admin' => [ 'driver' => 'session', 'provider' => 'admins', ], 'api' => [ 'driver' => 'token', 'provider' => 'users', ], ], 'providers' => [ 'users' => [ 'driver' => 'eloquent', 'model' => App\User::class, ], 'admins' => [ 'driver' => 'eloquent', 'model' => App\Admin::class, ], ], 'passwords' => [ 'users' => [ 'provider' => 'users', 'email' => 'auth.emails.password', 'table' => 'password_resets', 'expire' => 60, ], ],];

3.创建数据模型

执行php artisan make:model Admin --migration

然后修改database\migrations中的数据表结构,复制user表的就可以

执行php artisan migrate 会发现生成了admin表

4.定义后台路由

这里我直接定义了一个路由组

Route::group(['prefix' => 'admin' ,'middleware' => 'admin'], function () { Route::get('login', 'Admin\AuthController@getLogin'); Route::post('login', 'Admin\AuthController@postLogin'); Route::get('register', 'Admin\AuthController@getRegister'); Route::post('register', 'Admin\AuthController@postRegister'); Route::get('logout', 'Admin\AuthController@logout'); Route::get('/', 'Admin\AdminController@index');});

5.创建控制器

执行

php artisan make:controller Admin/AuthControllerphp artisan make:controller Admin/AdminController

AuthController.php可参照Auth中的AuthController.php

<?phpnamespace App\Http\Controllers\Admin;use App\Admin;use Validator;use App\Http\Controllers\Controller;use Illuminate\Foundation\Auth\ThrottlesLogins;use Illuminate\Foundation\Auth\AuthenticatesAndRegistersUsers;class AuthController extends Controller{ use AuthenticatesAndRegistersUsers, ThrottlesLogins; protected $redirectTo = '/admin'; protected $guard = 'admin'; protected $loginView = 'admin.login'; protected $registerView = 'admin.register'; public function __construct() { $this->middleware('guest:admin', ['except' => 'logout']); } protected function validator(array $data) { return Validator::make($data, [ 'name' => 'required|max:255', 'email' => 'required|email|max:255|unique:admins', 'password' => 'required|confirmed|min:6', ]); } protected function create(array $data) { return Admin::create([ 'name' => $data['name'], 'email' => $data['email'], 'password' => bcrypt($data['password']), ]); }}

AdminController.php

<?phpnamespace App\Http\Controllers\Admin;use Illuminate\Http\Request;use App\Http\Requests;use App\Http\Controllers\Controller;use Auth;class AdminController extends Controller{ public function __construct() { $this->middleware('auth:admin'); } public function index() { $admin = Auth::guard('admin')->user(); return view('admin.home'); }}

6.创建视图

这里直接复制auth中的视图和home.blade.php到一个新的admin文件夹中。

修改login和register中表单的action,添加上admin前缀

修改layouts中的app.blade.php

<ul class="nav navbar-nav navbar-right"> <!-- Authentication Links --> @if (Auth::guest('admin')) <li><a href="{{ url('/login') }}">Login</a></li> <li><a href="{{ url('/register') }}">Register</a></li> @else <li class="dropdown"> <a href="#" class="dropdown-toggle" data-toggle="dropdown" role="button" aria-expanded="false"> {{ Auth::guard('admin')->user()->name }} <span class="caret"></span> </a> <ul class="dropdown-menu" role="menu"> <li><a href="{{ url('/admin/logout') }}"><i class="fa fa-btn fa-sign-out"></i>Logout</a></li> </ul> </li> @endif </ul>

现在试一下前后台分别登录吧!

7.可能遇到的页面跳转问题

如果你遇到了这种情况可以尝试修改一下Middleware\Authenticate.php

return redirect()->guest($guard.'/login');

以上所述是小编给大家介绍的laravel5.2实现区分前后台用户登录的方法,希望对大家有所帮助,如果大家有任何疑问请给我留言,小编会及时回复大家的。在此也非常感谢大家对脚本之家网站的支持!

1.前台登录

直接使用laravel自带的auth

php artisan make:auth

然后可以查看路由文件:

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

执行php artisan migrate

会发现生成了两张表。

2.后台登录

编辑配置文件

config\auth.php

添加guards中的admin和providers中的admins

<?phpreturn [ 'defaults' => [ 'guard' => 'web', 'passwords' => 'users', ], 'guards' => [ 'web' => [ 'driver' => 'session', 'provider' => 'users', ], 'admin' => [ 'driver' => 'session', 'provider' => 'admins', ], 'api' => [ 'driver' => 'token', 'provider' => 'users', ], ], 'providers' => [ 'users' => [ 'driver' => 'eloquent', 'model' => App\User::class, ], 'admins' => [ 'driver' => 'eloquent', 'model' => App\Admin::class, ], ], 'passwords' => [ 'users' => [ 'provider' => 'users', 'email' => 'auth.emails.password', 'table' => 'password_resets', 'expire' => 60, ], ],];

3.创建数据模型

执行php artisan make:model Admin --migration

然后修改database\migrations中的数据表结构,复制user表的就可以

执行php artisan migrate 会发现生成了admin表

4.定义后台路由

这里我直接定义了一个路由组

Route::group(['prefix' => 'admin' ,'middleware' => 'admin'], function () { Route::get('login', 'Admin\AuthController@getLogin'); Route::post('login', 'Admin\AuthController@postLogin'); Route::get('register', 'Admin\AuthController@getRegister'); Route::post('register', 'Admin\AuthController@postRegister'); Route::get('logout', 'Admin\AuthController@logout'); Route::get('/', 'Admin\AdminController@index');});

5.创建控制器

执行

php artisan make:controller Admin/AuthControllerphp artisan make:controller Admin/AdminController

AuthController.php可参照Auth中的AuthController.php

<?phpnamespace App\Http\Controllers\Admin;use App\Admin;use Validator;use App\Http\Controllers\Controller;use Illuminate\Foundation\Auth\ThrottlesLogins;use Illuminate\Foundation\Auth\AuthenticatesAndRegistersUsers;class AuthController extends Controller{ use AuthenticatesAndRegistersUsers, ThrottlesLogins; protected $redirectTo = '/admin'; protected $guard = 'admin'; protected $loginView = 'admin.login'; protected $registerView = 'admin.register'; public function __construct() { $this->middleware('guest:admin', ['except' => 'logout']); } protected function validator(array $data) { return Validator::make($data, [ 'name' => 'required|max:255', 'email' => 'required|email|max:255|unique:admins', 'password' => 'required|confirmed|min:6', ]); } protected function create(array $data) { return Admin::create([ 'name' => $data['name'], 'email' => $data['email'], 'password' => bcrypt($data['password']), ]); }}

AdminController.php

<?phpnamespace App\Http\Controllers\Admin;use Illuminate\Http\Request;use App\Http\Requests;use App\Http\Controllers\Controller;use Auth;class AdminController extends Controller{ public function __construct() { $this->middleware('auth:admin'); } public function index() { $admin = Auth::guard('admin')->user(); return view('admin.home'); }}

6.创建视图

这里直接复制auth中的视图和home.blade.php到一个新的admin文件夹中。

修改login和register中表单的action,添加上admin前缀

修改layouts中的app.blade.php

<ul class="nav navbar-nav navbar-right"> <!-- Authentication Links --> @if (Auth::guest('admin')) <li><a href="{{ url('/login') }}">Login</a></li> <li><a href="{{ url('/register') }}">Register</a></li> @else <li class="dropdown"> <a href="#" class="dropdown-toggle" data-toggle="dropdown" role="button" aria-expanded="false"> {{ Auth::guard('admin')->user()->name }} <span class="caret"></span> </a> <ul class="dropdown-menu" role="menu"> <li><a href="{{ url('/admin/logout') }}"><i class="fa fa-btn fa-sign-out"></i>Logout</a></li> </ul> </li> @endif </ul>

现在试一下前后台分别登录吧!

7.可能遇到的页面跳转问题

如果你遇到了这种情况可以尝试修改一下Middleware\Authenticate.php

return redirect()->guest($guard.'/login');

以上所述是小编给大家介绍的laravel5.2实现区分前后台用户登录的方法,希望对大家有所帮助,如果大家有任何疑问请给我留言,小编会及时回复大家的。在此也非常感谢大家对脚本之家网站的支持!

laravel5.2实现区分前后台用户登录的方法laravel5.2实现区分前后台用户登录的方法laravel5.2实现区分前后台用户登录的方法

1.前台登录

直接使用laravel自带的auth

php artisan make:auth

然后可以查看路由文件:

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

执行php artisan migrate

会发现生成了两张表。

2.后台登录

编辑配置文件

config\auth.php

添加guards中的admin和providers中的admins

<?phpreturn [ 'defaults' => [ 'guard' => 'web', 'passwords' => 'users', ], 'guards' => [ 'web' => [ 'driver' => 'session', 'provider' => 'users', ], 'admin' => [ 'driver' => 'session', 'provider' => 'admins', ], 'api' => [ 'driver' => 'token', 'provider' => 'users', ], ], 'providers' => [ 'users' => [ 'driver' => 'eloquent', 'model' => App\User::class, ], 'admins' => [ 'driver' => 'eloquent', 'model' => App\Admin::class, ], ], 'passwords' => [ 'users' => [ 'provider' => 'users', 'email' => 'auth.emails.password', 'table' => 'password_resets', 'expire' => 60, ], ],];

3.创建数据模型

执行php artisan make:model Admin --migration

然后修改database\migrations中的数据表结构,复制user表的就可以

执行php artisan migrate 会发现生成了admin表

4.定义后台路由

这里我直接定义了一个路由组

Route::group(['prefix' => 'admin' ,'middleware' => 'admin'], function () { Route::get('login', 'Admin\AuthController@getLogin'); Route::post('login', 'Admin\AuthController@postLogin'); Route::get('register', 'Admin\AuthController@getRegister'); Route::post('register', 'Admin\AuthController@postRegister'); Route::get('logout', 'Admin\AuthController@logout'); Route::get('/', 'Admin\AdminController@index');});

5.创建控制器

执行

php artisan make:controller Admin/AuthControllerphp artisan make:controller Admin/AdminController

AuthController.php可参照Auth中的AuthController.php

<?phpnamespace App\Http\Controllers\Admin;use App\Admin;use Validator;use App\Http\Controllers\Controller;use Illuminate\Foundation\Auth\ThrottlesLogins;use Illuminate\Foundation\Auth\AuthenticatesAndRegistersUsers;class AuthController extends Controller{ use AuthenticatesAndRegistersUsers, ThrottlesLogins; protected $redirectTo = '/admin'; protected $guard = 'admin'; protected $loginView = 'admin.login'; protected $registerView = 'admin.register'; public function __construct() { $this->middleware('guest:admin', ['except' => 'logout']); } protected function validator(array $data) { return Validator::make($data, [ 'name' => 'required|max:255', 'email' => 'required|email|max:255|unique:admins', 'password' => 'required|confirmed|min:6', ]); } protected function create(array $data) { return Admin::create([ 'name' => $data['name'], 'email' => $data['email'], 'password' => bcrypt($data['password']), ]); }}

AdminController.php

<?phpnamespace App\Http\Controllers\Admin;use Illuminate\Http\Request;use App\Http\Requests;use App\Http\Controllers\Controller;use Auth;class AdminController extends Controller{ public function __construct() { $this->middleware('auth:admin'); } public function index() { $admin = Auth::guard('admin')->user(); return view('admin.home'); }}

6.创建视图

这里直接复制auth中的视图和home.blade.php到一个新的admin文件夹中。

修改login和register中表单的action,添加上admin前缀

修改layouts中的app.blade.php

<ul class="nav navbar-nav navbar-right"> <!-- Authentication Links --> @if (Auth::guest('admin')) <li><a href="{{ url('/login') }}">Login</a></li> <li><a href="{{ url('/register') }}">Register</a></li> @else <li class="dropdown"> <a href="#" class="dropdown-toggle" data-toggle="dropdown" role="button" aria-expanded="false"> {{ Auth::guard('admin')->user()->name }} <span class="caret"></span> </a> <ul class="dropdown-menu" role="menu"> <li><a href="{{ url('/admin/logout') }}"><i class="fa fa-btn fa-sign-out"></i>Logout</a></li> </ul> </li> @endif </ul>

现在试一下前后台分别登录吧!

7.可能遇到的页面跳转问题

如果你遇到了这种情况可以尝试修改一下Middleware\Authenticate.php

return redirect()->guest($guard.'/login');

以上所述是小编给大家介绍的laravel5.2实现区分前后台用户登录的方法,希望对大家有所帮助,如果大家有任何疑问请给我留言,小编会及时回复大家的。在此也非常感谢大家对脚本之家网站的支持!

,见下图

1.前台登录

直接使用laravel自带的auth

php artisan make:auth

然后可以查看路由文件:

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

执行php artisan migrate

会发现生成了两张表。

2.后台登录

编辑配置文件

config\auth.php

添加guards中的admin和providers中的admins

<?phpreturn [ 'defaults' => [ 'guard' => 'web', 'passwords' => 'users', ], 'guards' => [ 'web' => [ 'driver' => 'session', 'provider' => 'users', ], 'admin' => [ 'driver' => 'session', 'provider' => 'admins', ], 'api' => [ 'driver' => 'token', 'provider' => 'users', ], ], 'providers' => [ 'users' => [ 'driver' => 'eloquent', 'model' => App\User::class, ], 'admins' => [ 'driver' => 'eloquent', 'model' => App\Admin::class, ], ], 'passwords' => [ 'users' => [ 'provider' => 'users', 'email' => 'auth.emails.password', 'table' => 'password_resets', 'expire' => 60, ], ],];

3.创建数据模型

执行php artisan make:model Admin --migration

然后修改database\migrations中的数据表结构,复制user表的就可以

执行php artisan migrate 会发现生成了admin表

4.定义后台路由

这里我直接定义了一个路由组

Route::group(['prefix' => 'admin' ,'middleware' => 'admin'], function () { Route::get('login', 'Admin\AuthController@getLogin'); Route::post('login', 'Admin\AuthController@postLogin'); Route::get('register', 'Admin\AuthController@getRegister'); Route::post('register', 'Admin\AuthController@postRegister'); Route::get('logout', 'Admin\AuthController@logout'); Route::get('/', 'Admin\AdminController@index');});

5.创建控制器

执行

php artisan make:controller Admin/AuthControllerphp artisan make:controller Admin/AdminController

AuthController.php可参照Auth中的AuthController.php

<?phpnamespace App\Http\Controllers\Admin;use App\Admin;use Validator;use App\Http\Controllers\Controller;use Illuminate\Foundation\Auth\ThrottlesLogins;use Illuminate\Foundation\Auth\AuthenticatesAndRegistersUsers;class AuthController extends Controller{ use AuthenticatesAndRegistersUsers, ThrottlesLogins; protected $redirectTo = '/admin'; protected $guard = 'admin'; protected $loginView = 'admin.login'; protected $registerView = 'admin.register'; public function __construct() { $this->middleware('guest:admin', ['except' => 'logout']); } protected function validator(array $data) { return Validator::make($data, [ 'name' => 'required|max:255', 'email' => 'required|email|max:255|unique:admins', 'password' => 'required|confirmed|min:6', ]); } protected function create(array $data) { return Admin::create([ 'name' => $data['name'], 'email' => $data['email'], 'password' => bcrypt($data['password']), ]); }}

AdminController.php

<?phpnamespace App\Http\Controllers\Admin;use Illuminate\Http\Request;use App\Http\Requests;use App\Http\Controllers\Controller;use Auth;class AdminController extends Controller{ public function __construct() { $this->middleware('auth:admin'); } public function index() { $admin = Auth::guard('admin')->user(); return view('admin.home'); }}

6.创建视图

这里直接复制auth中的视图和home.blade.php到一个新的admin文件夹中。

修改login和register中表单的action,添加上admin前缀

修改layouts中的app.blade.php

<ul class="nav navbar-nav navbar-right"> <!-- Authentication Links --> @if (Auth::guest('admin')) <li><a href="{{ url('/login') }}">Login</a></li> <li><a href="{{ url('/register') }}">Register</a></li> @else <li class="dropdown"> <a href="#" class="dropdown-toggle" data-toggle="dropdown" role="button" aria-expanded="false"> {{ Auth::guard('admin')->user()->name }} <span class="caret"></span> </a> <ul class="dropdown-menu" role="menu"> <li><a href="{{ url('/admin/logout') }}"><i class="fa fa-btn fa-sign-out"></i>Logout</a></li> </ul> </li> @endif </ul>

现在试一下前后台分别登录吧!

7.可能遇到的页面跳转问题

如果你遇到了这种情况可以尝试修改一下Middleware\Authenticate.php

return redirect()->guest($guard.'/login');

以上所述是小编给大家介绍的laravel5.2实现区分前后台用户登录的方法,希望对大家有所帮助,如果大家有任何疑问请给我留言,小编会及时回复大家的。在此也非常感谢大家对脚本之家网站的支持!

laravel5.2实现区分前后台用户登录的方法

1.前台登录

直接使用laravel自带的auth

php artisan make:auth

然后可以查看路由文件:

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

执行php artisan migrate

会发现生成了两张表。

2.后台登录

编辑配置文件

config\auth.php

添加guards中的admin和providers中的admins

<?phpreturn [ 'defaults' => [ 'guard' => 'web', 'passwords' => 'users', ], 'guards' => [ 'web' => [ 'driver' => 'session', 'provider' => 'users', ], 'admin' => [ 'driver' => 'session', 'provider' => 'admins', ], 'api' => [ 'driver' => 'token', 'provider' => 'users', ], ], 'providers' => [ 'users' => [ 'driver' => 'eloquent', 'model' => App\User::class, ], 'admins' => [ 'driver' => 'eloquent', 'model' => App\Admin::class, ], ], 'passwords' => [ 'users' => [ 'provider' => 'users', 'email' => 'auth.emails.password', 'table' => 'password_resets', 'expire' => 60, ], ],];

3.创建数据模型

执行php artisan make:model Admin --migration

然后修改database\migrations中的数据表结构,复制user表的就可以

执行php artisan migrate 会发现生成了admin表

4.定义后台路由

这里我直接定义了一个路由组

Route::group(['prefix' => 'admin' ,'middleware' => 'admin'], function () { Route::get('login', 'Admin\AuthController@getLogin'); Route::post('login', 'Admin\AuthController@postLogin'); Route::get('register', 'Admin\AuthController@getRegister'); Route::post('register', 'Admin\AuthController@postRegister'); Route::get('logout', 'Admin\AuthController@logout'); Route::get('/', 'Admin\AdminController@index');});

5.创建控制器

执行

php artisan make:controller Admin/AuthControllerphp artisan make:controller Admin/AdminController

AuthController.php可参照Auth中的AuthController.php

<?phpnamespace App\Http\Controllers\Admin;use App\Admin;use Validator;use App\Http\Controllers\Controller;use Illuminate\Foundation\Auth\ThrottlesLogins;use Illuminate\Foundation\Auth\AuthenticatesAndRegistersUsers;class AuthController extends Controller{ use AuthenticatesAndRegistersUsers, ThrottlesLogins; protected $redirectTo = '/admin'; protected $guard = 'admin'; protected $loginView = 'admin.login'; protected $registerView = 'admin.register'; public function __construct() { $this->middleware('guest:admin', ['except' => 'logout']); } protected function validator(array $data) { return Validator::make($data, [ 'name' => 'required|max:255', 'email' => 'required|email|max:255|unique:admins', 'password' => 'required|confirmed|min:6', ]); } protected function create(array $data) { return Admin::create([ 'name' => $data['name'], 'email' => $data['email'], 'password' => bcrypt($data['password']), ]); }}

AdminController.php

<?phpnamespace App\Http\Controllers\Admin;use Illuminate\Http\Request;use App\Http\Requests;use App\Http\Controllers\Controller;use Auth;class AdminController extends Controller{ public function __construct() { $this->middleware('auth:admin'); } public function index() { $admin = Auth::guard('admin')->user(); return view('admin.home'); }}

6.创建视图

这里直接复制auth中的视图和home.blade.php到一个新的admin文件夹中。

修改login和register中表单的action,添加上admin前缀

修改layouts中的app.blade.php

<ul class="nav navbar-nav navbar-right"> <!-- Authentication Links --> @if (Auth::guest('admin')) <li><a href="{{ url('/login') }}">Login</a></li> <li><a href="{{ url('/register') }}">Register</a></li> @else <li class="dropdown"> <a href="#" class="dropdown-toggle" data-toggle="dropdown" role="button" aria-expanded="false"> {{ Auth::guard('admin')->user()->name }} <span class="caret"></span> </a> <ul class="dropdown-menu" role="menu"> <li><a href="{{ url('/admin/logout') }}"><i class="fa fa-btn fa-sign-out"></i>Logout</a></li> </ul> </li> @endif </ul>

现在试一下前后台分别登录吧!

7.可能遇到的页面跳转问题

如果你遇到了这种情况可以尝试修改一下Middleware\Authenticate.php

return redirect()->guest($guard.'/login');

以上所述是小编给大家介绍的laravel5.2实现区分前后台用户登录的方法,希望对大家有所帮助,如果大家有任何疑问请给我留言,小编会及时回复大家的。在此也非常感谢大家对脚本之家网站的支持!

,见下图

1.前台登录

直接使用laravel自带的auth

php artisan make:auth

然后可以查看路由文件:

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

执行php artisan migrate

会发现生成了两张表。

2.后台登录

编辑配置文件

config\auth.php

添加guards中的admin和providers中的admins

<?phpreturn [ 'defaults' => [ 'guard' => 'web', 'passwords' => 'users', ], 'guards' => [ 'web' => [ 'driver' => 'session', 'provider' => 'users', ], 'admin' => [ 'driver' => 'session', 'provider' => 'admins', ], 'api' => [ 'driver' => 'token', 'provider' => 'users', ], ], 'providers' => [ 'users' => [ 'driver' => 'eloquent', 'model' => App\User::class, ], 'admins' => [ 'driver' => 'eloquent', 'model' => App\Admin::class, ], ], 'passwords' => [ 'users' => [ 'provider' => 'users', 'email' => 'auth.emails.password', 'table' => 'password_resets', 'expire' => 60, ], ],];

3.创建数据模型

执行php artisan make:model Admin --migration

然后修改database\migrations中的数据表结构,复制user表的就可以

执行php artisan migrate 会发现生成了admin表

4.定义后台路由

这里我直接定义了一个路由组

Route::group(['prefix' => 'admin' ,'middleware' => 'admin'], function () { Route::get('login', 'Admin\AuthController@getLogin'); Route::post('login', 'Admin\AuthController@postLogin'); Route::get('register', 'Admin\AuthController@getRegister'); Route::post('register', 'Admin\AuthController@postRegister'); Route::get('logout', 'Admin\AuthController@logout'); Route::get('/', 'Admin\AdminController@index');});

5.创建控制器

执行

php artisan make:controller Admin/AuthControllerphp artisan make:controller Admin/AdminController

AuthController.php可参照Auth中的AuthController.php

<?phpnamespace App\Http\Controllers\Admin;use App\Admin;use Validator;use App\Http\Controllers\Controller;use Illuminate\Foundation\Auth\ThrottlesLogins;use Illuminate\Foundation\Auth\AuthenticatesAndRegistersUsers;class AuthController extends Controller{ use AuthenticatesAndRegistersUsers, ThrottlesLogins; protected $redirectTo = '/admin'; protected $guard = 'admin'; protected $loginView = 'admin.login'; protected $registerView = 'admin.register'; public function __construct() { $this->middleware('guest:admin', ['except' => 'logout']); } protected function validator(array $data) { return Validator::make($data, [ 'name' => 'required|max:255', 'email' => 'required|email|max:255|unique:admins', 'password' => 'required|confirmed|min:6', ]); } protected function create(array $data) { return Admin::create([ 'name' => $data['name'], 'email' => $data['email'], 'password' => bcrypt($data['password']), ]); }}

AdminController.php

<?phpnamespace App\Http\Controllers\Admin;use Illuminate\Http\Request;use App\Http\Requests;use App\Http\Controllers\Controller;use Auth;class AdminController extends Controller{ public function __construct() { $this->middleware('auth:admin'); } public function index() { $admin = Auth::guard('admin')->user(); return view('admin.home'); }}

6.创建视图

这里直接复制auth中的视图和home.blade.php到一个新的admin文件夹中。

修改login和register中表单的action,添加上admin前缀

修改layouts中的app.blade.php

<ul class="nav navbar-nav navbar-right"> <!-- Authentication Links --> @if (Auth::guest('admin')) <li><a href="{{ url('/login') }}">Login</a></li> <li><a href="{{ url('/register') }}">Register</a></li> @else <li class="dropdown"> <a href="#" class="dropdown-toggle" data-toggle="dropdown" role="button" aria-expanded="false"> {{ Auth::guard('admin')->user()->name }} <span class="caret"></span> </a> <ul class="dropdown-menu" role="menu"> <li><a href="{{ url('/admin/logout') }}"><i class="fa fa-btn fa-sign-out"></i>Logout</a></li> </ul> </li> @endif </ul>

现在试一下前后台分别登录吧!

7.可能遇到的页面跳转问题

如果你遇到了这种情况可以尝试修改一下Middleware\Authenticate.php

return redirect()->guest($guard.'/login');

以上所述是小编给大家介绍的laravel5.2实现区分前后台用户登录的方法,希望对大家有所帮助,如果大家有任何疑问请给我留言,小编会及时回复大家的。在此也非常感谢大家对脚本之家网站的支持!

laravel5.2实现区分前后台用户登录的方法laravel5.2实现区分前后台用户登录的方法

1.前台登录

直接使用laravel自带的auth

php artisan make:auth

然后可以查看路由文件:

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

执行php artisan migrate

会发现生成了两张表。

2.后台登录

编辑配置文件

config\auth.php

添加guards中的admin和providers中的admins

<?phpreturn [ 'defaults' => [ 'guard' => 'web', 'passwords' => 'users', ], 'guards' => [ 'web' => [ 'driver' => 'session', 'provider' => 'users', ], 'admin' => [ 'driver' => 'session', 'provider' => 'admins', ], 'api' => [ 'driver' => 'token', 'provider' => 'users', ], ], 'providers' => [ 'users' => [ 'driver' => 'eloquent', 'model' => App\User::class, ], 'admins' => [ 'driver' => 'eloquent', 'model' => App\Admin::class, ], ], 'passwords' => [ 'users' => [ 'provider' => 'users', 'email' => 'auth.emails.password', 'table' => 'password_resets', 'expire' => 60, ], ],];

3.创建数据模型

执行php artisan make:model Admin --migration

然后修改database\migrations中的数据表结构,复制user表的就可以

执行php artisan migrate 会发现生成了admin表

4.定义后台路由

这里我直接定义了一个路由组

Route::group(['prefix' => 'admin' ,'middleware' => 'admin'], function () { Route::get('login', 'Admin\AuthController@getLogin'); Route::post('login', 'Admin\AuthController@postLogin'); Route::get('register', 'Admin\AuthController@getRegister'); Route::post('register', 'Admin\AuthController@postRegister'); Route::get('logout', 'Admin\AuthController@logout'); Route::get('/', 'Admin\AdminController@index');});

5.创建控制器

执行

php artisan make:controller Admin/AuthControllerphp artisan make:controller Admin/AdminController

AuthController.php可参照Auth中的AuthController.php

<?phpnamespace App\Http\Controllers\Admin;use App\Admin;use Validator;use App\Http\Controllers\Controller;use Illuminate\Foundation\Auth\ThrottlesLogins;use Illuminate\Foundation\Auth\AuthenticatesAndRegistersUsers;class AuthController extends Controller{ use AuthenticatesAndRegistersUsers, ThrottlesLogins; protected $redirectTo = '/admin'; protected $guard = 'admin'; protected $loginView = 'admin.login'; protected $registerView = 'admin.register'; public function __construct() { $this->middleware('guest:admin', ['except' => 'logout']); } protected function validator(array $data) { return Validator::make($data, [ 'name' => 'required|max:255', 'email' => 'required|email|max:255|unique:admins', 'password' => 'required|confirmed|min:6', ]); } protected function create(array $data) { return Admin::create([ 'name' => $data['name'], 'email' => $data['email'], 'password' => bcrypt($data['password']), ]); }}

AdminController.php

<?phpnamespace App\Http\Controllers\Admin;use Illuminate\Http\Request;use App\Http\Requests;use App\Http\Controllers\Controller;use Auth;class AdminController extends Controller{ public function __construct() { $this->middleware('auth:admin'); } public function index() { $admin = Auth::guard('admin')->user(); return view('admin.home'); }}

6.创建视图

这里直接复制auth中的视图和home.blade.php到一个新的admin文件夹中。

修改login和register中表单的action,添加上admin前缀

修改layouts中的app.blade.php

<ul class="nav navbar-nav navbar-right"> <!-- Authentication Links --> @if (Auth::guest('admin')) <li><a href="{{ url('/login') }}">Login</a></li> <li><a href="{{ url('/register') }}">Register</a></li> @else <li class="dropdown"> <a href="#" class="dropdown-toggle" data-toggle="dropdown" role="button" aria-expanded="false"> {{ Auth::guard('admin')->user()->name }} <span class="caret"></span> </a> <ul class="dropdown-menu" role="menu"> <li><a href="{{ url('/admin/logout') }}"><i class="fa fa-btn fa-sign-out"></i>Logout</a></li> </ul> </li> @endif </ul>

现在试一下前后台分别登录吧!

7.可能遇到的页面跳转问题

如果你遇到了这种情况可以尝试修改一下Middleware\Authenticate.php

return redirect()->guest($guard.'/login');

以上所述是小编给大家介绍的laravel5.2实现区分前后台用户登录的方法,希望对大家有所帮助,如果大家有任何疑问请给我留言,小编会及时回复大家的。在此也非常感谢大家对脚本之家网站的支持!

,如下图

laravel5.2实现区分前后台用户登录的方法laravel5.2实现区分前后台用户登录的方法

laravel5.2实现区分前后台用户登录的方法

如下图

laravel5.2实现区分前后台用户登录的方法,如下图

1.前台登录

直接使用laravel自带的auth

php artisan make:auth

然后可以查看路由文件:

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

执行php artisan migrate

会发现生成了两张表。

2.后台登录

编辑配置文件

config\auth.php

添加guards中的admin和providers中的admins

<?phpreturn [ 'defaults' => [ 'guard' => 'web', 'passwords' => 'users', ], 'guards' => [ 'web' => [ 'driver' => 'session', 'provider' => 'users', ], 'admin' => [ 'driver' => 'session', 'provider' => 'admins', ], 'api' => [ 'driver' => 'token', 'provider' => 'users', ], ], 'providers' => [ 'users' => [ 'driver' => 'eloquent', 'model' => App\User::class, ], 'admins' => [ 'driver' => 'eloquent', 'model' => App\Admin::class, ], ], 'passwords' => [ 'users' => [ 'provider' => 'users', 'email' => 'auth.emails.password', 'table' => 'password_resets', 'expire' => 60, ], ],];

3.创建数据模型

执行php artisan make:model Admin --migration

然后修改database\migrations中的数据表结构,复制user表的就可以

执行php artisan migrate 会发现生成了admin表

4.定义后台路由

这里我直接定义了一个路由组

Route::group(['prefix' => 'admin' ,'middleware' => 'admin'], function () { Route::get('login', 'Admin\AuthController@getLogin'); Route::post('login', 'Admin\AuthController@postLogin'); Route::get('register', 'Admin\AuthController@getRegister'); Route::post('register', 'Admin\AuthController@postRegister'); Route::get('logout', 'Admin\AuthController@logout'); Route::get('/', 'Admin\AdminController@index');});

5.创建控制器

执行

php artisan make:controller Admin/AuthControllerphp artisan make:controller Admin/AdminController

AuthController.php可参照Auth中的AuthController.php

<?phpnamespace App\Http\Controllers\Admin;use App\Admin;use Validator;use App\Http\Controllers\Controller;use Illuminate\Foundation\Auth\ThrottlesLogins;use Illuminate\Foundation\Auth\AuthenticatesAndRegistersUsers;class AuthController extends Controller{ use AuthenticatesAndRegistersUsers, ThrottlesLogins; protected $redirectTo = '/admin'; protected $guard = 'admin'; protected $loginView = 'admin.login'; protected $registerView = 'admin.register'; public function __construct() { $this->middleware('guest:admin', ['except' => 'logout']); } protected function validator(array $data) { return Validator::make($data, [ 'name' => 'required|max:255', 'email' => 'required|email|max:255|unique:admins', 'password' => 'required|confirmed|min:6', ]); } protected function create(array $data) { return Admin::create([ 'name' => $data['name'], 'email' => $data['email'], 'password' => bcrypt($data['password']), ]); }}

AdminController.php

<?phpnamespace App\Http\Controllers\Admin;use Illuminate\Http\Request;use App\Http\Requests;use App\Http\Controllers\Controller;use Auth;class AdminController extends Controller{ public function __construct() { $this->middleware('auth:admin'); } public function index() { $admin = Auth::guard('admin')->user(); return view('admin.home'); }}

6.创建视图

这里直接复制auth中的视图和home.blade.php到一个新的admin文件夹中。

修改login和register中表单的action,添加上admin前缀

修改layouts中的app.blade.php

<ul class="nav navbar-nav navbar-right"> <!-- Authentication Links --> @if (Auth::guest('admin')) <li><a href="{{ url('/login') }}">Login</a></li> <li><a href="{{ url('/register') }}">Register</a></li> @else <li class="dropdown"> <a href="#" class="dropdown-toggle" data-toggle="dropdown" role="button" aria-expanded="false"> {{ Auth::guard('admin')->user()->name }} <span class="caret"></span> </a> <ul class="dropdown-menu" role="menu"> <li><a href="{{ url('/admin/logout') }}"><i class="fa fa-btn fa-sign-out"></i>Logout</a></li> </ul> </li> @endif </ul>

现在试一下前后台分别登录吧!

7.可能遇到的页面跳转问题

如果你遇到了这种情况可以尝试修改一下Middleware\Authenticate.php

return redirect()->guest($guard.'/login');

以上所述是小编给大家介绍的laravel5.2实现区分前后台用户登录的方法,希望对大家有所帮助,如果大家有任何疑问请给我留言,小编会及时回复大家的。在此也非常感谢大家对脚本之家网站的支持!

1.前台登录

直接使用laravel自带的auth

php artisan make:auth

然后可以查看路由文件:

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

执行php artisan migrate

会发现生成了两张表。

2.后台登录

编辑配置文件

config\auth.php

添加guards中的admin和providers中的admins

<?phpreturn [ 'defaults' => [ 'guard' => 'web', 'passwords' => 'users', ], 'guards' => [ 'web' => [ 'driver' => 'session', 'provider' => 'users', ], 'admin' => [ 'driver' => 'session', 'provider' => 'admins', ], 'api' => [ 'driver' => 'token', 'provider' => 'users', ], ], 'providers' => [ 'users' => [ 'driver' => 'eloquent', 'model' => App\User::class, ], 'admins' => [ 'driver' => 'eloquent', 'model' => App\Admin::class, ], ], 'passwords' => [ 'users' => [ 'provider' => 'users', 'email' => 'auth.emails.password', 'table' => 'password_resets', 'expire' => 60, ], ],];

3.创建数据模型

执行php artisan make:model Admin --migration

然后修改database\migrations中的数据表结构,复制user表的就可以

执行php artisan migrate 会发现生成了admin表

4.定义后台路由

这里我直接定义了一个路由组

Route::group(['prefix' => 'admin' ,'middleware' => 'admin'], function () { Route::get('login', 'Admin\AuthController@getLogin'); Route::post('login', 'Admin\AuthController@postLogin'); Route::get('register', 'Admin\AuthController@getRegister'); Route::post('register', 'Admin\AuthController@postRegister'); Route::get('logout', 'Admin\AuthController@logout'); Route::get('/', 'Admin\AdminController@index');});

5.创建控制器

执行

php artisan make:controller Admin/AuthControllerphp artisan make:controller Admin/AdminController

AuthController.php可参照Auth中的AuthController.php

<?phpnamespace App\Http\Controllers\Admin;use App\Admin;use Validator;use App\Http\Controllers\Controller;use Illuminate\Foundation\Auth\ThrottlesLogins;use Illuminate\Foundation\Auth\AuthenticatesAndRegistersUsers;class AuthController extends Controller{ use AuthenticatesAndRegistersUsers, ThrottlesLogins; protected $redirectTo = '/admin'; protected $guard = 'admin'; protected $loginView = 'admin.login'; protected $registerView = 'admin.register'; public function __construct() { $this->middleware('guest:admin', ['except' => 'logout']); } protected function validator(array $data) { return Validator::make($data, [ 'name' => 'required|max:255', 'email' => 'required|email|max:255|unique:admins', 'password' => 'required|confirmed|min:6', ]); } protected function create(array $data) { return Admin::create([ 'name' => $data['name'], 'email' => $data['email'], 'password' => bcrypt($data['password']), ]); }}

AdminController.php

<?phpnamespace App\Http\Controllers\Admin;use Illuminate\Http\Request;use App\Http\Requests;use App\Http\Controllers\Controller;use Auth;class AdminController extends Controller{ public function __construct() { $this->middleware('auth:admin'); } public function index() { $admin = Auth::guard('admin')->user(); return view('admin.home'); }}

6.创建视图

这里直接复制auth中的视图和home.blade.php到一个新的admin文件夹中。

修改login和register中表单的action,添加上admin前缀

修改layouts中的app.blade.php

<ul class="nav navbar-nav navbar-right"> <!-- Authentication Links --> @if (Auth::guest('admin')) <li><a href="{{ url('/login') }}">Login</a></li> <li><a href="{{ url('/register') }}">Register</a></li> @else <li class="dropdown"> <a href="#" class="dropdown-toggle" data-toggle="dropdown" role="button" aria-expanded="false"> {{ Auth::guard('admin')->user()->name }} <span class="caret"></span> </a> <ul class="dropdown-menu" role="menu"> <li><a href="{{ url('/admin/logout') }}"><i class="fa fa-btn fa-sign-out"></i>Logout</a></li> </ul> </li> @endif </ul>

现在试一下前后台分别登录吧!

7.可能遇到的页面跳转问题

如果你遇到了这种情况可以尝试修改一下Middleware\Authenticate.php

return redirect()->guest($guard.'/login');

以上所述是小编给大家介绍的laravel5.2实现区分前后台用户登录的方法,希望对大家有所帮助,如果大家有任何疑问请给我留言,小编会及时回复大家的。在此也非常感谢大家对脚本之家网站的支持!

,见图

威尼斯电脑教程

1.前台登录

直接使用laravel自带的auth

php artisan make:auth

然后可以查看路由文件:

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

执行php artisan migrate

会发现生成了两张表。

2.后台登录

编辑配置文件

config\auth.php

添加guards中的admin和providers中的admins

<?phpreturn [ 'defaults' => [ 'guard' => 'web', 'passwords' => 'users', ], 'guards' => [ 'web' => [ 'driver' => 'session', 'provider' => 'users', ], 'admin' => [ 'driver' => 'session', 'provider' => 'admins', ], 'api' => [ 'driver' => 'token', 'provider' => 'users', ], ], 'providers' => [ 'users' => [ 'driver' => 'eloquent', 'model' => App\User::class, ], 'admins' => [ 'driver' => 'eloquent', 'model' => App\Admin::class, ], ], 'passwords' => [ 'users' => [ 'provider' => 'users', 'email' => 'auth.emails.password', 'table' => 'password_resets', 'expire' => 60, ], ],];

3.创建数据模型

执行php artisan make:model Admin --migration

然后修改database\migrations中的数据表结构,复制user表的就可以

执行php artisan migrate 会发现生成了admin表

4.定义后台路由

这里我直接定义了一个路由组

Route::group(['prefix' => 'admin' ,'middleware' => 'admin'], function () { Route::get('login', 'Admin\AuthController@getLogin'); Route::post('login', 'Admin\AuthController@postLogin'); Route::get('register', 'Admin\AuthController@getRegister'); Route::post('register', 'Admin\AuthController@postRegister'); Route::get('logout', 'Admin\AuthController@logout'); Route::get('/', 'Admin\AdminController@index');});

5.创建控制器

执行

php artisan make:controller Admin/AuthControllerphp artisan make:controller Admin/AdminController

AuthController.php可参照Auth中的AuthController.php

<?phpnamespace App\Http\Controllers\Admin;use App\Admin;use Validator;use App\Http\Controllers\Controller;use Illuminate\Foundation\Auth\ThrottlesLogins;use Illuminate\Foundation\Auth\AuthenticatesAndRegistersUsers;class AuthController extends Controller{ use AuthenticatesAndRegistersUsers, ThrottlesLogins; protected $redirectTo = '/admin'; protected $guard = 'admin'; protected $loginView = 'admin.login'; protected $registerView = 'admin.register'; public function __construct() { $this->middleware('guest:admin', ['except' => 'logout']); } protected function validator(array $data) { return Validator::make($data, [ 'name' => 'required|max:255', 'email' => 'required|email|max:255|unique:admins', 'password' => 'required|confirmed|min:6', ]); } protected function create(array $data) { return Admin::create([ 'name' => $data['name'], 'email' => $data['email'], 'password' => bcrypt($data['password']), ]); }}

AdminController.php

<?phpnamespace App\Http\Controllers\Admin;use Illuminate\Http\Request;use App\Http\Requests;use App\Http\Controllers\Controller;use Auth;class AdminController extends Controller{ public function __construct() { $this->middleware('auth:admin'); } public function index() { $admin = Auth::guard('admin')->user(); return view('admin.home'); }}

6.创建视图

这里直接复制auth中的视图和home.blade.php到一个新的admin文件夹中。

修改login和register中表单的action,添加上admin前缀

修改layouts中的app.blade.php

<ul class="nav navbar-nav navbar-right"> <!-- Authentication Links --> @if (Auth::guest('admin')) <li><a href="{{ url('/login') }}">Login</a></li> <li><a href="{{ url('/register') }}">Register</a></li> @else <li class="dropdown"> <a href="#" class="dropdown-toggle" data-toggle="dropdown" role="button" aria-expanded="false"> {{ Auth::guard('admin')->user()->name }} <span class="caret"></span> </a> <ul class="dropdown-menu" role="menu"> <li><a href="{{ url('/admin/logout') }}"><i class="fa fa-btn fa-sign-out"></i>Logout</a></li> </ul> </li> @endif </ul>

现在试一下前后台分别登录吧!

7.可能遇到的页面跳转问题

如果你遇到了这种情况可以尝试修改一下Middleware\Authenticate.php

return redirect()->guest($guard.'/login');

以上所述是小编给大家介绍的laravel5.2实现区分前后台用户登录的方法,希望对大家有所帮助,如果大家有任何疑问请给我留言,小编会及时回复大家的。在此也非常感谢大家对脚本之家网站的支持!

laravel5.2实现区分前后台用户登录的方法

1.前台登录

直接使用laravel自带的auth

php artisan make:auth

然后可以查看路由文件:

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

执行php artisan migrate

会发现生成了两张表。

2.后台登录

编辑配置文件

config\auth.php

添加guards中的admin和providers中的admins

<?phpreturn [ 'defaults' => [ 'guard' => 'web', 'passwords' => 'users', ], 'guards' => [ 'web' => [ 'driver' => 'session', 'provider' => 'users', ], 'admin' => [ 'driver' => 'session', 'provider' => 'admins', ], 'api' => [ 'driver' => 'token', 'provider' => 'users', ], ], 'providers' => [ 'users' => [ 'driver' => 'eloquent', 'model' => App\User::class, ], 'admins' => [ 'driver' => 'eloquent', 'model' => App\Admin::class, ], ], 'passwords' => [ 'users' => [ 'provider' => 'users', 'email' => 'auth.emails.password', 'table' => 'password_resets', 'expire' => 60, ], ],];

3.创建数据模型

执行php artisan make:model Admin --migration

然后修改database\migrations中的数据表结构,复制user表的就可以

执行php artisan migrate 会发现生成了admin表

4.定义后台路由

这里我直接定义了一个路由组

Route::group(['prefix' => 'admin' ,'middleware' => 'admin'], function () { Route::get('login', 'Admin\AuthController@getLogin'); Route::post('login', 'Admin\AuthController@postLogin'); Route::get('register', 'Admin\AuthController@getRegister'); Route::post('register', 'Admin\AuthController@postRegister'); Route::get('logout', 'Admin\AuthController@logout'); Route::get('/', 'Admin\AdminController@index');});

5.创建控制器

执行

php artisan make:controller Admin/AuthControllerphp artisan make:controller Admin/AdminController

AuthController.php可参照Auth中的AuthController.php

<?phpnamespace App\Http\Controllers\Admin;use App\Admin;use Validator;use App\Http\Controllers\Controller;use Illuminate\Foundation\Auth\ThrottlesLogins;use Illuminate\Foundation\Auth\AuthenticatesAndRegistersUsers;class AuthController extends Controller{ use AuthenticatesAndRegistersUsers, ThrottlesLogins; protected $redirectTo = '/admin'; protected $guard = 'admin'; protected $loginView = 'admin.login'; protected $registerView = 'admin.register'; public function __construct() { $this->middleware('guest:admin', ['except' => 'logout']); } protected function validator(array $data) { return Validator::make($data, [ 'name' => 'required|max:255', 'email' => 'required|email|max:255|unique:admins', 'password' => 'required|confirmed|min:6', ]); } protected function create(array $data) { return Admin::create([ 'name' => $data['name'], 'email' => $data['email'], 'password' => bcrypt($data['password']), ]); }}

AdminController.php

<?phpnamespace App\Http\Controllers\Admin;use Illuminate\Http\Request;use App\Http\Requests;use App\Http\Controllers\Controller;use Auth;class AdminController extends Controller{ public function __construct() { $this->middleware('auth:admin'); } public function index() { $admin = Auth::guard('admin')->user(); return view('admin.home'); }}

6.创建视图

这里直接复制auth中的视图和home.blade.php到一个新的admin文件夹中。

修改login和register中表单的action,添加上admin前缀

修改layouts中的app.blade.php

<ul class="nav navbar-nav navbar-right"> <!-- Authentication Links --> @if (Auth::guest('admin')) <li><a href="{{ url('/login') }}">Login</a></li> <li><a href="{{ url('/register') }}">Register</a></li> @else <li class="dropdown"> <a href="#" class="dropdown-toggle" data-toggle="dropdown" role="button" aria-expanded="false"> {{ Auth::guard('admin')->user()->name }} <span class="caret"></span> </a> <ul class="dropdown-menu" role="menu"> <li><a href="{{ url('/admin/logout') }}"><i class="fa fa-btn fa-sign-out"></i>Logout</a></li> </ul> </li> @endif </ul>

现在试一下前后台分别登录吧!

7.可能遇到的页面跳转问题

如果你遇到了这种情况可以尝试修改一下Middleware\Authenticate.php

return redirect()->guest($guard.'/login');

以上所述是小编给大家介绍的laravel5.2实现区分前后台用户登录的方法,希望对大家有所帮助,如果大家有任何疑问请给我留言,小编会及时回复大家的。在此也非常感谢大家对脚本之家网站的支持!

1.前台登录

直接使用laravel自带的auth

php artisan make:auth

然后可以查看路由文件:

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

执行php artisan migrate

会发现生成了两张表。

2.后台登录

编辑配置文件

config\auth.php

添加guards中的admin和providers中的admins

<?phpreturn [ 'defaults' => [ 'guard' => 'web', 'passwords' => 'users', ], 'guards' => [ 'web' => [ 'driver' => 'session', 'provider' => 'users', ], 'admin' => [ 'driver' => 'session', 'provider' => 'admins', ], 'api' => [ 'driver' => 'token', 'provider' => 'users', ], ], 'providers' => [ 'users' => [ 'driver' => 'eloquent', 'model' => App\User::class, ], 'admins' => [ 'driver' => 'eloquent', 'model' => App\Admin::class, ], ], 'passwords' => [ 'users' => [ 'provider' => 'users', 'email' => 'auth.emails.password', 'table' => 'password_resets', 'expire' => 60, ], ],];

3.创建数据模型

执行php artisan make:model Admin --migration

然后修改database\migrations中的数据表结构,复制user表的就可以

执行php artisan migrate 会发现生成了admin表

4.定义后台路由

这里我直接定义了一个路由组

Route::group(['prefix' => 'admin' ,'middleware' => 'admin'], function () { Route::get('login', 'Admin\AuthController@getLogin'); Route::post('login', 'Admin\AuthController@postLogin'); Route::get('register', 'Admin\AuthController@getRegister'); Route::post('register', 'Admin\AuthController@postRegister'); Route::get('logout', 'Admin\AuthController@logout'); Route::get('/', 'Admin\AdminController@index');});

5.创建控制器

执行

php artisan make:controller Admin/AuthControllerphp artisan make:controller Admin/AdminController

AuthController.php可参照Auth中的AuthController.php

<?phpnamespace App\Http\Controllers\Admin;use App\Admin;use Validator;use App\Http\Controllers\Controller;use Illuminate\Foundation\Auth\ThrottlesLogins;use Illuminate\Foundation\Auth\AuthenticatesAndRegistersUsers;class AuthController extends Controller{ use AuthenticatesAndRegistersUsers, ThrottlesLogins; protected $redirectTo = '/admin'; protected $guard = 'admin'; protected $loginView = 'admin.login'; protected $registerView = 'admin.register'; public function __construct() { $this->middleware('guest:admin', ['except' => 'logout']); } protected function validator(array $data) { return Validator::make($data, [ 'name' => 'required|max:255', 'email' => 'required|email|max:255|unique:admins', 'password' => 'required|confirmed|min:6', ]); } protected function create(array $data) { return Admin::create([ 'name' => $data['name'], 'email' => $data['email'], 'password' => bcrypt($data['password']), ]); }}

AdminController.php

<?phpnamespace App\Http\Controllers\Admin;use Illuminate\Http\Request;use App\Http\Requests;use App\Http\Controllers\Controller;use Auth;class AdminController extends Controller{ public function __construct() { $this->middleware('auth:admin'); } public function index() { $admin = Auth::guard('admin')->user(); return view('admin.home'); }}

6.创建视图

这里直接复制auth中的视图和home.blade.php到一个新的admin文件夹中。

修改login和register中表单的action,添加上admin前缀

修改layouts中的app.blade.php

<ul class="nav navbar-nav navbar-right"> <!-- Authentication Links --> @if (Auth::guest('admin')) <li><a href="{{ url('/login') }}">Login</a></li> <li><a href="{{ url('/register') }}">Register</a></li> @else <li class="dropdown"> <a href="#" class="dropdown-toggle" data-toggle="dropdown" role="button" aria-expanded="false"> {{ Auth::guard('admin')->user()->name }} <span class="caret"></span> </a> <ul class="dropdown-menu" role="menu"> <li><a href="{{ url('/admin/logout') }}"><i class="fa fa-btn fa-sign-out"></i>Logout</a></li> </ul> </li> @endif </ul>

现在试一下前后台分别登录吧!

7.可能遇到的页面跳转问题

如果你遇到了这种情况可以尝试修改一下Middleware\Authenticate.php

return redirect()->guest($guard.'/login');

以上所述是小编给大家介绍的laravel5.2实现区分前后台用户登录的方法,希望对大家有所帮助,如果大家有任何疑问请给我留言,小编会及时回复大家的。在此也非常感谢大家对脚本之家网站的支持!

laravel5.2实现区分前后台用户登录的方法laravel5.2实现区分前后台用户登录的方法laravel5.2实现区分前后台用户登录的方法laravel5.2实现区分前后台用户登录的方法laravel5.2实现区分前后台用户登录的方法

1.前台登录

直接使用laravel自带的auth

php artisan make:auth

然后可以查看路由文件:

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

执行php artisan migrate

会发现生成了两张表。

2.后台登录

编辑配置文件

config\auth.php

添加guards中的admin和providers中的admins

<?phpreturn [ 'defaults' => [ 'guard' => 'web', 'passwords' => 'users', ], 'guards' => [ 'web' => [ 'driver' => 'session', 'provider' => 'users', ], 'admin' => [ 'driver' => 'session', 'provider' => 'admins', ], 'api' => [ 'driver' => 'token', 'provider' => 'users', ], ], 'providers' => [ 'users' => [ 'driver' => 'eloquent', 'model' => App\User::class, ], 'admins' => [ 'driver' => 'eloquent', 'model' => App\Admin::class, ], ], 'passwords' => [ 'users' => [ 'provider' => 'users', 'email' => 'auth.emails.password', 'table' => 'password_resets', 'expire' => 60, ], ],];

3.创建数据模型

执行php artisan make:model Admin --migration

然后修改database\migrations中的数据表结构,复制user表的就可以

执行php artisan migrate 会发现生成了admin表

4.定义后台路由

这里我直接定义了一个路由组

Route::group(['prefix' => 'admin' ,'middleware' => 'admin'], function () { Route::get('login', 'Admin\AuthController@getLogin'); Route::post('login', 'Admin\AuthController@postLogin'); Route::get('register', 'Admin\AuthController@getRegister'); Route::post('register', 'Admin\AuthController@postRegister'); Route::get('logout', 'Admin\AuthController@logout'); Route::get('/', 'Admin\AdminController@index');});

5.创建控制器

执行

php artisan make:controller Admin/AuthControllerphp artisan make:controller Admin/AdminController

AuthController.php可参照Auth中的AuthController.php

<?phpnamespace App\Http\Controllers\Admin;use App\Admin;use Validator;use App\Http\Controllers\Controller;use Illuminate\Foundation\Auth\ThrottlesLogins;use Illuminate\Foundation\Auth\AuthenticatesAndRegistersUsers;class AuthController extends Controller{ use AuthenticatesAndRegistersUsers, ThrottlesLogins; protected $redirectTo = '/admin'; protected $guard = 'admin'; protected $loginView = 'admin.login'; protected $registerView = 'admin.register'; public function __construct() { $this->middleware('guest:admin', ['except' => 'logout']); } protected function validator(array $data) { return Validator::make($data, [ 'name' => 'required|max:255', 'email' => 'required|email|max:255|unique:admins', 'password' => 'required|confirmed|min:6', ]); } protected function create(array $data) { return Admin::create([ 'name' => $data['name'], 'email' => $data['email'], 'password' => bcrypt($data['password']), ]); }}

AdminController.php

<?phpnamespace App\Http\Controllers\Admin;use Illuminate\Http\Request;use App\Http\Requests;use App\Http\Controllers\Controller;use Auth;class AdminController extends Controller{ public function __construct() { $this->middleware('auth:admin'); } public function index() { $admin = Auth::guard('admin')->user(); return view('admin.home'); }}

6.创建视图

这里直接复制auth中的视图和home.blade.php到一个新的admin文件夹中。

修改login和register中表单的action,添加上admin前缀

修改layouts中的app.blade.php

<ul class="nav navbar-nav navbar-right"> <!-- Authentication Links --> @if (Auth::guest('admin')) <li><a href="{{ url('/login') }}">Login</a></li> <li><a href="{{ url('/register') }}">Register</a></li> @else <li class="dropdown"> <a href="#" class="dropdown-toggle" data-toggle="dropdown" role="button" aria-expanded="false"> {{ Auth::guard('admin')->user()->name }} <span class="caret"></span> </a> <ul class="dropdown-menu" role="menu"> <li><a href="{{ url('/admin/logout') }}"><i class="fa fa-btn fa-sign-out"></i>Logout</a></li> </ul> </li> @endif </ul>

现在试一下前后台分别登录吧!

7.可能遇到的页面跳转问题

如果你遇到了这种情况可以尝试修改一下Middleware\Authenticate.php

return redirect()->guest($guard.'/login');

以上所述是小编给大家介绍的laravel5.2实现区分前后台用户登录的方法,希望对大家有所帮助,如果大家有任何疑问请给我留言,小编会及时回复大家的。在此也非常感谢大家对脚本之家网站的支持!

laravel5.2实现区分前后台用户登录的方法

laravel5.2实现区分前后台用户登录的方法

1.前台登录

直接使用laravel自带的auth

php artisan make:auth

然后可以查看路由文件:

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

执行php artisan migrate

会发现生成了两张表。

2.后台登录

编辑配置文件

config\auth.php

添加guards中的admin和providers中的admins

<?phpreturn [ 'defaults' => [ 'guard' => 'web', 'passwords' => 'users', ], 'guards' => [ 'web' => [ 'driver' => 'session', 'provider' => 'users', ], 'admin' => [ 'driver' => 'session', 'provider' => 'admins', ], 'api' => [ 'driver' => 'token', 'provider' => 'users', ], ], 'providers' => [ 'users' => [ 'driver' => 'eloquent', 'model' => App\User::class, ], 'admins' => [ 'driver' => 'eloquent', 'model' => App\Admin::class, ], ], 'passwords' => [ 'users' => [ 'provider' => 'users', 'email' => 'auth.emails.password', 'table' => 'password_resets', 'expire' => 60, ], ],];

3.创建数据模型

执行php artisan make:model Admin --migration

然后修改database\migrations中的数据表结构,复制user表的就可以

执行php artisan migrate 会发现生成了admin表

4.定义后台路由

这里我直接定义了一个路由组

Route::group(['prefix' => 'admin' ,'middleware' => 'admin'], function () { Route::get('login', 'Admin\AuthController@getLogin'); Route::post('login', 'Admin\AuthController@postLogin'); Route::get('register', 'Admin\AuthController@getRegister'); Route::post('register', 'Admin\AuthController@postRegister'); Route::get('logout', 'Admin\AuthController@logout'); Route::get('/', 'Admin\AdminController@index');});

5.创建控制器

执行

php artisan make:controller Admin/AuthControllerphp artisan make:controller Admin/AdminController

AuthController.php可参照Auth中的AuthController.php

<?phpnamespace App\Http\Controllers\Admin;use App\Admin;use Validator;use App\Http\Controllers\Controller;use Illuminate\Foundation\Auth\ThrottlesLogins;use Illuminate\Foundation\Auth\AuthenticatesAndRegistersUsers;class AuthController extends Controller{ use AuthenticatesAndRegistersUsers, ThrottlesLogins; protected $redirectTo = '/admin'; protected $guard = 'admin'; protected $loginView = 'admin.login'; protected $registerView = 'admin.register'; public function __construct() { $this->middleware('guest:admin', ['except' => 'logout']); } protected function validator(array $data) { return Validator::make($data, [ 'name' => 'required|max:255', 'email' => 'required|email|max:255|unique:admins', 'password' => 'required|confirmed|min:6', ]); } protected function create(array $data) { return Admin::create([ 'name' => $data['name'], 'email' => $data['email'], 'password' => bcrypt($data['password']), ]); }}

AdminController.php

<?phpnamespace App\Http\Controllers\Admin;use Illuminate\Http\Request;use App\Http\Requests;use App\Http\Controllers\Controller;use Auth;class AdminController extends Controller{ public function __construct() { $this->middleware('auth:admin'); } public function index() { $admin = Auth::guard('admin')->user(); return view('admin.home'); }}

6.创建视图

这里直接复制auth中的视图和home.blade.php到一个新的admin文件夹中。

修改login和register中表单的action,添加上admin前缀

修改layouts中的app.blade.php

<ul class="nav navbar-nav navbar-right"> <!-- Authentication Links --> @if (Auth::guest('admin')) <li><a href="{{ url('/login') }}">Login</a></li> <li><a href="{{ url('/register') }}">Register</a></li> @else <li class="dropdown"> <a href="#" class="dropdown-toggle" data-toggle="dropdown" role="button" aria-expanded="false"> {{ Auth::guard('admin')->user()->name }} <span class="caret"></span> </a> <ul class="dropdown-menu" role="menu"> <li><a href="{{ url('/admin/logout') }}"><i class="fa fa-btn fa-sign-out"></i>Logout</a></li> </ul> </li> @endif </ul>

现在试一下前后台分别登录吧!

7.可能遇到的页面跳转问题

如果你遇到了这种情况可以尝试修改一下Middleware\Authenticate.php

return redirect()->guest($guard.'/login');

以上所述是小编给大家介绍的laravel5.2实现区分前后台用户登录的方法,希望对大家有所帮助,如果大家有任何疑问请给我留言,小编会及时回复大家的。在此也非常感谢大家对脚本之家网站的支持!

laravel5.2实现区分前后台用户登录的方法laravel5.2实现区分前后台用户登录的方法

1.前台登录

直接使用laravel自带的auth

php artisan make:auth

然后可以查看路由文件:

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

执行php artisan migrate

会发现生成了两张表。

2.后台登录

编辑配置文件

config\auth.php

添加guards中的admin和providers中的admins

<?phpreturn [ 'defaults' => [ 'guard' => 'web', 'passwords' => 'users', ], 'guards' => [ 'web' => [ 'driver' => 'session', 'provider' => 'users', ], 'admin' => [ 'driver' => 'session', 'provider' => 'admins', ], 'api' => [ 'driver' => 'token', 'provider' => 'users', ], ], 'providers' => [ 'users' => [ 'driver' => 'eloquent', 'model' => App\User::class, ], 'admins' => [ 'driver' => 'eloquent', 'model' => App\Admin::class, ], ], 'passwords' => [ 'users' => [ 'provider' => 'users', 'email' => 'auth.emails.password', 'table' => 'password_resets', 'expire' => 60, ], ],];

3.创建数据模型

执行php artisan make:model Admin --migration

然后修改database\migrations中的数据表结构,复制user表的就可以

执行php artisan migrate 会发现生成了admin表

4.定义后台路由

这里我直接定义了一个路由组

Route::group(['prefix' => 'admin' ,'middleware' => 'admin'], function () { Route::get('login', 'Admin\AuthController@getLogin'); Route::post('login', 'Admin\AuthController@postLogin'); Route::get('register', 'Admin\AuthController@getRegister'); Route::post('register', 'Admin\AuthController@postRegister'); Route::get('logout', 'Admin\AuthController@logout'); Route::get('/', 'Admin\AdminController@index');});

5.创建控制器

执行

php artisan make:controller Admin/AuthControllerphp artisan make:controller Admin/AdminController

AuthController.php可参照Auth中的AuthController.php

<?phpnamespace App\Http\Controllers\Admin;use App\Admin;use Validator;use App\Http\Controllers\Controller;use Illuminate\Foundation\Auth\ThrottlesLogins;use Illuminate\Foundation\Auth\AuthenticatesAndRegistersUsers;class AuthController extends Controller{ use AuthenticatesAndRegistersUsers, ThrottlesLogins; protected $redirectTo = '/admin'; protected $guard = 'admin'; protected $loginView = 'admin.login'; protected $registerView = 'admin.register'; public function __construct() { $this->middleware('guest:admin', ['except' => 'logout']); } protected function validator(array $data) { return Validator::make($data, [ 'name' => 'required|max:255', 'email' => 'required|email|max:255|unique:admins', 'password' => 'required|confirmed|min:6', ]); } protected function create(array $data) { return Admin::create([ 'name' => $data['name'], 'email' => $data['email'], 'password' => bcrypt($data['password']), ]); }}

AdminController.php

<?phpnamespace App\Http\Controllers\Admin;use Illuminate\Http\Request;use App\Http\Requests;use App\Http\Controllers\Controller;use Auth;class AdminController extends Controller{ public function __construct() { $this->middleware('auth:admin'); } public function index() { $admin = Auth::guard('admin')->user(); return view('admin.home'); }}

6.创建视图

这里直接复制auth中的视图和home.blade.php到一个新的admin文件夹中。

修改login和register中表单的action,添加上admin前缀

修改layouts中的app.blade.php

<ul class="nav navbar-nav navbar-right"> <!-- Authentication Links --> @if (Auth::guest('admin')) <li><a href="{{ url('/login') }}">Login</a></li> <li><a href="{{ url('/register') }}">Register</a></li> @else <li class="dropdown"> <a href="#" class="dropdown-toggle" data-toggle="dropdown" role="button" aria-expanded="false"> {{ Auth::guard('admin')->user()->name }} <span class="caret"></span> </a> <ul class="dropdown-menu" role="menu"> <li><a href="{{ url('/admin/logout') }}"><i class="fa fa-btn fa-sign-out"></i>Logout</a></li> </ul> </li> @endif </ul>

现在试一下前后台分别登录吧!

7.可能遇到的页面跳转问题

如果你遇到了这种情况可以尝试修改一下Middleware\Authenticate.php

return redirect()->guest($guard.'/login');

以上所述是小编给大家介绍的laravel5.2实现区分前后台用户登录的方法,希望对大家有所帮助,如果大家有任何疑问请给我留言,小编会及时回复大家的。在此也非常感谢大家对脚本之家网站的支持!

laravel5.2实现区分前后台用户登录的方法

1.前台登录

直接使用laravel自带的auth

php artisan make:auth

然后可以查看路由文件:

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

执行php artisan migrate

会发现生成了两张表。

2.后台登录

编辑配置文件

config\auth.php

添加guards中的admin和providers中的admins

<?phpreturn [ 'defaults' => [ 'guard' => 'web', 'passwords' => 'users', ], 'guards' => [ 'web' => [ 'driver' => 'session', 'provider' => 'users', ], 'admin' => [ 'driver' => 'session', 'provider' => 'admins', ], 'api' => [ 'driver' => 'token', 'provider' => 'users', ], ], 'providers' => [ 'users' => [ 'driver' => 'eloquent', 'model' => App\User::class, ], 'admins' => [ 'driver' => 'eloquent', 'model' => App\Admin::class, ], ], 'passwords' => [ 'users' => [ 'provider' => 'users', 'email' => 'auth.emails.password', 'table' => 'password_resets', 'expire' => 60, ], ],];

3.创建数据模型

执行php artisan make:model Admin --migration

然后修改database\migrations中的数据表结构,复制user表的就可以

执行php artisan migrate 会发现生成了admin表

4.定义后台路由

这里我直接定义了一个路由组

Route::group(['prefix' => 'admin' ,'middleware' => 'admin'], function () { Route::get('login', 'Admin\AuthController@getLogin'); Route::post('login', 'Admin\AuthController@postLogin'); Route::get('register', 'Admin\AuthController@getRegister'); Route::post('register', 'Admin\AuthController@postRegister'); Route::get('logout', 'Admin\AuthController@logout'); Route::get('/', 'Admin\AdminController@index');});

5.创建控制器

执行

php artisan make:controller Admin/AuthControllerphp artisan make:controller Admin/AdminController

AuthController.php可参照Auth中的AuthController.php

<?phpnamespace App\Http\Controllers\Admin;use App\Admin;use Validator;use App\Http\Controllers\Controller;use Illuminate\Foundation\Auth\ThrottlesLogins;use Illuminate\Foundation\Auth\AuthenticatesAndRegistersUsers;class AuthController extends Controller{ use AuthenticatesAndRegistersUsers, ThrottlesLogins; protected $redirectTo = '/admin'; protected $guard = 'admin'; protected $loginView = 'admin.login'; protected $registerView = 'admin.register'; public function __construct() { $this->middleware('guest:admin', ['except' => 'logout']); } protected function validator(array $data) { return Validator::make($data, [ 'name' => 'required|max:255', 'email' => 'required|email|max:255|unique:admins', 'password' => 'required|confirmed|min:6', ]); } protected function create(array $data) { return Admin::create([ 'name' => $data['name'], 'email' => $data['email'], 'password' => bcrypt($data['password']), ]); }}

AdminController.php

<?phpnamespace App\Http\Controllers\Admin;use Illuminate\Http\Request;use App\Http\Requests;use App\Http\Controllers\Controller;use Auth;class AdminController extends Controller{ public function __construct() { $this->middleware('auth:admin'); } public function index() { $admin = Auth::guard('admin')->user(); return view('admin.home'); }}

6.创建视图

这里直接复制auth中的视图和home.blade.php到一个新的admin文件夹中。

修改login和register中表单的action,添加上admin前缀

修改layouts中的app.blade.php

<ul class="nav navbar-nav navbar-right"> <!-- Authentication Links --> @if (Auth::guest('admin')) <li><a href="{{ url('/login') }}">Login</a></li> <li><a href="{{ url('/register') }}">Register</a></li> @else <li class="dropdown"> <a href="#" class="dropdown-toggle" data-toggle="dropdown" role="button" aria-expanded="false"> {{ Auth::guard('admin')->user()->name }} <span class="caret"></span> </a> <ul class="dropdown-menu" role="menu"> <li><a href="{{ url('/admin/logout') }}"><i class="fa fa-btn fa-sign-out"></i>Logout</a></li> </ul> </li> @endif </ul>

现在试一下前后台分别登录吧!

7.可能遇到的页面跳转问题

如果你遇到了这种情况可以尝试修改一下Middleware\Authenticate.php

return redirect()->guest($guard.'/login');

以上所述是小编给大家介绍的laravel5.2实现区分前后台用户登录的方法,希望对大家有所帮助,如果大家有任何疑问请给我留言,小编会及时回复大家的。在此也非常感谢大家对脚本之家网站的支持!

1.前台登录

直接使用laravel自带的auth

php artisan make:auth

然后可以查看路由文件:

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

执行php artisan migrate

会发现生成了两张表。

2.后台登录

编辑配置文件

config\auth.php

添加guards中的admin和providers中的admins

<?phpreturn [ 'defaults' => [ 'guard' => 'web', 'passwords' => 'users', ], 'guards' => [ 'web' => [ 'driver' => 'session', 'provider' => 'users', ], 'admin' => [ 'driver' => 'session', 'provider' => 'admins', ], 'api' => [ 'driver' => 'token', 'provider' => 'users', ], ], 'providers' => [ 'users' => [ 'driver' => 'eloquent', 'model' => App\User::class, ], 'admins' => [ 'driver' => 'eloquent', 'model' => App\Admin::class, ], ], 'passwords' => [ 'users' => [ 'provider' => 'users', 'email' => 'auth.emails.password', 'table' => 'password_resets', 'expire' => 60, ], ],];

3.创建数据模型

执行php artisan make:model Admin --migration

然后修改database\migrations中的数据表结构,复制user表的就可以

执行php artisan migrate 会发现生成了admin表

4.定义后台路由

这里我直接定义了一个路由组

Route::group(['prefix' => 'admin' ,'middleware' => 'admin'], function () { Route::get('login', 'Admin\AuthController@getLogin'); Route::post('login', 'Admin\AuthController@postLogin'); Route::get('register', 'Admin\AuthController@getRegister'); Route::post('register', 'Admin\AuthController@postRegister'); Route::get('logout', 'Admin\AuthController@logout'); Route::get('/', 'Admin\AdminController@index');});

5.创建控制器

执行

php artisan make:controller Admin/AuthControllerphp artisan make:controller Admin/AdminController

AuthController.php可参照Auth中的AuthController.php

<?phpnamespace App\Http\Controllers\Admin;use App\Admin;use Validator;use App\Http\Controllers\Controller;use Illuminate\Foundation\Auth\ThrottlesLogins;use Illuminate\Foundation\Auth\AuthenticatesAndRegistersUsers;class AuthController extends Controller{ use AuthenticatesAndRegistersUsers, ThrottlesLogins; protected $redirectTo = '/admin'; protected $guard = 'admin'; protected $loginView = 'admin.login'; protected $registerView = 'admin.register'; public function __construct() { $this->middleware('guest:admin', ['except' => 'logout']); } protected function validator(array $data) { return Validator::make($data, [ 'name' => 'required|max:255', 'email' => 'required|email|max:255|unique:admins', 'password' => 'required|confirmed|min:6', ]); } protected function create(array $data) { return Admin::create([ 'name' => $data['name'], 'email' => $data['email'], 'password' => bcrypt($data['password']), ]); }}

AdminController.php

<?phpnamespace App\Http\Controllers\Admin;use Illuminate\Http\Request;use App\Http\Requests;use App\Http\Controllers\Controller;use Auth;class AdminController extends Controller{ public function __construct() { $this->middleware('auth:admin'); } public function index() { $admin = Auth::guard('admin')->user(); return view('admin.home'); }}

6.创建视图

这里直接复制auth中的视图和home.blade.php到一个新的admin文件夹中。

修改login和register中表单的action,添加上admin前缀

修改layouts中的app.blade.php

<ul class="nav navbar-nav navbar-right"> <!-- Authentication Links --> @if (Auth::guest('admin')) <li><a href="{{ url('/login') }}">Login</a></li> <li><a href="{{ url('/register') }}">Register</a></li> @else <li class="dropdown"> <a href="#" class="dropdown-toggle" data-toggle="dropdown" role="button" aria-expanded="false"> {{ Auth::guard('admin')->user()->name }} <span class="caret"></span> </a> <ul class="dropdown-menu" role="menu"> <li><a href="{{ url('/admin/logout') }}"><i class="fa fa-btn fa-sign-out"></i>Logout</a></li> </ul> </li> @endif </ul>

现在试一下前后台分别登录吧!

7.可能遇到的页面跳转问题

如果你遇到了这种情况可以尝试修改一下Middleware\Authenticate.php

return redirect()->guest($guard.'/login');

以上所述是小编给大家介绍的laravel5.2实现区分前后台用户登录的方法,希望对大家有所帮助,如果大家有任何疑问请给我留言,小编会及时回复大家的。在此也非常感谢大家对脚本之家网站的支持!

laravel5.2实现区分前后台用户登录的方法

1.前台登录

直接使用laravel自带的auth

php artisan make:auth

然后可以查看路由文件:

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

执行php artisan migrate

会发现生成了两张表。

2.后台登录

编辑配置文件

config\auth.php

添加guards中的admin和providers中的admins

<?phpreturn [ 'defaults' => [ 'guard' => 'web', 'passwords' => 'users', ], 'guards' => [ 'web' => [ 'driver' => 'session', 'provider' => 'users', ], 'admin' => [ 'driver' => 'session', 'provider' => 'admins', ], 'api' => [ 'driver' => 'token', 'provider' => 'users', ], ], 'providers' => [ 'users' => [ 'driver' => 'eloquent', 'model' => App\User::class, ], 'admins' => [ 'driver' => 'eloquent', 'model' => App\Admin::class, ], ], 'passwords' => [ 'users' => [ 'provider' => 'users', 'email' => 'auth.emails.password', 'table' => 'password_resets', 'expire' => 60, ], ],];

3.创建数据模型

执行php artisan make:model Admin --migration

然后修改database\migrations中的数据表结构,复制user表的就可以

执行php artisan migrate 会发现生成了admin表

4.定义后台路由

这里我直接定义了一个路由组

Route::group(['prefix' => 'admin' ,'middleware' => 'admin'], function () { Route::get('login', 'Admin\AuthController@getLogin'); Route::post('login', 'Admin\AuthController@postLogin'); Route::get('register', 'Admin\AuthController@getRegister'); Route::post('register', 'Admin\AuthController@postRegister'); Route::get('logout', 'Admin\AuthController@logout'); Route::get('/', 'Admin\AdminController@index');});

5.创建控制器

执行

php artisan make:controller Admin/AuthControllerphp artisan make:controller Admin/AdminController

AuthController.php可参照Auth中的AuthController.php

<?phpnamespace App\Http\Controllers\Admin;use App\Admin;use Validator;use App\Http\Controllers\Controller;use Illuminate\Foundation\Auth\ThrottlesLogins;use Illuminate\Foundation\Auth\AuthenticatesAndRegistersUsers;class AuthController extends Controller{ use AuthenticatesAndRegistersUsers, ThrottlesLogins; protected $redirectTo = '/admin'; protected $guard = 'admin'; protected $loginView = 'admin.login'; protected $registerView = 'admin.register'; public function __construct() { $this->middleware('guest:admin', ['except' => 'logout']); } protected function validator(array $data) { return Validator::make($data, [ 'name' => 'required|max:255', 'email' => 'required|email|max:255|unique:admins', 'password' => 'required|confirmed|min:6', ]); } protected function create(array $data) { return Admin::create([ 'name' => $data['name'], 'email' => $data['email'], 'password' => bcrypt($data['password']), ]); }}

AdminController.php

<?phpnamespace App\Http\Controllers\Admin;use Illuminate\Http\Request;use App\Http\Requests;use App\Http\Controllers\Controller;use Auth;class AdminController extends Controller{ public function __construct() { $this->middleware('auth:admin'); } public function index() { $admin = Auth::guard('admin')->user(); return view('admin.home'); }}

6.创建视图

这里直接复制auth中的视图和home.blade.php到一个新的admin文件夹中。

修改login和register中表单的action,添加上admin前缀

修改layouts中的app.blade.php

<ul class="nav navbar-nav navbar-right"> <!-- Authentication Links --> @if (Auth::guest('admin')) <li><a href="{{ url('/login') }}">Login</a></li> <li><a href="{{ url('/register') }}">Register</a></li> @else <li class="dropdown"> <a href="#" class="dropdown-toggle" data-toggle="dropdown" role="button" aria-expanded="false"> {{ Auth::guard('admin')->user()->name }} <span class="caret"></span> </a> <ul class="dropdown-menu" role="menu"> <li><a href="{{ url('/admin/logout') }}"><i class="fa fa-btn fa-sign-out"></i>Logout</a></li> </ul> </li> @endif </ul>

现在试一下前后台分别登录吧!

7.可能遇到的页面跳转问题

如果你遇到了这种情况可以尝试修改一下Middleware\Authenticate.php

return redirect()->guest($guard.'/login');

以上所述是小编给大家介绍的laravel5.2实现区分前后台用户登录的方法,希望对大家有所帮助,如果大家有任何疑问请给我留言,小编会及时回复大家的。在此也非常感谢大家对脚本之家网站的支持!

laravel5.2实现区分前后台用户登录的方法

1.前台登录

直接使用laravel自带的auth

php artisan make:auth

然后可以查看路由文件:

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

执行php artisan migrate

会发现生成了两张表。

2.后台登录

编辑配置文件

config\auth.php

添加guards中的admin和providers中的admins

<?phpreturn [ 'defaults' => [ 'guard' => 'web', 'passwords' => 'users', ], 'guards' => [ 'web' => [ 'driver' => 'session', 'provider' => 'users', ], 'admin' => [ 'driver' => 'session', 'provider' => 'admins', ], 'api' => [ 'driver' => 'token', 'provider' => 'users', ], ], 'providers' => [ 'users' => [ 'driver' => 'eloquent', 'model' => App\User::class, ], 'admins' => [ 'driver' => 'eloquent', 'model' => App\Admin::class, ], ], 'passwords' => [ 'users' => [ 'provider' => 'users', 'email' => 'auth.emails.password', 'table' => 'password_resets', 'expire' => 60, ], ],];

3.创建数据模型

执行php artisan make:model Admin --migration

然后修改database\migrations中的数据表结构,复制user表的就可以

执行php artisan migrate 会发现生成了admin表

4.定义后台路由

这里我直接定义了一个路由组

Route::group(['prefix' => 'admin' ,'middleware' => 'admin'], function () { Route::get('login', 'Admin\AuthController@getLogin'); Route::post('login', 'Admin\AuthController@postLogin'); Route::get('register', 'Admin\AuthController@getRegister'); Route::post('register', 'Admin\AuthController@postRegister'); Route::get('logout', 'Admin\AuthController@logout'); Route::get('/', 'Admin\AdminController@index');});

5.创建控制器

执行

php artisan make:controller Admin/AuthControllerphp artisan make:controller Admin/AdminController

AuthController.php可参照Auth中的AuthController.php

<?phpnamespace App\Http\Controllers\Admin;use App\Admin;use Validator;use App\Http\Controllers\Controller;use Illuminate\Foundation\Auth\ThrottlesLogins;use Illuminate\Foundation\Auth\AuthenticatesAndRegistersUsers;class AuthController extends Controller{ use AuthenticatesAndRegistersUsers, ThrottlesLogins; protected $redirectTo = '/admin'; protected $guard = 'admin'; protected $loginView = 'admin.login'; protected $registerView = 'admin.register'; public function __construct() { $this->middleware('guest:admin', ['except' => 'logout']); } protected function validator(array $data) { return Validator::make($data, [ 'name' => 'required|max:255', 'email' => 'required|email|max:255|unique:admins', 'password' => 'required|confirmed|min:6', ]); } protected function create(array $data) { return Admin::create([ 'name' => $data['name'], 'email' => $data['email'], 'password' => bcrypt($data['password']), ]); }}

AdminController.php

<?phpnamespace App\Http\Controllers\Admin;use Illuminate\Http\Request;use App\Http\Requests;use App\Http\Controllers\Controller;use Auth;class AdminController extends Controller{ public function __construct() { $this->middleware('auth:admin'); } public function index() { $admin = Auth::guard('admin')->user(); return view('admin.home'); }}

6.创建视图

这里直接复制auth中的视图和home.blade.php到一个新的admin文件夹中。

修改login和register中表单的action,添加上admin前缀

修改layouts中的app.blade.php

<ul class="nav navbar-nav navbar-right"> <!-- Authentication Links --> @if (Auth::guest('admin')) <li><a href="{{ url('/login') }}">Login</a></li> <li><a href="{{ url('/register') }}">Register</a></li> @else <li class="dropdown"> <a href="#" class="dropdown-toggle" data-toggle="dropdown" role="button" aria-expanded="false"> {{ Auth::guard('admin')->user()->name }} <span class="caret"></span> </a> <ul class="dropdown-menu" role="menu"> <li><a href="{{ url('/admin/logout') }}"><i class="fa fa-btn fa-sign-out"></i>Logout</a></li> </ul> </li> @endif </ul>

现在试一下前后台分别登录吧!

7.可能遇到的页面跳转问题

如果你遇到了这种情况可以尝试修改一下Middleware\Authenticate.php

return redirect()->guest($guard.'/login');

以上所述是小编给大家介绍的laravel5.2实现区分前后台用户登录的方法,希望对大家有所帮助,如果大家有任何疑问请给我留言,小编会及时回复大家的。在此也非常感谢大家对脚本之家网站的支持!

1.前台登录

直接使用laravel自带的auth

php artisan make:auth

然后可以查看路由文件:

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

执行php artisan migrate

会发现生成了两张表。

2.后台登录

编辑配置文件

config\auth.php

添加guards中的admin和providers中的admins

<?phpreturn [ 'defaults' => [ 'guard' => 'web', 'passwords' => 'users', ], 'guards' => [ 'web' => [ 'driver' => 'session', 'provider' => 'users', ], 'admin' => [ 'driver' => 'session', 'provider' => 'admins', ], 'api' => [ 'driver' => 'token', 'provider' => 'users', ], ], 'providers' => [ 'users' => [ 'driver' => 'eloquent', 'model' => App\User::class, ], 'admins' => [ 'driver' => 'eloquent', 'model' => App\Admin::class, ], ], 'passwords' => [ 'users' => [ 'provider' => 'users', 'email' => 'auth.emails.password', 'table' => 'password_resets', 'expire' => 60, ], ],];

3.创建数据模型

执行php artisan make:model Admin --migration

然后修改database\migrations中的数据表结构,复制user表的就可以

执行php artisan migrate 会发现生成了admin表

4.定义后台路由

这里我直接定义了一个路由组

Route::group(['prefix' => 'admin' ,'middleware' => 'admin'], function () { Route::get('login', 'Admin\AuthController@getLogin'); Route::post('login', 'Admin\AuthController@postLogin'); Route::get('register', 'Admin\AuthController@getRegister'); Route::post('register', 'Admin\AuthController@postRegister'); Route::get('logout', 'Admin\AuthController@logout'); Route::get('/', 'Admin\AdminController@index');});

5.创建控制器

执行

php artisan make:controller Admin/AuthControllerphp artisan make:controller Admin/AdminController

AuthController.php可参照Auth中的AuthController.php

<?phpnamespace App\Http\Controllers\Admin;use App\Admin;use Validator;use App\Http\Controllers\Controller;use Illuminate\Foundation\Auth\ThrottlesLogins;use Illuminate\Foundation\Auth\AuthenticatesAndRegistersUsers;class AuthController extends Controller{ use AuthenticatesAndRegistersUsers, ThrottlesLogins; protected $redirectTo = '/admin'; protected $guard = 'admin'; protected $loginView = 'admin.login'; protected $registerView = 'admin.register'; public function __construct() { $this->middleware('guest:admin', ['except' => 'logout']); } protected function validator(array $data) { return Validator::make($data, [ 'name' => 'required|max:255', 'email' => 'required|email|max:255|unique:admins', 'password' => 'required|confirmed|min:6', ]); } protected function create(array $data) { return Admin::create([ 'name' => $data['name'], 'email' => $data['email'], 'password' => bcrypt($data['password']), ]); }}

AdminController.php

<?phpnamespace App\Http\Controllers\Admin;use Illuminate\Http\Request;use App\Http\Requests;use App\Http\Controllers\Controller;use Auth;class AdminController extends Controller{ public function __construct() { $this->middleware('auth:admin'); } public function index() { $admin = Auth::guard('admin')->user(); return view('admin.home'); }}

6.创建视图

这里直接复制auth中的视图和home.blade.php到一个新的admin文件夹中。

修改login和register中表单的action,添加上admin前缀

修改layouts中的app.blade.php

<ul class="nav navbar-nav navbar-right"> <!-- Authentication Links --> @if (Auth::guest('admin')) <li><a href="{{ url('/login') }}">Login</a></li> <li><a href="{{ url('/register') }}">Register</a></li> @else <li class="dropdown"> <a href="#" class="dropdown-toggle" data-toggle="dropdown" role="button" aria-expanded="false"> {{ Auth::guard('admin')->user()->name }} <span class="caret"></span> </a> <ul class="dropdown-menu" role="menu"> <li><a href="{{ url('/admin/logout') }}"><i class="fa fa-btn fa-sign-out"></i>Logout</a></li> </ul> </li> @endif </ul>

现在试一下前后台分别登录吧!

7.可能遇到的页面跳转问题

如果你遇到了这种情况可以尝试修改一下Middleware\Authenticate.php

return redirect()->guest($guard.'/login');

以上所述是小编给大家介绍的laravel5.2实现区分前后台用户登录的方法,希望对大家有所帮助,如果大家有任何疑问请给我留言,小编会及时回复大家的。在此也非常感谢大家对脚本之家网站的支持!

1.前台登录

直接使用laravel自带的auth

php artisan make:auth

然后可以查看路由文件:

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

执行php artisan migrate

会发现生成了两张表。

2.后台登录

编辑配置文件

config\auth.php

添加guards中的admin和providers中的admins

<?phpreturn [ 'defaults' => [ 'guard' => 'web', 'passwords' => 'users', ], 'guards' => [ 'web' => [ 'driver' => 'session', 'provider' => 'users', ], 'admin' => [ 'driver' => 'session', 'provider' => 'admins', ], 'api' => [ 'driver' => 'token', 'provider' => 'users', ], ], 'providers' => [ 'users' => [ 'driver' => 'eloquent', 'model' => App\User::class, ], 'admins' => [ 'driver' => 'eloquent', 'model' => App\Admin::class, ], ], 'passwords' => [ 'users' => [ 'provider' => 'users', 'email' => 'auth.emails.password', 'table' => 'password_resets', 'expire' => 60, ], ],];

3.创建数据模型

执行php artisan make:model Admin --migration

然后修改database\migrations中的数据表结构,复制user表的就可以

执行php artisan migrate 会发现生成了admin表

4.定义后台路由

这里我直接定义了一个路由组

Route::group(['prefix' => 'admin' ,'middleware' => 'admin'], function () { Route::get('login', 'Admin\AuthController@getLogin'); Route::post('login', 'Admin\AuthController@postLogin'); Route::get('register', 'Admin\AuthController@getRegister'); Route::post('register', 'Admin\AuthController@postRegister'); Route::get('logout', 'Admin\AuthController@logout'); Route::get('/', 'Admin\AdminController@index');});

5.创建控制器

执行

php artisan make:controller Admin/AuthControllerphp artisan make:controller Admin/AdminController

AuthController.php可参照Auth中的AuthController.php

<?phpnamespace App\Http\Controllers\Admin;use App\Admin;use Validator;use App\Http\Controllers\Controller;use Illuminate\Foundation\Auth\ThrottlesLogins;use Illuminate\Foundation\Auth\AuthenticatesAndRegistersUsers;class AuthController extends Controller{ use AuthenticatesAndRegistersUsers, ThrottlesLogins; protected $redirectTo = '/admin'; protected $guard = 'admin'; protected $loginView = 'admin.login'; protected $registerView = 'admin.register'; public function __construct() { $this->middleware('guest:admin', ['except' => 'logout']); } protected function validator(array $data) { return Validator::make($data, [ 'name' => 'required|max:255', 'email' => 'required|email|max:255|unique:admins', 'password' => 'required|confirmed|min:6', ]); } protected function create(array $data) { return Admin::create([ 'name' => $data['name'], 'email' => $data['email'], 'password' => bcrypt($data['password']), ]); }}

AdminController.php

<?phpnamespace App\Http\Controllers\Admin;use Illuminate\Http\Request;use App\Http\Requests;use App\Http\Controllers\Controller;use Auth;class AdminController extends Controller{ public function __construct() { $this->middleware('auth:admin'); } public function index() { $admin = Auth::guard('admin')->user(); return view('admin.home'); }}

6.创建视图

这里直接复制auth中的视图和home.blade.php到一个新的admin文件夹中。

修改login和register中表单的action,添加上admin前缀

修改layouts中的app.blade.php

<ul class="nav navbar-nav navbar-right"> <!-- Authentication Links --> @if (Auth::guest('admin')) <li><a href="{{ url('/login') }}">Login</a></li> <li><a href="{{ url('/register') }}">Register</a></li> @else <li class="dropdown"> <a href="#" class="dropdown-toggle" data-toggle="dropdown" role="button" aria-expanded="false"> {{ Auth::guard('admin')->user()->name }} <span class="caret"></span> </a> <ul class="dropdown-menu" role="menu"> <li><a href="{{ url('/admin/logout') }}"><i class="fa fa-btn fa-sign-out"></i>Logout</a></li> </ul> </li> @endif </ul>

现在试一下前后台分别登录吧!

7.可能遇到的页面跳转问题

如果你遇到了这种情况可以尝试修改一下Middleware\Authenticate.php

return redirect()->guest($guard.'/login');

以上所述是小编给大家介绍的laravel5.2实现区分前后台用户登录的方法,希望对大家有所帮助,如果大家有任何疑问请给我留言,小编会及时回复大家的。在此也非常感谢大家对脚本之家网站的支持!

1.前台登录

直接使用laravel自带的auth

php artisan make:auth

然后可以查看路由文件:

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

执行php artisan migrate

会发现生成了两张表。

2.后台登录

编辑配置文件

config\auth.php

添加guards中的admin和providers中的admins

<?phpreturn [ 'defaults' => [ 'guard' => 'web', 'passwords' => 'users', ], 'guards' => [ 'web' => [ 'driver' => 'session', 'provider' => 'users', ], 'admin' => [ 'driver' => 'session', 'provider' => 'admins', ], 'api' => [ 'driver' => 'token', 'provider' => 'users', ], ], 'providers' => [ 'users' => [ 'driver' => 'eloquent', 'model' => App\User::class, ], 'admins' => [ 'driver' => 'eloquent', 'model' => App\Admin::class, ], ], 'passwords' => [ 'users' => [ 'provider' => 'users', 'email' => 'auth.emails.password', 'table' => 'password_resets', 'expire' => 60, ], ],];

3.创建数据模型

执行php artisan make:model Admin --migration

然后修改database\migrations中的数据表结构,复制user表的就可以

执行php artisan migrate 会发现生成了admin表

4.定义后台路由

这里我直接定义了一个路由组

Route::group(['prefix' => 'admin' ,'middleware' => 'admin'], function () { Route::get('login', 'Admin\AuthController@getLogin'); Route::post('login', 'Admin\AuthController@postLogin'); Route::get('register', 'Admin\AuthController@getRegister'); Route::post('register', 'Admin\AuthController@postRegister'); Route::get('logout', 'Admin\AuthController@logout'); Route::get('/', 'Admin\AdminController@index');});

5.创建控制器

执行

php artisan make:controller Admin/AuthControllerphp artisan make:controller Admin/AdminController

AuthController.php可参照Auth中的AuthController.php

<?phpnamespace App\Http\Controllers\Admin;use App\Admin;use Validator;use App\Http\Controllers\Controller;use Illuminate\Foundation\Auth\ThrottlesLogins;use Illuminate\Foundation\Auth\AuthenticatesAndRegistersUsers;class AuthController extends Controller{ use AuthenticatesAndRegistersUsers, ThrottlesLogins; protected $redirectTo = '/admin'; protected $guard = 'admin'; protected $loginView = 'admin.login'; protected $registerView = 'admin.register'; public function __construct() { $this->middleware('guest:admin', ['except' => 'logout']); } protected function validator(array $data) { return Validator::make($data, [ 'name' => 'required|max:255', 'email' => 'required|email|max:255|unique:admins', 'password' => 'required|confirmed|min:6', ]); } protected function create(array $data) { return Admin::create([ 'name' => $data['name'], 'email' => $data['email'], 'password' => bcrypt($data['password']), ]); }}

AdminController.php

<?phpnamespace App\Http\Controllers\Admin;use Illuminate\Http\Request;use App\Http\Requests;use App\Http\Controllers\Controller;use Auth;class AdminController extends Controller{ public function __construct() { $this->middleware('auth:admin'); } public function index() { $admin = Auth::guard('admin')->user(); return view('admin.home'); }}

6.创建视图

这里直接复制auth中的视图和home.blade.php到一个新的admin文件夹中。

修改login和register中表单的action,添加上admin前缀

修改layouts中的app.blade.php

<ul class="nav navbar-nav navbar-right"> <!-- Authentication Links --> @if (Auth::guest('admin')) <li><a href="{{ url('/login') }}">Login</a></li> <li><a href="{{ url('/register') }}">Register</a></li> @else <li class="dropdown"> <a href="#" class="dropdown-toggle" data-toggle="dropdown" role="button" aria-expanded="false"> {{ Auth::guard('admin')->user()->name }} <span class="caret"></span> </a> <ul class="dropdown-menu" role="menu"> <li><a href="{{ url('/admin/logout') }}"><i class="fa fa-btn fa-sign-out"></i>Logout</a></li> </ul> </li> @endif </ul>

现在试一下前后台分别登录吧!

7.可能遇到的页面跳转问题

如果你遇到了这种情况可以尝试修改一下Middleware\Authenticate.php

return redirect()->guest($guard.'/login');

以上所述是小编给大家介绍的laravel5.2实现区分前后台用户登录的方法,希望对大家有所帮助,如果大家有任何疑问请给我留言,小编会及时回复大家的。在此也非常感谢大家对脚本之家网站的支持!

威尼斯电脑教程laravel5.2实现区分前后台用户登录的方法

1.前台登录

直接使用laravel自带的auth

php artisan make:auth

然后可以查看路由文件:

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

执行php artisan migrate

会发现生成了两张表。

2.后台登录

编辑配置文件

config\auth.php

添加guards中的admin和providers中的admins

<?phpreturn [ 'defaults' => [ 'guard' => 'web', 'passwords' => 'users', ], 'guards' => [ 'web' => [ 'driver' => 'session', 'provider' => 'users', ], 'admin' => [ 'driver' => 'session', 'provider' => 'admins', ], 'api' => [ 'driver' => 'token', 'provider' => 'users', ], ], 'providers' => [ 'users' => [ 'driver' => 'eloquent', 'model' => App\User::class, ], 'admins' => [ 'driver' => 'eloquent', 'model' => App\Admin::class, ], ], 'passwords' => [ 'users' => [ 'provider' => 'users', 'email' => 'auth.emails.password', 'table' => 'password_resets', 'expire' => 60, ], ],];

3.创建数据模型

执行php artisan make:model Admin --migration

然后修改database\migrations中的数据表结构,复制user表的就可以

执行php artisan migrate 会发现生成了admin表

4.定义后台路由

这里我直接定义了一个路由组

Route::group(['prefix' => 'admin' ,'middleware' => 'admin'], function () { Route::get('login', 'Admin\AuthController@getLogin'); Route::post('login', 'Admin\AuthController@postLogin'); Route::get('register', 'Admin\AuthController@getRegister'); Route::post('register', 'Admin\AuthController@postRegister'); Route::get('logout', 'Admin\AuthController@logout'); Route::get('/', 'Admin\AdminController@index');});

5.创建控制器

执行

php artisan make:controller Admin/AuthControllerphp artisan make:controller Admin/AdminController

AuthController.php可参照Auth中的AuthController.php

<?phpnamespace App\Http\Controllers\Admin;use App\Admin;use Validator;use App\Http\Controllers\Controller;use Illuminate\Foundation\Auth\ThrottlesLogins;use Illuminate\Foundation\Auth\AuthenticatesAndRegistersUsers;class AuthController extends Controller{ use AuthenticatesAndRegistersUsers, ThrottlesLogins; protected $redirectTo = '/admin'; protected $guard = 'admin'; protected $loginView = 'admin.login'; protected $registerView = 'admin.register'; public function __construct() { $this->middleware('guest:admin', ['except' => 'logout']); } protected function validator(array $data) { return Validator::make($data, [ 'name' => 'required|max:255', 'email' => 'required|email|max:255|unique:admins', 'password' => 'required|confirmed|min:6', ]); } protected function create(array $data) { return Admin::create([ 'name' => $data['name'], 'email' => $data['email'], 'password' => bcrypt($data['password']), ]); }}

AdminController.php

<?phpnamespace App\Http\Controllers\Admin;use Illuminate\Http\Request;use App\Http\Requests;use App\Http\Controllers\Controller;use Auth;class AdminController extends Controller{ public function __construct() { $this->middleware('auth:admin'); } public function index() { $admin = Auth::guard('admin')->user(); return view('admin.home'); }}

6.创建视图

这里直接复制auth中的视图和home.blade.php到一个新的admin文件夹中。

修改login和register中表单的action,添加上admin前缀

修改layouts中的app.blade.php

<ul class="nav navbar-nav navbar-right"> <!-- Authentication Links --> @if (Auth::guest('admin')) <li><a href="{{ url('/login') }}">Login</a></li> <li><a href="{{ url('/register') }}">Register</a></li> @else <li class="dropdown"> <a href="#" class="dropdown-toggle" data-toggle="dropdown" role="button" aria-expanded="false"> {{ Auth::guard('admin')->user()->name }} <span class="caret"></span> </a> <ul class="dropdown-menu" role="menu"> <li><a href="{{ url('/admin/logout') }}"><i class="fa fa-btn fa-sign-out"></i>Logout</a></li> </ul> </li> @endif </ul>

现在试一下前后台分别登录吧!

7.可能遇到的页面跳转问题

如果你遇到了这种情况可以尝试修改一下Middleware\Authenticate.php

return redirect()->guest($guard.'/login');

以上所述是小编给大家介绍的laravel5.2实现区分前后台用户登录的方法,希望对大家有所帮助,如果大家有任何疑问请给我留言,小编会及时回复大家的。在此也非常感谢大家对脚本之家网站的支持!

1.前台登录

直接使用laravel自带的auth

php artisan make:auth

然后可以查看路由文件:

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

执行php artisan migrate

会发现生成了两张表。

2.后台登录

编辑配置文件

config\auth.php

添加guards中的admin和providers中的admins

<?phpreturn [ 'defaults' => [ 'guard' => 'web', 'passwords' => 'users', ], 'guards' => [ 'web' => [ 'driver' => 'session', 'provider' => 'users', ], 'admin' => [ 'driver' => 'session', 'provider' => 'admins', ], 'api' => [ 'driver' => 'token', 'provider' => 'users', ], ], 'providers' => [ 'users' => [ 'driver' => 'eloquent', 'model' => App\User::class, ], 'admins' => [ 'driver' => 'eloquent', 'model' => App\Admin::class, ], ], 'passwords' => [ 'users' => [ 'provider' => 'users', 'email' => 'auth.emails.password', 'table' => 'password_resets', 'expire' => 60, ], ],];

3.创建数据模型

执行php artisan make:model Admin --migration

然后修改database\migrations中的数据表结构,复制user表的就可以

执行php artisan migrate 会发现生成了admin表

4.定义后台路由

这里我直接定义了一个路由组

Route::group(['prefix' => 'admin' ,'middleware' => 'admin'], function () { Route::get('login', 'Admin\AuthController@getLogin'); Route::post('login', 'Admin\AuthController@postLogin'); Route::get('register', 'Admin\AuthController@getRegister'); Route::post('register', 'Admin\AuthController@postRegister'); Route::get('logout', 'Admin\AuthController@logout'); Route::get('/', 'Admin\AdminController@index');});

5.创建控制器

执行

php artisan make:controller Admin/AuthControllerphp artisan make:controller Admin/AdminController

AuthController.php可参照Auth中的AuthController.php

<?phpnamespace App\Http\Controllers\Admin;use App\Admin;use Validator;use App\Http\Controllers\Controller;use Illuminate\Foundation\Auth\ThrottlesLogins;use Illuminate\Foundation\Auth\AuthenticatesAndRegistersUsers;class AuthController extends Controller{ use AuthenticatesAndRegistersUsers, ThrottlesLogins; protected $redirectTo = '/admin'; protected $guard = 'admin'; protected $loginView = 'admin.login'; protected $registerView = 'admin.register'; public function __construct() { $this->middleware('guest:admin', ['except' => 'logout']); } protected function validator(array $data) { return Validator::make($data, [ 'name' => 'required|max:255', 'email' => 'required|email|max:255|unique:admins', 'password' => 'required|confirmed|min:6', ]); } protected function create(array $data) { return Admin::create([ 'name' => $data['name'], 'email' => $data['email'], 'password' => bcrypt($data['password']), ]); }}

AdminController.php

<?phpnamespace App\Http\Controllers\Admin;use Illuminate\Http\Request;use App\Http\Requests;use App\Http\Controllers\Controller;use Auth;class AdminController extends Controller{ public function __construct() { $this->middleware('auth:admin'); } public function index() { $admin = Auth::guard('admin')->user(); return view('admin.home'); }}

6.创建视图

这里直接复制auth中的视图和home.blade.php到一个新的admin文件夹中。

修改login和register中表单的action,添加上admin前缀

修改layouts中的app.blade.php

<ul class="nav navbar-nav navbar-right"> <!-- Authentication Links --> @if (Auth::guest('admin')) <li><a href="{{ url('/login') }}">Login</a></li> <li><a href="{{ url('/register') }}">Register</a></li> @else <li class="dropdown"> <a href="#" class="dropdown-toggle" data-toggle="dropdown" role="button" aria-expanded="false"> {{ Auth::guard('admin')->user()->name }} <span class="caret"></span> </a> <ul class="dropdown-menu" role="menu"> <li><a href="{{ url('/admin/logout') }}"><i class="fa fa-btn fa-sign-out"></i>Logout</a></li> </ul> </li> @endif </ul>

现在试一下前后台分别登录吧!

7.可能遇到的页面跳转问题

如果你遇到了这种情况可以尝试修改一下Middleware\Authenticate.php

return redirect()->guest($guard.'/login');

以上所述是小编给大家介绍的laravel5.2实现区分前后台用户登录的方法,希望对大家有所帮助,如果大家有任何疑问请给我留言,小编会及时回复大家的。在此也非常感谢大家对脚本之家网站的支持!

1.前台登录

直接使用laravel自带的auth

php artisan make:auth

然后可以查看路由文件:

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

执行php artisan migrate

会发现生成了两张表。

2.后台登录

编辑配置文件

config\auth.php

添加guards中的admin和providers中的admins

<?phpreturn [ 'defaults' => [ 'guard' => 'web', 'passwords' => 'users', ], 'guards' => [ 'web' => [ 'driver' => 'session', 'provider' => 'users', ], 'admin' => [ 'driver' => 'session', 'provider' => 'admins', ], 'api' => [ 'driver' => 'token', 'provider' => 'users', ], ], 'providers' => [ 'users' => [ 'driver' => 'eloquent', 'model' => App\User::class, ], 'admins' => [ 'driver' => 'eloquent', 'model' => App\Admin::class, ], ], 'passwords' => [ 'users' => [ 'provider' => 'users', 'email' => 'auth.emails.password', 'table' => 'password_resets', 'expire' => 60, ], ],];

3.创建数据模型

执行php artisan make:model Admin --migration

然后修改database\migrations中的数据表结构,复制user表的就可以

执行php artisan migrate 会发现生成了admin表

4.定义后台路由

这里我直接定义了一个路由组

Route::group(['prefix' => 'admin' ,'middleware' => 'admin'], function () { Route::get('login', 'Admin\AuthController@getLogin'); Route::post('login', 'Admin\AuthController@postLogin'); Route::get('register', 'Admin\AuthController@getRegister'); Route::post('register', 'Admin\AuthController@postRegister'); Route::get('logout', 'Admin\AuthController@logout'); Route::get('/', 'Admin\AdminController@index');});

5.创建控制器

执行

php artisan make:controller Admin/AuthControllerphp artisan make:controller Admin/AdminController

AuthController.php可参照Auth中的AuthController.php

<?phpnamespace App\Http\Controllers\Admin;use App\Admin;use Validator;use App\Http\Controllers\Controller;use Illuminate\Foundation\Auth\ThrottlesLogins;use Illuminate\Foundation\Auth\AuthenticatesAndRegistersUsers;class AuthController extends Controller{ use AuthenticatesAndRegistersUsers, ThrottlesLogins; protected $redirectTo = '/admin'; protected $guard = 'admin'; protected $loginView = 'admin.login'; protected $registerView = 'admin.register'; public function __construct() { $this->middleware('guest:admin', ['except' => 'logout']); } protected function validator(array $data) { return Validator::make($data, [ 'name' => 'required|max:255', 'email' => 'required|email|max:255|unique:admins', 'password' => 'required|confirmed|min:6', ]); } protected function create(array $data) { return Admin::create([ 'name' => $data['name'], 'email' => $data['email'], 'password' => bcrypt($data['password']), ]); }}

AdminController.php

<?phpnamespace App\Http\Controllers\Admin;use Illuminate\Http\Request;use App\Http\Requests;use App\Http\Controllers\Controller;use Auth;class AdminController extends Controller{ public function __construct() { $this->middleware('auth:admin'); } public function index() { $admin = Auth::guard('admin')->user(); return view('admin.home'); }}

6.创建视图

这里直接复制auth中的视图和home.blade.php到一个新的admin文件夹中。

修改login和register中表单的action,添加上admin前缀

修改layouts中的app.blade.php

<ul class="nav navbar-nav navbar-right"> <!-- Authentication Links --> @if (Auth::guest('admin')) <li><a href="{{ url('/login') }}">Login</a></li> <li><a href="{{ url('/register') }}">Register</a></li> @else <li class="dropdown"> <a href="#" class="dropdown-toggle" data-toggle="dropdown" role="button" aria-expanded="false"> {{ Auth::guard('admin')->user()->name }} <span class="caret"></span> </a> <ul class="dropdown-menu" role="menu"> <li><a href="{{ url('/admin/logout') }}"><i class="fa fa-btn fa-sign-out"></i>Logout</a></li> </ul> </li> @endif </ul>

现在试一下前后台分别登录吧!

7.可能遇到的页面跳转问题

如果你遇到了这种情况可以尝试修改一下Middleware\Authenticate.php

return redirect()->guest($guard.'/login');

以上所述是小编给大家介绍的laravel5.2实现区分前后台用户登录的方法,希望对大家有所帮助,如果大家有任何疑问请给我留言,小编会及时回复大家的。在此也非常感谢大家对脚本之家网站的支持!

laravel5.2实现区分前后台用户登录的方法

1.前台登录

直接使用laravel自带的auth

php artisan make:auth

然后可以查看路由文件:

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

执行php artisan migrate

会发现生成了两张表。

2.后台登录

编辑配置文件

config\auth.php

添加guards中的admin和providers中的admins

<?phpreturn [ 'defaults' => [ 'guard' => 'web', 'passwords' => 'users', ], 'guards' => [ 'web' => [ 'driver' => 'session', 'provider' => 'users', ], 'admin' => [ 'driver' => 'session', 'provider' => 'admins', ], 'api' => [ 'driver' => 'token', 'provider' => 'users', ], ], 'providers' => [ 'users' => [ 'driver' => 'eloquent', 'model' => App\User::class, ], 'admins' => [ 'driver' => 'eloquent', 'model' => App\Admin::class, ], ], 'passwords' => [ 'users' => [ 'provider' => 'users', 'email' => 'auth.emails.password', 'table' => 'password_resets', 'expire' => 60, ], ],];

3.创建数据模型

执行php artisan make:model Admin --migration

然后修改database\migrations中的数据表结构,复制user表的就可以

执行php artisan migrate 会发现生成了admin表

4.定义后台路由

这里我直接定义了一个路由组

Route::group(['prefix' => 'admin' ,'middleware' => 'admin'], function () { Route::get('login', 'Admin\AuthController@getLogin'); Route::post('login', 'Admin\AuthController@postLogin'); Route::get('register', 'Admin\AuthController@getRegister'); Route::post('register', 'Admin\AuthController@postRegister'); Route::get('logout', 'Admin\AuthController@logout'); Route::get('/', 'Admin\AdminController@index');});

5.创建控制器

执行

php artisan make:controller Admin/AuthControllerphp artisan make:controller Admin/AdminController

AuthController.php可参照Auth中的AuthController.php

<?phpnamespace App\Http\Controllers\Admin;use App\Admin;use Validator;use App\Http\Controllers\Controller;use Illuminate\Foundation\Auth\ThrottlesLogins;use Illuminate\Foundation\Auth\AuthenticatesAndRegistersUsers;class AuthController extends Controller{ use AuthenticatesAndRegistersUsers, ThrottlesLogins; protected $redirectTo = '/admin'; protected $guard = 'admin'; protected $loginView = 'admin.login'; protected $registerView = 'admin.register'; public function __construct() { $this->middleware('guest:admin', ['except' => 'logout']); } protected function validator(array $data) { return Validator::make($data, [ 'name' => 'required|max:255', 'email' => 'required|email|max:255|unique:admins', 'password' => 'required|confirmed|min:6', ]); } protected function create(array $data) { return Admin::create([ 'name' => $data['name'], 'email' => $data['email'], 'password' => bcrypt($data['password']), ]); }}

AdminController.php

<?phpnamespace App\Http\Controllers\Admin;use Illuminate\Http\Request;use App\Http\Requests;use App\Http\Controllers\Controller;use Auth;class AdminController extends Controller{ public function __construct() { $this->middleware('auth:admin'); } public function index() { $admin = Auth::guard('admin')->user(); return view('admin.home'); }}

6.创建视图

这里直接复制auth中的视图和home.blade.php到一个新的admin文件夹中。

修改login和register中表单的action,添加上admin前缀

修改layouts中的app.blade.php

<ul class="nav navbar-nav navbar-right"> <!-- Authentication Links --> @if (Auth::guest('admin')) <li><a href="{{ url('/login') }}">Login</a></li> <li><a href="{{ url('/register') }}">Register</a></li> @else <li class="dropdown"> <a href="#" class="dropdown-toggle" data-toggle="dropdown" role="button" aria-expanded="false"> {{ Auth::guard('admin')->user()->name }} <span class="caret"></span> </a> <ul class="dropdown-menu" role="menu"> <li><a href="{{ url('/admin/logout') }}"><i class="fa fa-btn fa-sign-out"></i>Logout</a></li> </ul> </li> @endif </ul>

现在试一下前后台分别登录吧!

7.可能遇到的页面跳转问题

如果你遇到了这种情况可以尝试修改一下Middleware\Authenticate.php

return redirect()->guest($guard.'/login');

以上所述是小编给大家介绍的laravel5.2实现区分前后台用户登录的方法,希望对大家有所帮助,如果大家有任何疑问请给我留言,小编会及时回复大家的。在此也非常感谢大家对脚本之家网站的支持!

1.前台登录

直接使用laravel自带的auth

php artisan make:auth

然后可以查看路由文件:

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

执行php artisan migrate

会发现生成了两张表。

2.后台登录

编辑配置文件

config\auth.php

添加guards中的admin和providers中的admins

<?phpreturn [ 'defaults' => [ 'guard' => 'web', 'passwords' => 'users', ], 'guards' => [ 'web' => [ 'driver' => 'session', 'provider' => 'users', ], 'admin' => [ 'driver' => 'session', 'provider' => 'admins', ], 'api' => [ 'driver' => 'token', 'provider' => 'users', ], ], 'providers' => [ 'users' => [ 'driver' => 'eloquent', 'model' => App\User::class, ], 'admins' => [ 'driver' => 'eloquent', 'model' => App\Admin::class, ], ], 'passwords' => [ 'users' => [ 'provider' => 'users', 'email' => 'auth.emails.password', 'table' => 'password_resets', 'expire' => 60, ], ],];

3.创建数据模型

执行php artisan make:model Admin --migration

然后修改database\migrations中的数据表结构,复制user表的就可以

执行php artisan migrate 会发现生成了admin表

4.定义后台路由

这里我直接定义了一个路由组

Route::group(['prefix' => 'admin' ,'middleware' => 'admin'], function () { Route::get('login', 'Admin\AuthController@getLogin'); Route::post('login', 'Admin\AuthController@postLogin'); Route::get('register', 'Admin\AuthController@getRegister'); Route::post('register', 'Admin\AuthController@postRegister'); Route::get('logout', 'Admin\AuthController@logout'); Route::get('/', 'Admin\AdminController@index');});

5.创建控制器

执行

php artisan make:controller Admin/AuthControllerphp artisan make:controller Admin/AdminController

AuthController.php可参照Auth中的AuthController.php

<?phpnamespace App\Http\Controllers\Admin;use App\Admin;use Validator;use App\Http\Controllers\Controller;use Illuminate\Foundation\Auth\ThrottlesLogins;use Illuminate\Foundation\Auth\AuthenticatesAndRegistersUsers;class AuthController extends Controller{ use AuthenticatesAndRegistersUsers, ThrottlesLogins; protected $redirectTo = '/admin'; protected $guard = 'admin'; protected $loginView = 'admin.login'; protected $registerView = 'admin.register'; public function __construct() { $this->middleware('guest:admin', ['except' => 'logout']); } protected function validator(array $data) { return Validator::make($data, [ 'name' => 'required|max:255', 'email' => 'required|email|max:255|unique:admins', 'password' => 'required|confirmed|min:6', ]); } protected function create(array $data) { return Admin::create([ 'name' => $data['name'], 'email' => $data['email'], 'password' => bcrypt($data['password']), ]); }}

AdminController.php

<?phpnamespace App\Http\Controllers\Admin;use Illuminate\Http\Request;use App\Http\Requests;use App\Http\Controllers\Controller;use Auth;class AdminController extends Controller{ public function __construct() { $this->middleware('auth:admin'); } public function index() { $admin = Auth::guard('admin')->user(); return view('admin.home'); }}

6.创建视图

这里直接复制auth中的视图和home.blade.php到一个新的admin文件夹中。

修改login和register中表单的action,添加上admin前缀

修改layouts中的app.blade.php

<ul class="nav navbar-nav navbar-right"> <!-- Authentication Links --> @if (Auth::guest('admin')) <li><a href="{{ url('/login') }}">Login</a></li> <li><a href="{{ url('/register') }}">Register</a></li> @else <li class="dropdown"> <a href="#" class="dropdown-toggle" data-toggle="dropdown" role="button" aria-expanded="false"> {{ Auth::guard('admin')->user()->name }} <span class="caret"></span> </a> <ul class="dropdown-menu" role="menu"> <li><a href="{{ url('/admin/logout') }}"><i class="fa fa-btn fa-sign-out"></i>Logout</a></li> </ul> </li> @endif </ul>

现在试一下前后台分别登录吧!

7.可能遇到的页面跳转问题

如果你遇到了这种情况可以尝试修改一下Middleware\Authenticate.php

return redirect()->guest($guard.'/login');

以上所述是小编给大家介绍的laravel5.2实现区分前后台用户登录的方法,希望对大家有所帮助,如果大家有任何疑问请给我留言,小编会及时回复大家的。在此也非常感谢大家对脚本之家网站的支持!

laravel5.2实现区分前后台用户登录的方法

1.前台登录

直接使用laravel自带的auth

php artisan make:auth

然后可以查看路由文件:

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

执行php artisan migrate

会发现生成了两张表。

2.后台登录

编辑配置文件

config\auth.php

添加guards中的admin和providers中的admins

<?phpreturn [ 'defaults' => [ 'guard' => 'web', 'passwords' => 'users', ], 'guards' => [ 'web' => [ 'driver' => 'session', 'provider' => 'users', ], 'admin' => [ 'driver' => 'session', 'provider' => 'admins', ], 'api' => [ 'driver' => 'token', 'provider' => 'users', ], ], 'providers' => [ 'users' => [ 'driver' => 'eloquent', 'model' => App\User::class, ], 'admins' => [ 'driver' => 'eloquent', 'model' => App\Admin::class, ], ], 'passwords' => [ 'users' => [ 'provider' => 'users', 'email' => 'auth.emails.password', 'table' => 'password_resets', 'expire' => 60, ], ],];

3.创建数据模型

执行php artisan make:model Admin --migration

然后修改database\migrations中的数据表结构,复制user表的就可以

执行php artisan migrate 会发现生成了admin表

4.定义后台路由

这里我直接定义了一个路由组

Route::group(['prefix' => 'admin' ,'middleware' => 'admin'], function () { Route::get('login', 'Admin\AuthController@getLogin'); Route::post('login', 'Admin\AuthController@postLogin'); Route::get('register', 'Admin\AuthController@getRegister'); Route::post('register', 'Admin\AuthController@postRegister'); Route::get('logout', 'Admin\AuthController@logout'); Route::get('/', 'Admin\AdminController@index');});

5.创建控制器

执行

php artisan make:controller Admin/AuthControllerphp artisan make:controller Admin/AdminController

AuthController.php可参照Auth中的AuthController.php

<?phpnamespace App\Http\Controllers\Admin;use App\Admin;use Validator;use App\Http\Controllers\Controller;use Illuminate\Foundation\Auth\ThrottlesLogins;use Illuminate\Foundation\Auth\AuthenticatesAndRegistersUsers;class AuthController extends Controller{ use AuthenticatesAndRegistersUsers, ThrottlesLogins; protected $redirectTo = '/admin'; protected $guard = 'admin'; protected $loginView = 'admin.login'; protected $registerView = 'admin.register'; public function __construct() { $this->middleware('guest:admin', ['except' => 'logout']); } protected function validator(array $data) { return Validator::make($data, [ 'name' => 'required|max:255', 'email' => 'required|email|max:255|unique:admins', 'password' => 'required|confirmed|min:6', ]); } protected function create(array $data) { return Admin::create([ 'name' => $data['name'], 'email' => $data['email'], 'password' => bcrypt($data['password']), ]); }}

AdminController.php

<?phpnamespace App\Http\Controllers\Admin;use Illuminate\Http\Request;use App\Http\Requests;use App\Http\Controllers\Controller;use Auth;class AdminController extends Controller{ public function __construct() { $this->middleware('auth:admin'); } public function index() { $admin = Auth::guard('admin')->user(); return view('admin.home'); }}

6.创建视图

这里直接复制auth中的视图和home.blade.php到一个新的admin文件夹中。

修改login和register中表单的action,添加上admin前缀

修改layouts中的app.blade.php

<ul class="nav navbar-nav navbar-right"> <!-- Authentication Links --> @if (Auth::guest('admin')) <li><a href="{{ url('/login') }}">Login</a></li> <li><a href="{{ url('/register') }}">Register</a></li> @else <li class="dropdown"> <a href="#" class="dropdown-toggle" data-toggle="dropdown" role="button" aria-expanded="false"> {{ Auth::guard('admin')->user()->name }} <span class="caret"></span> </a> <ul class="dropdown-menu" role="menu"> <li><a href="{{ url('/admin/logout') }}"><i class="fa fa-btn fa-sign-out"></i>Logout</a></li> </ul> </li> @endif </ul>

现在试一下前后台分别登录吧!

7.可能遇到的页面跳转问题

如果你遇到了这种情况可以尝试修改一下Middleware\Authenticate.php

return redirect()->guest($guard.'/login');

以上所述是小编给大家介绍的laravel5.2实现区分前后台用户登录的方法,希望对大家有所帮助,如果大家有任何疑问请给我留言,小编会及时回复大家的。在此也非常感谢大家对脚本之家网站的支持!

1.前台登录

直接使用laravel自带的auth

php artisan make:auth

然后可以查看路由文件:

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

执行php artisan migrate

会发现生成了两张表。

2.后台登录

编辑配置文件

config\auth.php

添加guards中的admin和providers中的admins

<?phpreturn [ 'defaults' => [ 'guard' => 'web', 'passwords' => 'users', ], 'guards' => [ 'web' => [ 'driver' => 'session', 'provider' => 'users', ], 'admin' => [ 'driver' => 'session', 'provider' => 'admins', ], 'api' => [ 'driver' => 'token', 'provider' => 'users', ], ], 'providers' => [ 'users' => [ 'driver' => 'eloquent', 'model' => App\User::class, ], 'admins' => [ 'driver' => 'eloquent', 'model' => App\Admin::class, ], ], 'passwords' => [ 'users' => [ 'provider' => 'users', 'email' => 'auth.emails.password', 'table' => 'password_resets', 'expire' => 60, ], ],];

3.创建数据模型

执行php artisan make:model Admin --migration

然后修改database\migrations中的数据表结构,复制user表的就可以

执行php artisan migrate 会发现生成了admin表

4.定义后台路由

这里我直接定义了一个路由组

Route::group(['prefix' => 'admin' ,'middleware' => 'admin'], function () { Route::get('login', 'Admin\AuthController@getLogin'); Route::post('login', 'Admin\AuthController@postLogin'); Route::get('register', 'Admin\AuthController@getRegister'); Route::post('register', 'Admin\AuthController@postRegister'); Route::get('logout', 'Admin\AuthController@logout'); Route::get('/', 'Admin\AdminController@index');});

5.创建控制器

执行

php artisan make:controller Admin/AuthControllerphp artisan make:controller Admin/AdminController

AuthController.php可参照Auth中的AuthController.php

<?phpnamespace App\Http\Controllers\Admin;use App\Admin;use Validator;use App\Http\Controllers\Controller;use Illuminate\Foundation\Auth\ThrottlesLogins;use Illuminate\Foundation\Auth\AuthenticatesAndRegistersUsers;class AuthController extends Controller{ use AuthenticatesAndRegistersUsers, ThrottlesLogins; protected $redirectTo = '/admin'; protected $guard = 'admin'; protected $loginView = 'admin.login'; protected $registerView = 'admin.register'; public function __construct() { $this->middleware('guest:admin', ['except' => 'logout']); } protected function validator(array $data) { return Validator::make($data, [ 'name' => 'required|max:255', 'email' => 'required|email|max:255|unique:admins', 'password' => 'required|confirmed|min:6', ]); } protected function create(array $data) { return Admin::create([ 'name' => $data['name'], 'email' => $data['email'], 'password' => bcrypt($data['password']), ]); }}

AdminController.php

<?phpnamespace App\Http\Controllers\Admin;use Illuminate\Http\Request;use App\Http\Requests;use App\Http\Controllers\Controller;use Auth;class AdminController extends Controller{ public function __construct() { $this->middleware('auth:admin'); } public function index() { $admin = Auth::guard('admin')->user(); return view('admin.home'); }}

6.创建视图

这里直接复制auth中的视图和home.blade.php到一个新的admin文件夹中。

修改login和register中表单的action,添加上admin前缀

修改layouts中的app.blade.php

<ul class="nav navbar-nav navbar-right"> <!-- Authentication Links --> @if (Auth::guest('admin')) <li><a href="{{ url('/login') }}">Login</a></li> <li><a href="{{ url('/register') }}">Register</a></li> @else <li class="dropdown"> <a href="#" class="dropdown-toggle" data-toggle="dropdown" role="button" aria-expanded="false"> {{ Auth::guard('admin')->user()->name }} <span class="caret"></span> </a> <ul class="dropdown-menu" role="menu"> <li><a href="{{ url('/admin/logout') }}"><i class="fa fa-btn fa-sign-out"></i>Logout</a></li> </ul> </li> @endif </ul>

现在试一下前后台分别登录吧!

7.可能遇到的页面跳转问题

如果你遇到了这种情况可以尝试修改一下Middleware\Authenticate.php

return redirect()->guest($guard.'/login');

以上所述是小编给大家介绍的laravel5.2实现区分前后台用户登录的方法,希望对大家有所帮助,如果大家有任何疑问请给我留言,小编会及时回复大家的。在此也非常感谢大家对脚本之家网站的支持!

1.前台登录

直接使用laravel自带的auth

php artisan make:auth

然后可以查看路由文件:

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

执行php artisan migrate

会发现生成了两张表。

2.后台登录

编辑配置文件

config\auth.php

添加guards中的admin和providers中的admins

<?phpreturn [ 'defaults' => [ 'guard' => 'web', 'passwords' => 'users', ], 'guards' => [ 'web' => [ 'driver' => 'session', 'provider' => 'users', ], 'admin' => [ 'driver' => 'session', 'provider' => 'admins', ], 'api' => [ 'driver' => 'token', 'provider' => 'users', ], ], 'providers' => [ 'users' => [ 'driver' => 'eloquent', 'model' => App\User::class, ], 'admins' => [ 'driver' => 'eloquent', 'model' => App\Admin::class, ], ], 'passwords' => [ 'users' => [ 'provider' => 'users', 'email' => 'auth.emails.password', 'table' => 'password_resets', 'expire' => 60, ], ],];

3.创建数据模型

执行php artisan make:model Admin --migration

然后修改database\migrations中的数据表结构,复制user表的就可以

执行php artisan migrate 会发现生成了admin表

4.定义后台路由

这里我直接定义了一个路由组

Route::group(['prefix' => 'admin' ,'middleware' => 'admin'], function () { Route::get('login', 'Admin\AuthController@getLogin'); Route::post('login', 'Admin\AuthController@postLogin'); Route::get('register', 'Admin\AuthController@getRegister'); Route::post('register', 'Admin\AuthController@postRegister'); Route::get('logout', 'Admin\AuthController@logout'); Route::get('/', 'Admin\AdminController@index');});

5.创建控制器

执行

php artisan make:controller Admin/AuthControllerphp artisan make:controller Admin/AdminController

AuthController.php可参照Auth中的AuthController.php

<?phpnamespace App\Http\Controllers\Admin;use App\Admin;use Validator;use App\Http\Controllers\Controller;use Illuminate\Foundation\Auth\ThrottlesLogins;use Illuminate\Foundation\Auth\AuthenticatesAndRegistersUsers;class AuthController extends Controller{ use AuthenticatesAndRegistersUsers, ThrottlesLogins; protected $redirectTo = '/admin'; protected $guard = 'admin'; protected $loginView = 'admin.login'; protected $registerView = 'admin.register'; public function __construct() { $this->middleware('guest:admin', ['except' => 'logout']); } protected function validator(array $data) { return Validator::make($data, [ 'name' => 'required|max:255', 'email' => 'required|email|max:255|unique:admins', 'password' => 'required|confirmed|min:6', ]); } protected function create(array $data) { return Admin::create([ 'name' => $data['name'], 'email' => $data['email'], 'password' => bcrypt($data['password']), ]); }}

AdminController.php

<?phpnamespace App\Http\Controllers\Admin;use Illuminate\Http\Request;use App\Http\Requests;use App\Http\Controllers\Controller;use Auth;class AdminController extends Controller{ public function __construct() { $this->middleware('auth:admin'); } public function index() { $admin = Auth::guard('admin')->user(); return view('admin.home'); }}

6.创建视图

这里直接复制auth中的视图和home.blade.php到一个新的admin文件夹中。

修改login和register中表单的action,添加上admin前缀

修改layouts中的app.blade.php

<ul class="nav navbar-nav navbar-right"> <!-- Authentication Links --> @if (Auth::guest('admin')) <li><a href="{{ url('/login') }}">Login</a></li> <li><a href="{{ url('/register') }}">Register</a></li> @else <li class="dropdown"> <a href="#" class="dropdown-toggle" data-toggle="dropdown" role="button" aria-expanded="false"> {{ Auth::guard('admin')->user()->name }} <span class="caret"></span> </a> <ul class="dropdown-menu" role="menu"> <li><a href="{{ url('/admin/logout') }}"><i class="fa fa-btn fa-sign-out"></i>Logout</a></li> </ul> </li> @endif </ul>

现在试一下前后台分别登录吧!

7.可能遇到的页面跳转问题

如果你遇到了这种情况可以尝试修改一下Middleware\Authenticate.php

return redirect()->guest($guard.'/login');

以上所述是小编给大家介绍的laravel5.2实现区分前后台用户登录的方法,希望对大家有所帮助,如果大家有任何疑问请给我留言,小编会及时回复大家的。在此也非常感谢大家对脚本之家网站的支持!

1.

1.前台登录

直接使用laravel自带的auth

php artisan make:auth

然后可以查看路由文件:

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

执行php artisan migrate

会发现生成了两张表。

2.后台登录

编辑配置文件

config\auth.php

添加guards中的admin和providers中的admins

<?phpreturn [ 'defaults' => [ 'guard' => 'web', 'passwords' => 'users', ], 'guards' => [ 'web' => [ 'driver' => 'session', 'provider' => 'users', ], 'admin' => [ 'driver' => 'session', 'provider' => 'admins', ], 'api' => [ 'driver' => 'token', 'provider' => 'users', ], ], 'providers' => [ 'users' => [ 'driver' => 'eloquent', 'model' => App\User::class, ], 'admins' => [ 'driver' => 'eloquent', 'model' => App\Admin::class, ], ], 'passwords' => [ 'users' => [ 'provider' => 'users', 'email' => 'auth.emails.password', 'table' => 'password_resets', 'expire' => 60, ], ],];

3.创建数据模型

执行php artisan make:model Admin --migration

然后修改database\migrations中的数据表结构,复制user表的就可以

执行php artisan migrate 会发现生成了admin表

4.定义后台路由

这里我直接定义了一个路由组

Route::group(['prefix' => 'admin' ,'middleware' => 'admin'], function () { Route::get('login', 'Admin\AuthController@getLogin'); Route::post('login', 'Admin\AuthController@postLogin'); Route::get('register', 'Admin\AuthController@getRegister'); Route::post('register', 'Admin\AuthController@postRegister'); Route::get('logout', 'Admin\AuthController@logout'); Route::get('/', 'Admin\AdminController@index');});

5.创建控制器

执行

php artisan make:controller Admin/AuthControllerphp artisan make:controller Admin/AdminController

AuthController.php可参照Auth中的AuthController.php

<?phpnamespace App\Http\Controllers\Admin;use App\Admin;use Validator;use App\Http\Controllers\Controller;use Illuminate\Foundation\Auth\ThrottlesLogins;use Illuminate\Foundation\Auth\AuthenticatesAndRegistersUsers;class AuthController extends Controller{ use AuthenticatesAndRegistersUsers, ThrottlesLogins; protected $redirectTo = '/admin'; protected $guard = 'admin'; protected $loginView = 'admin.login'; protected $registerView = 'admin.register'; public function __construct() { $this->middleware('guest:admin', ['except' => 'logout']); } protected function validator(array $data) { return Validator::make($data, [ 'name' => 'required|max:255', 'email' => 'required|email|max:255|unique:admins', 'password' => 'required|confirmed|min:6', ]); } protected function create(array $data) { return Admin::create([ 'name' => $data['name'], 'email' => $data['email'], 'password' => bcrypt($data['password']), ]); }}

AdminController.php

<?phpnamespace App\Http\Controllers\Admin;use Illuminate\Http\Request;use App\Http\Requests;use App\Http\Controllers\Controller;use Auth;class AdminController extends Controller{ public function __construct() { $this->middleware('auth:admin'); } public function index() { $admin = Auth::guard('admin')->user(); return view('admin.home'); }}

6.创建视图

这里直接复制auth中的视图和home.blade.php到一个新的admin文件夹中。

修改login和register中表单的action,添加上admin前缀

修改layouts中的app.blade.php

<ul class="nav navbar-nav navbar-right"> <!-- Authentication Links --> @if (Auth::guest('admin')) <li><a href="{{ url('/login') }}">Login</a></li> <li><a href="{{ url('/register') }}">Register</a></li> @else <li class="dropdown"> <a href="#" class="dropdown-toggle" data-toggle="dropdown" role="button" aria-expanded="false"> {{ Auth::guard('admin')->user()->name }} <span class="caret"></span> </a> <ul class="dropdown-menu" role="menu"> <li><a href="{{ url('/admin/logout') }}"><i class="fa fa-btn fa-sign-out"></i>Logout</a></li> </ul> </li> @endif </ul>

现在试一下前后台分别登录吧!

7.可能遇到的页面跳转问题

如果你遇到了这种情况可以尝试修改一下Middleware\Authenticate.php

return redirect()->guest($guard.'/login');

以上所述是小编给大家介绍的laravel5.2实现区分前后台用户登录的方法,希望对大家有所帮助,如果大家有任何疑问请给我留言,小编会及时回复大家的。在此也非常感谢大家对脚本之家网站的支持!

laravel5.2实现区分前后台用户登录的方法

1.前台登录

直接使用laravel自带的auth

php artisan make:auth

然后可以查看路由文件:

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

执行php artisan migrate

会发现生成了两张表。

2.后台登录

编辑配置文件

config\auth.php

添加guards中的admin和providers中的admins

<?phpreturn [ 'defaults' => [ 'guard' => 'web', 'passwords' => 'users', ], 'guards' => [ 'web' => [ 'driver' => 'session', 'provider' => 'users', ], 'admin' => [ 'driver' => 'session', 'provider' => 'admins', ], 'api' => [ 'driver' => 'token', 'provider' => 'users', ], ], 'providers' => [ 'users' => [ 'driver' => 'eloquent', 'model' => App\User::class, ], 'admins' => [ 'driver' => 'eloquent', 'model' => App\Admin::class, ], ], 'passwords' => [ 'users' => [ 'provider' => 'users', 'email' => 'auth.emails.password', 'table' => 'password_resets', 'expire' => 60, ], ],];

3.创建数据模型

执行php artisan make:model Admin --migration

然后修改database\migrations中的数据表结构,复制user表的就可以

执行php artisan migrate 会发现生成了admin表

4.定义后台路由

这里我直接定义了一个路由组

Route::group(['prefix' => 'admin' ,'middleware' => 'admin'], function () { Route::get('login', 'Admin\AuthController@getLogin'); Route::post('login', 'Admin\AuthController@postLogin'); Route::get('register', 'Admin\AuthController@getRegister'); Route::post('register', 'Admin\AuthController@postRegister'); Route::get('logout', 'Admin\AuthController@logout'); Route::get('/', 'Admin\AdminController@index');});

5.创建控制器

执行

php artisan make:controller Admin/AuthControllerphp artisan make:controller Admin/AdminController

AuthController.php可参照Auth中的AuthController.php

<?phpnamespace App\Http\Controllers\Admin;use App\Admin;use Validator;use App\Http\Controllers\Controller;use Illuminate\Foundation\Auth\ThrottlesLogins;use Illuminate\Foundation\Auth\AuthenticatesAndRegistersUsers;class AuthController extends Controller{ use AuthenticatesAndRegistersUsers, ThrottlesLogins; protected $redirectTo = '/admin'; protected $guard = 'admin'; protected $loginView = 'admin.login'; protected $registerView = 'admin.register'; public function __construct() { $this->middleware('guest:admin', ['except' => 'logout']); } protected function validator(array $data) { return Validator::make($data, [ 'name' => 'required|max:255', 'email' => 'required|email|max:255|unique:admins', 'password' => 'required|confirmed|min:6', ]); } protected function create(array $data) { return Admin::create([ 'name' => $data['name'], 'email' => $data['email'], 'password' => bcrypt($data['password']), ]); }}

AdminController.php

<?phpnamespace App\Http\Controllers\Admin;use Illuminate\Http\Request;use App\Http\Requests;use App\Http\Controllers\Controller;use Auth;class AdminController extends Controller{ public function __construct() { $this->middleware('auth:admin'); } public function index() { $admin = Auth::guard('admin')->user(); return view('admin.home'); }}

6.创建视图

这里直接复制auth中的视图和home.blade.php到一个新的admin文件夹中。

修改login和register中表单的action,添加上admin前缀

修改layouts中的app.blade.php

<ul class="nav navbar-nav navbar-right"> <!-- Authentication Links --> @if (Auth::guest('admin')) <li><a href="{{ url('/login') }}">Login</a></li> <li><a href="{{ url('/register') }}">Register</a></li> @else <li class="dropdown"> <a href="#" class="dropdown-toggle" data-toggle="dropdown" role="button" aria-expanded="false"> {{ Auth::guard('admin')->user()->name }} <span class="caret"></span> </a> <ul class="dropdown-menu" role="menu"> <li><a href="{{ url('/admin/logout') }}"><i class="fa fa-btn fa-sign-out"></i>Logout</a></li> </ul> </li> @endif </ul>

现在试一下前后台分别登录吧!

7.可能遇到的页面跳转问题

如果你遇到了这种情况可以尝试修改一下Middleware\Authenticate.php

return redirect()->guest($guard.'/login');

以上所述是小编给大家介绍的laravel5.2实现区分前后台用户登录的方法,希望对大家有所帮助,如果大家有任何疑问请给我留言,小编会及时回复大家的。在此也非常感谢大家对脚本之家网站的支持!

laravel5.2实现区分前后台用户登录的方法

1.前台登录

直接使用laravel自带的auth

php artisan make:auth

然后可以查看路由文件:

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

执行php artisan migrate

会发现生成了两张表。

2.后台登录

编辑配置文件

config\auth.php

添加guards中的admin和providers中的admins

<?phpreturn [ 'defaults' => [ 'guard' => 'web', 'passwords' => 'users', ], 'guards' => [ 'web' => [ 'driver' => 'session', 'provider' => 'users', ], 'admin' => [ 'driver' => 'session', 'provider' => 'admins', ], 'api' => [ 'driver' => 'token', 'provider' => 'users', ], ], 'providers' => [ 'users' => [ 'driver' => 'eloquent', 'model' => App\User::class, ], 'admins' => [ 'driver' => 'eloquent', 'model' => App\Admin::class, ], ], 'passwords' => [ 'users' => [ 'provider' => 'users', 'email' => 'auth.emails.password', 'table' => 'password_resets', 'expire' => 60, ], ],];

3.创建数据模型

执行php artisan make:model Admin --migration

然后修改database\migrations中的数据表结构,复制user表的就可以

执行php artisan migrate 会发现生成了admin表

4.定义后台路由

这里我直接定义了一个路由组

Route::group(['prefix' => 'admin' ,'middleware' => 'admin'], function () { Route::get('login', 'Admin\AuthController@getLogin'); Route::post('login', 'Admin\AuthController@postLogin'); Route::get('register', 'Admin\AuthController@getRegister'); Route::post('register', 'Admin\AuthController@postRegister'); Route::get('logout', 'Admin\AuthController@logout'); Route::get('/', 'Admin\AdminController@index');});

5.创建控制器

执行

php artisan make:controller Admin/AuthControllerphp artisan make:controller Admin/AdminController

AuthController.php可参照Auth中的AuthController.php

<?phpnamespace App\Http\Controllers\Admin;use App\Admin;use Validator;use App\Http\Controllers\Controller;use Illuminate\Foundation\Auth\ThrottlesLogins;use Illuminate\Foundation\Auth\AuthenticatesAndRegistersUsers;class AuthController extends Controller{ use AuthenticatesAndRegistersUsers, ThrottlesLogins; protected $redirectTo = '/admin'; protected $guard = 'admin'; protected $loginView = 'admin.login'; protected $registerView = 'admin.register'; public function __construct() { $this->middleware('guest:admin', ['except' => 'logout']); } protected function validator(array $data) { return Validator::make($data, [ 'name' => 'required|max:255', 'email' => 'required|email|max:255|unique:admins', 'password' => 'required|confirmed|min:6', ]); } protected function create(array $data) { return Admin::create([ 'name' => $data['name'], 'email' => $data['email'], 'password' => bcrypt($data['password']), ]); }}

AdminController.php

<?phpnamespace App\Http\Controllers\Admin;use Illuminate\Http\Request;use App\Http\Requests;use App\Http\Controllers\Controller;use Auth;class AdminController extends Controller{ public function __construct() { $this->middleware('auth:admin'); } public function index() { $admin = Auth::guard('admin')->user(); return view('admin.home'); }}

6.创建视图

这里直接复制auth中的视图和home.blade.php到一个新的admin文件夹中。

修改login和register中表单的action,添加上admin前缀

修改layouts中的app.blade.php

<ul class="nav navbar-nav navbar-right"> <!-- Authentication Links --> @if (Auth::guest('admin')) <li><a href="{{ url('/login') }}">Login</a></li> <li><a href="{{ url('/register') }}">Register</a></li> @else <li class="dropdown"> <a href="#" class="dropdown-toggle" data-toggle="dropdown" role="button" aria-expanded="false"> {{ Auth::guard('admin')->user()->name }} <span class="caret"></span> </a> <ul class="dropdown-menu" role="menu"> <li><a href="{{ url('/admin/logout') }}"><i class="fa fa-btn fa-sign-out"></i>Logout</a></li> </ul> </li> @endif </ul>

现在试一下前后台分别登录吧!

7.可能遇到的页面跳转问题

如果你遇到了这种情况可以尝试修改一下Middleware\Authenticate.php

return redirect()->guest($guard.'/login');

以上所述是小编给大家介绍的laravel5.2实现区分前后台用户登录的方法,希望对大家有所帮助,如果大家有任何疑问请给我留言,小编会及时回复大家的。在此也非常感谢大家对脚本之家网站的支持!

laravel5.2实现区分前后台用户登录的方法

1.前台登录

直接使用laravel自带的auth

php artisan make:auth

然后可以查看路由文件:

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

执行php artisan migrate

会发现生成了两张表。

2.后台登录

编辑配置文件

config\auth.php

添加guards中的admin和providers中的admins

<?phpreturn [ 'defaults' => [ 'guard' => 'web', 'passwords' => 'users', ], 'guards' => [ 'web' => [ 'driver' => 'session', 'provider' => 'users', ], 'admin' => [ 'driver' => 'session', 'provider' => 'admins', ], 'api' => [ 'driver' => 'token', 'provider' => 'users', ], ], 'providers' => [ 'users' => [ 'driver' => 'eloquent', 'model' => App\User::class, ], 'admins' => [ 'driver' => 'eloquent', 'model' => App\Admin::class, ], ], 'passwords' => [ 'users' => [ 'provider' => 'users', 'email' => 'auth.emails.password', 'table' => 'password_resets', 'expire' => 60, ], ],];

3.创建数据模型

执行php artisan make:model Admin --migration

然后修改database\migrations中的数据表结构,复制user表的就可以

执行php artisan migrate 会发现生成了admin表

4.定义后台路由

这里我直接定义了一个路由组

Route::group(['prefix' => 'admin' ,'middleware' => 'admin'], function () { Route::get('login', 'Admin\AuthController@getLogin'); Route::post('login', 'Admin\AuthController@postLogin'); Route::get('register', 'Admin\AuthController@getRegister'); Route::post('register', 'Admin\AuthController@postRegister'); Route::get('logout', 'Admin\AuthController@logout'); Route::get('/', 'Admin\AdminController@index');});

5.创建控制器

执行

php artisan make:controller Admin/AuthControllerphp artisan make:controller Admin/AdminController

AuthController.php可参照Auth中的AuthController.php

<?phpnamespace App\Http\Controllers\Admin;use App\Admin;use Validator;use App\Http\Controllers\Controller;use Illuminate\Foundation\Auth\ThrottlesLogins;use Illuminate\Foundation\Auth\AuthenticatesAndRegistersUsers;class AuthController extends Controller{ use AuthenticatesAndRegistersUsers, ThrottlesLogins; protected $redirectTo = '/admin'; protected $guard = 'admin'; protected $loginView = 'admin.login'; protected $registerView = 'admin.register'; public function __construct() { $this->middleware('guest:admin', ['except' => 'logout']); } protected function validator(array $data) { return Validator::make($data, [ 'name' => 'required|max:255', 'email' => 'required|email|max:255|unique:admins', 'password' => 'required|confirmed|min:6', ]); } protected function create(array $data) { return Admin::create([ 'name' => $data['name'], 'email' => $data['email'], 'password' => bcrypt($data['password']), ]); }}

AdminController.php

<?phpnamespace App\Http\Controllers\Admin;use Illuminate\Http\Request;use App\Http\Requests;use App\Http\Controllers\Controller;use Auth;class AdminController extends Controller{ public function __construct() { $this->middleware('auth:admin'); } public function index() { $admin = Auth::guard('admin')->user(); return view('admin.home'); }}

6.创建视图

这里直接复制auth中的视图和home.blade.php到一个新的admin文件夹中。

修改login和register中表单的action,添加上admin前缀

修改layouts中的app.blade.php

<ul class="nav navbar-nav navbar-right"> <!-- Authentication Links --> @if (Auth::guest('admin')) <li><a href="{{ url('/login') }}">Login</a></li> <li><a href="{{ url('/register') }}">Register</a></li> @else <li class="dropdown"> <a href="#" class="dropdown-toggle" data-toggle="dropdown" role="button" aria-expanded="false"> {{ Auth::guard('admin')->user()->name }} <span class="caret"></span> </a> <ul class="dropdown-menu" role="menu"> <li><a href="{{ url('/admin/logout') }}"><i class="fa fa-btn fa-sign-out"></i>Logout</a></li> </ul> </li> @endif </ul>

现在试一下前后台分别登录吧!

7.可能遇到的页面跳转问题

如果你遇到了这种情况可以尝试修改一下Middleware\Authenticate.php

return redirect()->guest($guard.'/login');

以上所述是小编给大家介绍的laravel5.2实现区分前后台用户登录的方法,希望对大家有所帮助,如果大家有任何疑问请给我留言,小编会及时回复大家的。在此也非常感谢大家对脚本之家网站的支持!

1.前台登录

直接使用laravel自带的auth

php artisan make:auth

然后可以查看路由文件:

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

执行php artisan migrate

会发现生成了两张表。

2.后台登录

编辑配置文件

config\auth.php

添加guards中的admin和providers中的admins

<?phpreturn [ 'defaults' => [ 'guard' => 'web', 'passwords' => 'users', ], 'guards' => [ 'web' => [ 'driver' => 'session', 'provider' => 'users', ], 'admin' => [ 'driver' => 'session', 'provider' => 'admins', ], 'api' => [ 'driver' => 'token', 'provider' => 'users', ], ], 'providers' => [ 'users' => [ 'driver' => 'eloquent', 'model' => App\User::class, ], 'admins' => [ 'driver' => 'eloquent', 'model' => App\Admin::class, ], ], 'passwords' => [ 'users' => [ 'provider' => 'users', 'email' => 'auth.emails.password', 'table' => 'password_resets', 'expire' => 60, ], ],];

3.创建数据模型

执行php artisan make:model Admin --migration

然后修改database\migrations中的数据表结构,复制user表的就可以

执行php artisan migrate 会发现生成了admin表

4.定义后台路由

这里我直接定义了一个路由组

Route::group(['prefix' => 'admin' ,'middleware' => 'admin'], function () { Route::get('login', 'Admin\AuthController@getLogin'); Route::post('login', 'Admin\AuthController@postLogin'); Route::get('register', 'Admin\AuthController@getRegister'); Route::post('register', 'Admin\AuthController@postRegister'); Route::get('logout', 'Admin\AuthController@logout'); Route::get('/', 'Admin\AdminController@index');});

5.创建控制器

执行

php artisan make:controller Admin/AuthControllerphp artisan make:controller Admin/AdminController

AuthController.php可参照Auth中的AuthController.php

<?phpnamespace App\Http\Controllers\Admin;use App\Admin;use Validator;use App\Http\Controllers\Controller;use Illuminate\Foundation\Auth\ThrottlesLogins;use Illuminate\Foundation\Auth\AuthenticatesAndRegistersUsers;class AuthController extends Controller{ use AuthenticatesAndRegistersUsers, ThrottlesLogins; protected $redirectTo = '/admin'; protected $guard = 'admin'; protected $loginView = 'admin.login'; protected $registerView = 'admin.register'; public function __construct() { $this->middleware('guest:admin', ['except' => 'logout']); } protected function validator(array $data) { return Validator::make($data, [ 'name' => 'required|max:255', 'email' => 'required|email|max:255|unique:admins', 'password' => 'required|confirmed|min:6', ]); } protected function create(array $data) { return Admin::create([ 'name' => $data['name'], 'email' => $data['email'], 'password' => bcrypt($data['password']), ]); }}

AdminController.php

<?phpnamespace App\Http\Controllers\Admin;use Illuminate\Http\Request;use App\Http\Requests;use App\Http\Controllers\Controller;use Auth;class AdminController extends Controller{ public function __construct() { $this->middleware('auth:admin'); } public function index() { $admin = Auth::guard('admin')->user(); return view('admin.home'); }}

6.创建视图

这里直接复制auth中的视图和home.blade.php到一个新的admin文件夹中。

修改login和register中表单的action,添加上admin前缀

修改layouts中的app.blade.php

<ul class="nav navbar-nav navbar-right"> <!-- Authentication Links --> @if (Auth::guest('admin')) <li><a href="{{ url('/login') }}">Login</a></li> <li><a href="{{ url('/register') }}">Register</a></li> @else <li class="dropdown"> <a href="#" class="dropdown-toggle" data-toggle="dropdown" role="button" aria-expanded="false"> {{ Auth::guard('admin')->user()->name }} <span class="caret"></span> </a> <ul class="dropdown-menu" role="menu"> <li><a href="{{ url('/admin/logout') }}"><i class="fa fa-btn fa-sign-out"></i>Logout</a></li> </ul> </li> @endif </ul>

现在试一下前后台分别登录吧!

7.可能遇到的页面跳转问题

如果你遇到了这种情况可以尝试修改一下Middleware\Authenticate.php

return redirect()->guest($guard.'/login');

以上所述是小编给大家介绍的laravel5.2实现区分前后台用户登录的方法,希望对大家有所帮助,如果大家有任何疑问请给我留言,小编会及时回复大家的。在此也非常感谢大家对脚本之家网站的支持!

laravel5.2实现区分前后台用户登录的方法laravel5.2实现区分前后台用户登录的方法

1.前台登录

直接使用laravel自带的auth

php artisan make:auth

然后可以查看路由文件:

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

执行php artisan migrate

会发现生成了两张表。

2.后台登录

编辑配置文件

config\auth.php

添加guards中的admin和providers中的admins

<?phpreturn [ 'defaults' => [ 'guard' => 'web', 'passwords' => 'users', ], 'guards' => [ 'web' => [ 'driver' => 'session', 'provider' => 'users', ], 'admin' => [ 'driver' => 'session', 'provider' => 'admins', ], 'api' => [ 'driver' => 'token', 'provider' => 'users', ], ], 'providers' => [ 'users' => [ 'driver' => 'eloquent', 'model' => App\User::class, ], 'admins' => [ 'driver' => 'eloquent', 'model' => App\Admin::class, ], ], 'passwords' => [ 'users' => [ 'provider' => 'users', 'email' => 'auth.emails.password', 'table' => 'password_resets', 'expire' => 60, ], ],];

3.创建数据模型

执行php artisan make:model Admin --migration

然后修改database\migrations中的数据表结构,复制user表的就可以

执行php artisan migrate 会发现生成了admin表

4.定义后台路由

这里我直接定义了一个路由组

Route::group(['prefix' => 'admin' ,'middleware' => 'admin'], function () { Route::get('login', 'Admin\AuthController@getLogin'); Route::post('login', 'Admin\AuthController@postLogin'); Route::get('register', 'Admin\AuthController@getRegister'); Route::post('register', 'Admin\AuthController@postRegister'); Route::get('logout', 'Admin\AuthController@logout'); Route::get('/', 'Admin\AdminController@index');});

5.创建控制器

执行

php artisan make:controller Admin/AuthControllerphp artisan make:controller Admin/AdminController

AuthController.php可参照Auth中的AuthController.php

<?phpnamespace App\Http\Controllers\Admin;use App\Admin;use Validator;use App\Http\Controllers\Controller;use Illuminate\Foundation\Auth\ThrottlesLogins;use Illuminate\Foundation\Auth\AuthenticatesAndRegistersUsers;class AuthController extends Controller{ use AuthenticatesAndRegistersUsers, ThrottlesLogins; protected $redirectTo = '/admin'; protected $guard = 'admin'; protected $loginView = 'admin.login'; protected $registerView = 'admin.register'; public function __construct() { $this->middleware('guest:admin', ['except' => 'logout']); } protected function validator(array $data) { return Validator::make($data, [ 'name' => 'required|max:255', 'email' => 'required|email|max:255|unique:admins', 'password' => 'required|confirmed|min:6', ]); } protected function create(array $data) { return Admin::create([ 'name' => $data['name'], 'email' => $data['email'], 'password' => bcrypt($data['password']), ]); }}

AdminController.php

<?phpnamespace App\Http\Controllers\Admin;use Illuminate\Http\Request;use App\Http\Requests;use App\Http\Controllers\Controller;use Auth;class AdminController extends Controller{ public function __construct() { $this->middleware('auth:admin'); } public function index() { $admin = Auth::guard('admin')->user(); return view('admin.home'); }}

6.创建视图

这里直接复制auth中的视图和home.blade.php到一个新的admin文件夹中。

修改login和register中表单的action,添加上admin前缀

修改layouts中的app.blade.php

<ul class="nav navbar-nav navbar-right"> <!-- Authentication Links --> @if (Auth::guest('admin')) <li><a href="{{ url('/login') }}">Login</a></li> <li><a href="{{ url('/register') }}">Register</a></li> @else <li class="dropdown"> <a href="#" class="dropdown-toggle" data-toggle="dropdown" role="button" aria-expanded="false"> {{ Auth::guard('admin')->user()->name }} <span class="caret"></span> </a> <ul class="dropdown-menu" role="menu"> <li><a href="{{ url('/admin/logout') }}"><i class="fa fa-btn fa-sign-out"></i>Logout</a></li> </ul> </li> @endif </ul>

现在试一下前后台分别登录吧!

7.可能遇到的页面跳转问题

如果你遇到了这种情况可以尝试修改一下Middleware\Authenticate.php

return redirect()->guest($guard.'/login');

以上所述是小编给大家介绍的laravel5.2实现区分前后台用户登录的方法,希望对大家有所帮助,如果大家有任何疑问请给我留言,小编会及时回复大家的。在此也非常感谢大家对脚本之家网站的支持!

1.前台登录

直接使用laravel自带的auth

php artisan make:auth

然后可以查看路由文件:

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

执行php artisan migrate

会发现生成了两张表。

2.后台登录

编辑配置文件

config\auth.php

添加guards中的admin和providers中的admins

<?phpreturn [ 'defaults' => [ 'guard' => 'web', 'passwords' => 'users', ], 'guards' => [ 'web' => [ 'driver' => 'session', 'provider' => 'users', ], 'admin' => [ 'driver' => 'session', 'provider' => 'admins', ], 'api' => [ 'driver' => 'token', 'provider' => 'users', ], ], 'providers' => [ 'users' => [ 'driver' => 'eloquent', 'model' => App\User::class, ], 'admins' => [ 'driver' => 'eloquent', 'model' => App\Admin::class, ], ], 'passwords' => [ 'users' => [ 'provider' => 'users', 'email' => 'auth.emails.password', 'table' => 'password_resets', 'expire' => 60, ], ],];

3.创建数据模型

执行php artisan make:model Admin --migration

然后修改database\migrations中的数据表结构,复制user表的就可以

执行php artisan migrate 会发现生成了admin表

4.定义后台路由

这里我直接定义了一个路由组

Route::group(['prefix' => 'admin' ,'middleware' => 'admin'], function () { Route::get('login', 'Admin\AuthController@getLogin'); Route::post('login', 'Admin\AuthController@postLogin'); Route::get('register', 'Admin\AuthController@getRegister'); Route::post('register', 'Admin\AuthController@postRegister'); Route::get('logout', 'Admin\AuthController@logout'); Route::get('/', 'Admin\AdminController@index');});

5.创建控制器

执行

php artisan make:controller Admin/AuthControllerphp artisan make:controller Admin/AdminController

AuthController.php可参照Auth中的AuthController.php

<?phpnamespace App\Http\Controllers\Admin;use App\Admin;use Validator;use App\Http\Controllers\Controller;use Illuminate\Foundation\Auth\ThrottlesLogins;use Illuminate\Foundation\Auth\AuthenticatesAndRegistersUsers;class AuthController extends Controller{ use AuthenticatesAndRegistersUsers, ThrottlesLogins; protected $redirectTo = '/admin'; protected $guard = 'admin'; protected $loginView = 'admin.login'; protected $registerView = 'admin.register'; public function __construct() { $this->middleware('guest:admin', ['except' => 'logout']); } protected function validator(array $data) { return Validator::make($data, [ 'name' => 'required|max:255', 'email' => 'required|email|max:255|unique:admins', 'password' => 'required|confirmed|min:6', ]); } protected function create(array $data) { return Admin::create([ 'name' => $data['name'], 'email' => $data['email'], 'password' => bcrypt($data['password']), ]); }}

AdminController.php

<?phpnamespace App\Http\Controllers\Admin;use Illuminate\Http\Request;use App\Http\Requests;use App\Http\Controllers\Controller;use Auth;class AdminController extends Controller{ public function __construct() { $this->middleware('auth:admin'); } public function index() { $admin = Auth::guard('admin')->user(); return view('admin.home'); }}

6.创建视图

这里直接复制auth中的视图和home.blade.php到一个新的admin文件夹中。

修改login和register中表单的action,添加上admin前缀

修改layouts中的app.blade.php

<ul class="nav navbar-nav navbar-right"> <!-- Authentication Links --> @if (Auth::guest('admin')) <li><a href="{{ url('/login') }}">Login</a></li> <li><a href="{{ url('/register') }}">Register</a></li> @else <li class="dropdown"> <a href="#" class="dropdown-toggle" data-toggle="dropdown" role="button" aria-expanded="false"> {{ Auth::guard('admin')->user()->name }} <span class="caret"></span> </a> <ul class="dropdown-menu" role="menu"> <li><a href="{{ url('/admin/logout') }}"><i class="fa fa-btn fa-sign-out"></i>Logout</a></li> </ul> </li> @endif </ul>

现在试一下前后台分别登录吧!

7.可能遇到的页面跳转问题

如果你遇到了这种情况可以尝试修改一下Middleware\Authenticate.php

return redirect()->guest($guard.'/login');

以上所述是小编给大家介绍的laravel5.2实现区分前后台用户登录的方法,希望对大家有所帮助,如果大家有任何疑问请给我留言,小编会及时回复大家的。在此也非常感谢大家对脚本之家网站的支持!

laravel5.2实现区分前后台用户登录的方法

1.前台登录

直接使用laravel自带的auth

php artisan make:auth

然后可以查看路由文件:

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

执行php artisan migrate

会发现生成了两张表。

2.后台登录

编辑配置文件

config\auth.php

添加guards中的admin和providers中的admins

<?phpreturn [ 'defaults' => [ 'guard' => 'web', 'passwords' => 'users', ], 'guards' => [ 'web' => [ 'driver' => 'session', 'provider' => 'users', ], 'admin' => [ 'driver' => 'session', 'provider' => 'admins', ], 'api' => [ 'driver' => 'token', 'provider' => 'users', ], ], 'providers' => [ 'users' => [ 'driver' => 'eloquent', 'model' => App\User::class, ], 'admins' => [ 'driver' => 'eloquent', 'model' => App\Admin::class, ], ], 'passwords' => [ 'users' => [ 'provider' => 'users', 'email' => 'auth.emails.password', 'table' => 'password_resets', 'expire' => 60, ], ],];

3.创建数据模型

执行php artisan make:model Admin --migration

然后修改database\migrations中的数据表结构,复制user表的就可以

执行php artisan migrate 会发现生成了admin表

4.定义后台路由

这里我直接定义了一个路由组

Route::group(['prefix' => 'admin' ,'middleware' => 'admin'], function () { Route::get('login', 'Admin\AuthController@getLogin'); Route::post('login', 'Admin\AuthController@postLogin'); Route::get('register', 'Admin\AuthController@getRegister'); Route::post('register', 'Admin\AuthController@postRegister'); Route::get('logout', 'Admin\AuthController@logout'); Route::get('/', 'Admin\AdminController@index');});

5.创建控制器

执行

php artisan make:controller Admin/AuthControllerphp artisan make:controller Admin/AdminController

AuthController.php可参照Auth中的AuthController.php

<?phpnamespace App\Http\Controllers\Admin;use App\Admin;use Validator;use App\Http\Controllers\Controller;use Illuminate\Foundation\Auth\ThrottlesLogins;use Illuminate\Foundation\Auth\AuthenticatesAndRegistersUsers;class AuthController extends Controller{ use AuthenticatesAndRegistersUsers, ThrottlesLogins; protected $redirectTo = '/admin'; protected $guard = 'admin'; protected $loginView = 'admin.login'; protected $registerView = 'admin.register'; public function __construct() { $this->middleware('guest:admin', ['except' => 'logout']); } protected function validator(array $data) { return Validator::make($data, [ 'name' => 'required|max:255', 'email' => 'required|email|max:255|unique:admins', 'password' => 'required|confirmed|min:6', ]); } protected function create(array $data) { return Admin::create([ 'name' => $data['name'], 'email' => $data['email'], 'password' => bcrypt($data['password']), ]); }}

AdminController.php

<?phpnamespace App\Http\Controllers\Admin;use Illuminate\Http\Request;use App\Http\Requests;use App\Http\Controllers\Controller;use Auth;class AdminController extends Controller{ public function __construct() { $this->middleware('auth:admin'); } public function index() { $admin = Auth::guard('admin')->user(); return view('admin.home'); }}

6.创建视图

这里直接复制auth中的视图和home.blade.php到一个新的admin文件夹中。

修改login和register中表单的action,添加上admin前缀

修改layouts中的app.blade.php

<ul class="nav navbar-nav navbar-right"> <!-- Authentication Links --> @if (Auth::guest('admin')) <li><a href="{{ url('/login') }}">Login</a></li> <li><a href="{{ url('/register') }}">Register</a></li> @else <li class="dropdown"> <a href="#" class="dropdown-toggle" data-toggle="dropdown" role="button" aria-expanded="false"> {{ Auth::guard('admin')->user()->name }} <span class="caret"></span> </a> <ul class="dropdown-menu" role="menu"> <li><a href="{{ url('/admin/logout') }}"><i class="fa fa-btn fa-sign-out"></i>Logout</a></li> </ul> </li> @endif </ul>

现在试一下前后台分别登录吧!

7.可能遇到的页面跳转问题

如果你遇到了这种情况可以尝试修改一下Middleware\Authenticate.php

return redirect()->guest($guard.'/login');

以上所述是小编给大家介绍的laravel5.2实现区分前后台用户登录的方法,希望对大家有所帮助,如果大家有任何疑问请给我留言,小编会及时回复大家的。在此也非常感谢大家对脚本之家网站的支持!

1.前台登录

直接使用laravel自带的auth

php artisan make:auth

然后可以查看路由文件:

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

执行php artisan migrate

会发现生成了两张表。

2.后台登录

编辑配置文件

config\auth.php

添加guards中的admin和providers中的admins

<?phpreturn [ 'defaults' => [ 'guard' => 'web', 'passwords' => 'users', ], 'guards' => [ 'web' => [ 'driver' => 'session', 'provider' => 'users', ], 'admin' => [ 'driver' => 'session', 'provider' => 'admins', ], 'api' => [ 'driver' => 'token', 'provider' => 'users', ], ], 'providers' => [ 'users' => [ 'driver' => 'eloquent', 'model' => App\User::class, ], 'admins' => [ 'driver' => 'eloquent', 'model' => App\Admin::class, ], ], 'passwords' => [ 'users' => [ 'provider' => 'users', 'email' => 'auth.emails.password', 'table' => 'password_resets', 'expire' => 60, ], ],];

3.创建数据模型

执行php artisan make:model Admin --migration

然后修改database\migrations中的数据表结构,复制user表的就可以

执行php artisan migrate 会发现生成了admin表

4.定义后台路由

这里我直接定义了一个路由组

Route::group(['prefix' => 'admin' ,'middleware' => 'admin'], function () { Route::get('login', 'Admin\AuthController@getLogin'); Route::post('login', 'Admin\AuthController@postLogin'); Route::get('register', 'Admin\AuthController@getRegister'); Route::post('register', 'Admin\AuthController@postRegister'); Route::get('logout', 'Admin\AuthController@logout'); Route::get('/', 'Admin\AdminController@index');});

5.创建控制器

执行

php artisan make:controller Admin/AuthControllerphp artisan make:controller Admin/AdminController

AuthController.php可参照Auth中的AuthController.php

<?phpnamespace App\Http\Controllers\Admin;use App\Admin;use Validator;use App\Http\Controllers\Controller;use Illuminate\Foundation\Auth\ThrottlesLogins;use Illuminate\Foundation\Auth\AuthenticatesAndRegistersUsers;class AuthController extends Controller{ use AuthenticatesAndRegistersUsers, ThrottlesLogins; protected $redirectTo = '/admin'; protected $guard = 'admin'; protected $loginView = 'admin.login'; protected $registerView = 'admin.register'; public function __construct() { $this->middleware('guest:admin', ['except' => 'logout']); } protected function validator(array $data) { return Validator::make($data, [ 'name' => 'required|max:255', 'email' => 'required|email|max:255|unique:admins', 'password' => 'required|confirmed|min:6', ]); } protected function create(array $data) { return Admin::create([ 'name' => $data['name'], 'email' => $data['email'], 'password' => bcrypt($data['password']), ]); }}

AdminController.php

<?phpnamespace App\Http\Controllers\Admin;use Illuminate\Http\Request;use App\Http\Requests;use App\Http\Controllers\Controller;use Auth;class AdminController extends Controller{ public function __construct() { $this->middleware('auth:admin'); } public function index() { $admin = Auth::guard('admin')->user(); return view('admin.home'); }}

6.创建视图

这里直接复制auth中的视图和home.blade.php到一个新的admin文件夹中。

修改login和register中表单的action,添加上admin前缀

修改layouts中的app.blade.php

<ul class="nav navbar-nav navbar-right"> <!-- Authentication Links --> @if (Auth::guest('admin')) <li><a href="{{ url('/login') }}">Login</a></li> <li><a href="{{ url('/register') }}">Register</a></li> @else <li class="dropdown"> <a href="#" class="dropdown-toggle" data-toggle="dropdown" role="button" aria-expanded="false"> {{ Auth::guard('admin')->user()->name }} <span class="caret"></span> </a> <ul class="dropdown-menu" role="menu"> <li><a href="{{ url('/admin/logout') }}"><i class="fa fa-btn fa-sign-out"></i>Logout</a></li> </ul> </li> @endif </ul>

现在试一下前后台分别登录吧!

7.可能遇到的页面跳转问题

如果你遇到了这种情况可以尝试修改一下Middleware\Authenticate.php

return redirect()->guest($guard.'/login');

以上所述是小编给大家介绍的laravel5.2实现区分前后台用户登录的方法,希望对大家有所帮助,如果大家有任何疑问请给我留言,小编会及时回复大家的。在此也非常感谢大家对脚本之家网站的支持!

1.前台登录

直接使用laravel自带的auth

php artisan make:auth

然后可以查看路由文件:

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

执行php artisan migrate

会发现生成了两张表。

2.后台登录

编辑配置文件

config\auth.php

添加guards中的admin和providers中的admins

<?phpreturn [ 'defaults' => [ 'guard' => 'web', 'passwords' => 'users', ], 'guards' => [ 'web' => [ 'driver' => 'session', 'provider' => 'users', ], 'admin' => [ 'driver' => 'session', 'provider' => 'admins', ], 'api' => [ 'driver' => 'token', 'provider' => 'users', ], ], 'providers' => [ 'users' => [ 'driver' => 'eloquent', 'model' => App\User::class, ], 'admins' => [ 'driver' => 'eloquent', 'model' => App\Admin::class, ], ], 'passwords' => [ 'users' => [ 'provider' => 'users', 'email' => 'auth.emails.password', 'table' => 'password_resets', 'expire' => 60, ], ],];

3.创建数据模型

执行php artisan make:model Admin --migration

然后修改database\migrations中的数据表结构,复制user表的就可以

执行php artisan migrate 会发现生成了admin表

4.定义后台路由

这里我直接定义了一个路由组

Route::group(['prefix' => 'admin' ,'middleware' => 'admin'], function () { Route::get('login', 'Admin\AuthController@getLogin'); Route::post('login', 'Admin\AuthController@postLogin'); Route::get('register', 'Admin\AuthController@getRegister'); Route::post('register', 'Admin\AuthController@postRegister'); Route::get('logout', 'Admin\AuthController@logout'); Route::get('/', 'Admin\AdminController@index');});

5.创建控制器

执行

php artisan make:controller Admin/AuthControllerphp artisan make:controller Admin/AdminController

AuthController.php可参照Auth中的AuthController.php

<?phpnamespace App\Http\Controllers\Admin;use App\Admin;use Validator;use App\Http\Controllers\Controller;use Illuminate\Foundation\Auth\ThrottlesLogins;use Illuminate\Foundation\Auth\AuthenticatesAndRegistersUsers;class AuthController extends Controller{ use AuthenticatesAndRegistersUsers, ThrottlesLogins; protected $redirectTo = '/admin'; protected $guard = 'admin'; protected $loginView = 'admin.login'; protected $registerView = 'admin.register'; public function __construct() { $this->middleware('guest:admin', ['except' => 'logout']); } protected function validator(array $data) { return Validator::make($data, [ 'name' => 'required|max:255', 'email' => 'required|email|max:255|unique:admins', 'password' => 'required|confirmed|min:6', ]); } protected function create(array $data) { return Admin::create([ 'name' => $data['name'], 'email' => $data['email'], 'password' => bcrypt($data['password']), ]); }}

AdminController.php

<?phpnamespace App\Http\Controllers\Admin;use Illuminate\Http\Request;use App\Http\Requests;use App\Http\Controllers\Controller;use Auth;class AdminController extends Controller{ public function __construct() { $this->middleware('auth:admin'); } public function index() { $admin = Auth::guard('admin')->user(); return view('admin.home'); }}

6.创建视图

这里直接复制auth中的视图和home.blade.php到一个新的admin文件夹中。

修改login和register中表单的action,添加上admin前缀

修改layouts中的app.blade.php

<ul class="nav navbar-nav navbar-right"> <!-- Authentication Links --> @if (Auth::guest('admin')) <li><a href="{{ url('/login') }}">Login</a></li> <li><a href="{{ url('/register') }}">Register</a></li> @else <li class="dropdown"> <a href="#" class="dropdown-toggle" data-toggle="dropdown" role="button" aria-expanded="false"> {{ Auth::guard('admin')->user()->name }} <span class="caret"></span> </a> <ul class="dropdown-menu" role="menu"> <li><a href="{{ url('/admin/logout') }}"><i class="fa fa-btn fa-sign-out"></i>Logout</a></li> </ul> </li> @endif </ul>

现在试一下前后台分别登录吧!

7.可能遇到的页面跳转问题

如果你遇到了这种情况可以尝试修改一下Middleware\Authenticate.php

return redirect()->guest($guard.'/login');

以上所述是小编给大家介绍的laravel5.2实现区分前后台用户登录的方法,希望对大家有所帮助,如果大家有任何疑问请给我留言,小编会及时回复大家的。在此也非常感谢大家对脚本之家网站的支持!

2.laravel5.2实现区分前后台用户登录的方法。

1.前台登录

直接使用laravel自带的auth

php artisan make:auth

然后可以查看路由文件:

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

执行php artisan migrate

会发现生成了两张表。

2.后台登录

编辑配置文件

config\auth.php

添加guards中的admin和providers中的admins

<?phpreturn [ 'defaults' => [ 'guard' => 'web', 'passwords' => 'users', ], 'guards' => [ 'web' => [ 'driver' => 'session', 'provider' => 'users', ], 'admin' => [ 'driver' => 'session', 'provider' => 'admins', ], 'api' => [ 'driver' => 'token', 'provider' => 'users', ], ], 'providers' => [ 'users' => [ 'driver' => 'eloquent', 'model' => App\User::class, ], 'admins' => [ 'driver' => 'eloquent', 'model' => App\Admin::class, ], ], 'passwords' => [ 'users' => [ 'provider' => 'users', 'email' => 'auth.emails.password', 'table' => 'password_resets', 'expire' => 60, ], ],];

3.创建数据模型

执行php artisan make:model Admin --migration

然后修改database\migrations中的数据表结构,复制user表的就可以

执行php artisan migrate 会发现生成了admin表

4.定义后台路由

这里我直接定义了一个路由组

Route::group(['prefix' => 'admin' ,'middleware' => 'admin'], function () { Route::get('login', 'Admin\AuthController@getLogin'); Route::post('login', 'Admin\AuthController@postLogin'); Route::get('register', 'Admin\AuthController@getRegister'); Route::post('register', 'Admin\AuthController@postRegister'); Route::get('logout', 'Admin\AuthController@logout'); Route::get('/', 'Admin\AdminController@index');});

5.创建控制器

执行

php artisan make:controller Admin/AuthControllerphp artisan make:controller Admin/AdminController

AuthController.php可参照Auth中的AuthController.php

<?phpnamespace App\Http\Controllers\Admin;use App\Admin;use Validator;use App\Http\Controllers\Controller;use Illuminate\Foundation\Auth\ThrottlesLogins;use Illuminate\Foundation\Auth\AuthenticatesAndRegistersUsers;class AuthController extends Controller{ use AuthenticatesAndRegistersUsers, ThrottlesLogins; protected $redirectTo = '/admin'; protected $guard = 'admin'; protected $loginView = 'admin.login'; protected $registerView = 'admin.register'; public function __construct() { $this->middleware('guest:admin', ['except' => 'logout']); } protected function validator(array $data) { return Validator::make($data, [ 'name' => 'required|max:255', 'email' => 'required|email|max:255|unique:admins', 'password' => 'required|confirmed|min:6', ]); } protected function create(array $data) { return Admin::create([ 'name' => $data['name'], 'email' => $data['email'], 'password' => bcrypt($data['password']), ]); }}

AdminController.php

<?phpnamespace App\Http\Controllers\Admin;use Illuminate\Http\Request;use App\Http\Requests;use App\Http\Controllers\Controller;use Auth;class AdminController extends Controller{ public function __construct() { $this->middleware('auth:admin'); } public function index() { $admin = Auth::guard('admin')->user(); return view('admin.home'); }}

6.创建视图

这里直接复制auth中的视图和home.blade.php到一个新的admin文件夹中。

修改login和register中表单的action,添加上admin前缀

修改layouts中的app.blade.php

<ul class="nav navbar-nav navbar-right"> <!-- Authentication Links --> @if (Auth::guest('admin')) <li><a href="{{ url('/login') }}">Login</a></li> <li><a href="{{ url('/register') }}">Register</a></li> @else <li class="dropdown"> <a href="#" class="dropdown-toggle" data-toggle="dropdown" role="button" aria-expanded="false"> {{ Auth::guard('admin')->user()->name }} <span class="caret"></span> </a> <ul class="dropdown-menu" role="menu"> <li><a href="{{ url('/admin/logout') }}"><i class="fa fa-btn fa-sign-out"></i>Logout</a></li> </ul> </li> @endif </ul>

现在试一下前后台分别登录吧!

7.可能遇到的页面跳转问题

如果你遇到了这种情况可以尝试修改一下Middleware\Authenticate.php

return redirect()->guest($guard.'/login');

以上所述是小编给大家介绍的laravel5.2实现区分前后台用户登录的方法,希望对大家有所帮助,如果大家有任何疑问请给我留言,小编会及时回复大家的。在此也非常感谢大家对脚本之家网站的支持!

laravel5.2实现区分前后台用户登录的方法laravel5.2实现区分前后台用户登录的方法

1.前台登录

直接使用laravel自带的auth

php artisan make:auth

然后可以查看路由文件:

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

执行php artisan migrate

会发现生成了两张表。

2.后台登录

编辑配置文件

config\auth.php

添加guards中的admin和providers中的admins

<?phpreturn [ 'defaults' => [ 'guard' => 'web', 'passwords' => 'users', ], 'guards' => [ 'web' => [ 'driver' => 'session', 'provider' => 'users', ], 'admin' => [ 'driver' => 'session', 'provider' => 'admins', ], 'api' => [ 'driver' => 'token', 'provider' => 'users', ], ], 'providers' => [ 'users' => [ 'driver' => 'eloquent', 'model' => App\User::class, ], 'admins' => [ 'driver' => 'eloquent', 'model' => App\Admin::class, ], ], 'passwords' => [ 'users' => [ 'provider' => 'users', 'email' => 'auth.emails.password', 'table' => 'password_resets', 'expire' => 60, ], ],];

3.创建数据模型

执行php artisan make:model Admin --migration

然后修改database\migrations中的数据表结构,复制user表的就可以

执行php artisan migrate 会发现生成了admin表

4.定义后台路由

这里我直接定义了一个路由组

Route::group(['prefix' => 'admin' ,'middleware' => 'admin'], function () { Route::get('login', 'Admin\AuthController@getLogin'); Route::post('login', 'Admin\AuthController@postLogin'); Route::get('register', 'Admin\AuthController@getRegister'); Route::post('register', 'Admin\AuthController@postRegister'); Route::get('logout', 'Admin\AuthController@logout'); Route::get('/', 'Admin\AdminController@index');});

5.创建控制器

执行

php artisan make:controller Admin/AuthControllerphp artisan make:controller Admin/AdminController

AuthController.php可参照Auth中的AuthController.php

<?phpnamespace App\Http\Controllers\Admin;use App\Admin;use Validator;use App\Http\Controllers\Controller;use Illuminate\Foundation\Auth\ThrottlesLogins;use Illuminate\Foundation\Auth\AuthenticatesAndRegistersUsers;class AuthController extends Controller{ use AuthenticatesAndRegistersUsers, ThrottlesLogins; protected $redirectTo = '/admin'; protected $guard = 'admin'; protected $loginView = 'admin.login'; protected $registerView = 'admin.register'; public function __construct() { $this->middleware('guest:admin', ['except' => 'logout']); } protected function validator(array $data) { return Validator::make($data, [ 'name' => 'required|max:255', 'email' => 'required|email|max:255|unique:admins', 'password' => 'required|confirmed|min:6', ]); } protected function create(array $data) { return Admin::create([ 'name' => $data['name'], 'email' => $data['email'], 'password' => bcrypt($data['password']), ]); }}

AdminController.php

<?phpnamespace App\Http\Controllers\Admin;use Illuminate\Http\Request;use App\Http\Requests;use App\Http\Controllers\Controller;use Auth;class AdminController extends Controller{ public function __construct() { $this->middleware('auth:admin'); } public function index() { $admin = Auth::guard('admin')->user(); return view('admin.home'); }}

6.创建视图

这里直接复制auth中的视图和home.blade.php到一个新的admin文件夹中。

修改login和register中表单的action,添加上admin前缀

修改layouts中的app.blade.php

<ul class="nav navbar-nav navbar-right"> <!-- Authentication Links --> @if (Auth::guest('admin')) <li><a href="{{ url('/login') }}">Login</a></li> <li><a href="{{ url('/register') }}">Register</a></li> @else <li class="dropdown"> <a href="#" class="dropdown-toggle" data-toggle="dropdown" role="button" aria-expanded="false"> {{ Auth::guard('admin')->user()->name }} <span class="caret"></span> </a> <ul class="dropdown-menu" role="menu"> <li><a href="{{ url('/admin/logout') }}"><i class="fa fa-btn fa-sign-out"></i>Logout</a></li> </ul> </li> @endif </ul>

现在试一下前后台分别登录吧!

7.可能遇到的页面跳转问题

如果你遇到了这种情况可以尝试修改一下Middleware\Authenticate.php

return redirect()->guest($guard.'/login');

以上所述是小编给大家介绍的laravel5.2实现区分前后台用户登录的方法,希望对大家有所帮助,如果大家有任何疑问请给我留言,小编会及时回复大家的。在此也非常感谢大家对脚本之家网站的支持!

3.

1.前台登录

直接使用laravel自带的auth

php artisan make:auth

然后可以查看路由文件:

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

执行php artisan migrate

会发现生成了两张表。

2.后台登录

编辑配置文件

config\auth.php

添加guards中的admin和providers中的admins

<?phpreturn [ 'defaults' => [ 'guard' => 'web', 'passwords' => 'users', ], 'guards' => [ 'web' => [ 'driver' => 'session', 'provider' => 'users', ], 'admin' => [ 'driver' => 'session', 'provider' => 'admins', ], 'api' => [ 'driver' => 'token', 'provider' => 'users', ], ], 'providers' => [ 'users' => [ 'driver' => 'eloquent', 'model' => App\User::class, ], 'admins' => [ 'driver' => 'eloquent', 'model' => App\Admin::class, ], ], 'passwords' => [ 'users' => [ 'provider' => 'users', 'email' => 'auth.emails.password', 'table' => 'password_resets', 'expire' => 60, ], ],];

3.创建数据模型

执行php artisan make:model Admin --migration

然后修改database\migrations中的数据表结构,复制user表的就可以

执行php artisan migrate 会发现生成了admin表

4.定义后台路由

这里我直接定义了一个路由组

Route::group(['prefix' => 'admin' ,'middleware' => 'admin'], function () { Route::get('login', 'Admin\AuthController@getLogin'); Route::post('login', 'Admin\AuthController@postLogin'); Route::get('register', 'Admin\AuthController@getRegister'); Route::post('register', 'Admin\AuthController@postRegister'); Route::get('logout', 'Admin\AuthController@logout'); Route::get('/', 'Admin\AdminController@index');});

5.创建控制器

执行

php artisan make:controller Admin/AuthControllerphp artisan make:controller Admin/AdminController

AuthController.php可参照Auth中的AuthController.php

<?phpnamespace App\Http\Controllers\Admin;use App\Admin;use Validator;use App\Http\Controllers\Controller;use Illuminate\Foundation\Auth\ThrottlesLogins;use Illuminate\Foundation\Auth\AuthenticatesAndRegistersUsers;class AuthController extends Controller{ use AuthenticatesAndRegistersUsers, ThrottlesLogins; protected $redirectTo = '/admin'; protected $guard = 'admin'; protected $loginView = 'admin.login'; protected $registerView = 'admin.register'; public function __construct() { $this->middleware('guest:admin', ['except' => 'logout']); } protected function validator(array $data) { return Validator::make($data, [ 'name' => 'required|max:255', 'email' => 'required|email|max:255|unique:admins', 'password' => 'required|confirmed|min:6', ]); } protected function create(array $data) { return Admin::create([ 'name' => $data['name'], 'email' => $data['email'], 'password' => bcrypt($data['password']), ]); }}

AdminController.php

<?phpnamespace App\Http\Controllers\Admin;use Illuminate\Http\Request;use App\Http\Requests;use App\Http\Controllers\Controller;use Auth;class AdminController extends Controller{ public function __construct() { $this->middleware('auth:admin'); } public function index() { $admin = Auth::guard('admin')->user(); return view('admin.home'); }}

6.创建视图

这里直接复制auth中的视图和home.blade.php到一个新的admin文件夹中。

修改login和register中表单的action,添加上admin前缀

修改layouts中的app.blade.php

<ul class="nav navbar-nav navbar-right"> <!-- Authentication Links --> @if (Auth::guest('admin')) <li><a href="{{ url('/login') }}">Login</a></li> <li><a href="{{ url('/register') }}">Register</a></li> @else <li class="dropdown"> <a href="#" class="dropdown-toggle" data-toggle="dropdown" role="button" aria-expanded="false"> {{ Auth::guard('admin')->user()->name }} <span class="caret"></span> </a> <ul class="dropdown-menu" role="menu"> <li><a href="{{ url('/admin/logout') }}"><i class="fa fa-btn fa-sign-out"></i>Logout</a></li> </ul> </li> @endif </ul>

现在试一下前后台分别登录吧!

7.可能遇到的页面跳转问题

如果你遇到了这种情况可以尝试修改一下Middleware\Authenticate.php

return redirect()->guest($guard.'/login');

以上所述是小编给大家介绍的laravel5.2实现区分前后台用户登录的方法,希望对大家有所帮助,如果大家有任何疑问请给我留言,小编会及时回复大家的。在此也非常感谢大家对脚本之家网站的支持!

laravel5.2实现区分前后台用户登录的方法

1.前台登录

直接使用laravel自带的auth

php artisan make:auth

然后可以查看路由文件:

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

执行php artisan migrate

会发现生成了两张表。

2.后台登录

编辑配置文件

config\auth.php

添加guards中的admin和providers中的admins

<?phpreturn [ 'defaults' => [ 'guard' => 'web', 'passwords' => 'users', ], 'guards' => [ 'web' => [ 'driver' => 'session', 'provider' => 'users', ], 'admin' => [ 'driver' => 'session', 'provider' => 'admins', ], 'api' => [ 'driver' => 'token', 'provider' => 'users', ], ], 'providers' => [ 'users' => [ 'driver' => 'eloquent', 'model' => App\User::class, ], 'admins' => [ 'driver' => 'eloquent', 'model' => App\Admin::class, ], ], 'passwords' => [ 'users' => [ 'provider' => 'users', 'email' => 'auth.emails.password', 'table' => 'password_resets', 'expire' => 60, ], ],];

3.创建数据模型

执行php artisan make:model Admin --migration

然后修改database\migrations中的数据表结构,复制user表的就可以

执行php artisan migrate 会发现生成了admin表

4.定义后台路由

这里我直接定义了一个路由组

Route::group(['prefix' => 'admin' ,'middleware' => 'admin'], function () { Route::get('login', 'Admin\AuthController@getLogin'); Route::post('login', 'Admin\AuthController@postLogin'); Route::get('register', 'Admin\AuthController@getRegister'); Route::post('register', 'Admin\AuthController@postRegister'); Route::get('logout', 'Admin\AuthController@logout'); Route::get('/', 'Admin\AdminController@index');});

5.创建控制器

执行

php artisan make:controller Admin/AuthControllerphp artisan make:controller Admin/AdminController

AuthController.php可参照Auth中的AuthController.php

<?phpnamespace App\Http\Controllers\Admin;use App\Admin;use Validator;use App\Http\Controllers\Controller;use Illuminate\Foundation\Auth\ThrottlesLogins;use Illuminate\Foundation\Auth\AuthenticatesAndRegistersUsers;class AuthController extends Controller{ use AuthenticatesAndRegistersUsers, ThrottlesLogins; protected $redirectTo = '/admin'; protected $guard = 'admin'; protected $loginView = 'admin.login'; protected $registerView = 'admin.register'; public function __construct() { $this->middleware('guest:admin', ['except' => 'logout']); } protected function validator(array $data) { return Validator::make($data, [ 'name' => 'required|max:255', 'email' => 'required|email|max:255|unique:admins', 'password' => 'required|confirmed|min:6', ]); } protected function create(array $data) { return Admin::create([ 'name' => $data['name'], 'email' => $data['email'], 'password' => bcrypt($data['password']), ]); }}

AdminController.php

<?phpnamespace App\Http\Controllers\Admin;use Illuminate\Http\Request;use App\Http\Requests;use App\Http\Controllers\Controller;use Auth;class AdminController extends Controller{ public function __construct() { $this->middleware('auth:admin'); } public function index() { $admin = Auth::guard('admin')->user(); return view('admin.home'); }}

6.创建视图

这里直接复制auth中的视图和home.blade.php到一个新的admin文件夹中。

修改login和register中表单的action,添加上admin前缀

修改layouts中的app.blade.php

<ul class="nav navbar-nav navbar-right"> <!-- Authentication Links --> @if (Auth::guest('admin')) <li><a href="{{ url('/login') }}">Login</a></li> <li><a href="{{ url('/register') }}">Register</a></li> @else <li class="dropdown"> <a href="#" class="dropdown-toggle" data-toggle="dropdown" role="button" aria-expanded="false"> {{ Auth::guard('admin')->user()->name }} <span class="caret"></span> </a> <ul class="dropdown-menu" role="menu"> <li><a href="{{ url('/admin/logout') }}"><i class="fa fa-btn fa-sign-out"></i>Logout</a></li> </ul> </li> @endif </ul>

现在试一下前后台分别登录吧!

7.可能遇到的页面跳转问题

如果你遇到了这种情况可以尝试修改一下Middleware\Authenticate.php

return redirect()->guest($guard.'/login');

以上所述是小编给大家介绍的laravel5.2实现区分前后台用户登录的方法,希望对大家有所帮助,如果大家有任何疑问请给我留言,小编会及时回复大家的。在此也非常感谢大家对脚本之家网站的支持!

laravel5.2实现区分前后台用户登录的方法

1.前台登录

直接使用laravel自带的auth

php artisan make:auth

然后可以查看路由文件:

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

执行php artisan migrate

会发现生成了两张表。

2.后台登录

编辑配置文件

config\auth.php

添加guards中的admin和providers中的admins

<?phpreturn [ 'defaults' => [ 'guard' => 'web', 'passwords' => 'users', ], 'guards' => [ 'web' => [ 'driver' => 'session', 'provider' => 'users', ], 'admin' => [ 'driver' => 'session', 'provider' => 'admins', ], 'api' => [ 'driver' => 'token', 'provider' => 'users', ], ], 'providers' => [ 'users' => [ 'driver' => 'eloquent', 'model' => App\User::class, ], 'admins' => [ 'driver' => 'eloquent', 'model' => App\Admin::class, ], ], 'passwords' => [ 'users' => [ 'provider' => 'users', 'email' => 'auth.emails.password', 'table' => 'password_resets', 'expire' => 60, ], ],];

3.创建数据模型

执行php artisan make:model Admin --migration

然后修改database\migrations中的数据表结构,复制user表的就可以

执行php artisan migrate 会发现生成了admin表

4.定义后台路由

这里我直接定义了一个路由组

Route::group(['prefix' => 'admin' ,'middleware' => 'admin'], function () { Route::get('login', 'Admin\AuthController@getLogin'); Route::post('login', 'Admin\AuthController@postLogin'); Route::get('register', 'Admin\AuthController@getRegister'); Route::post('register', 'Admin\AuthController@postRegister'); Route::get('logout', 'Admin\AuthController@logout'); Route::get('/', 'Admin\AdminController@index');});

5.创建控制器

执行

php artisan make:controller Admin/AuthControllerphp artisan make:controller Admin/AdminController

AuthController.php可参照Auth中的AuthController.php

<?phpnamespace App\Http\Controllers\Admin;use App\Admin;use Validator;use App\Http\Controllers\Controller;use Illuminate\Foundation\Auth\ThrottlesLogins;use Illuminate\Foundation\Auth\AuthenticatesAndRegistersUsers;class AuthController extends Controller{ use AuthenticatesAndRegistersUsers, ThrottlesLogins; protected $redirectTo = '/admin'; protected $guard = 'admin'; protected $loginView = 'admin.login'; protected $registerView = 'admin.register'; public function __construct() { $this->middleware('guest:admin', ['except' => 'logout']); } protected function validator(array $data) { return Validator::make($data, [ 'name' => 'required|max:255', 'email' => 'required|email|max:255|unique:admins', 'password' => 'required|confirmed|min:6', ]); } protected function create(array $data) { return Admin::create([ 'name' => $data['name'], 'email' => $data['email'], 'password' => bcrypt($data['password']), ]); }}

AdminController.php

<?phpnamespace App\Http\Controllers\Admin;use Illuminate\Http\Request;use App\Http\Requests;use App\Http\Controllers\Controller;use Auth;class AdminController extends Controller{ public function __construct() { $this->middleware('auth:admin'); } public function index() { $admin = Auth::guard('admin')->user(); return view('admin.home'); }}

6.创建视图

这里直接复制auth中的视图和home.blade.php到一个新的admin文件夹中。

修改login和register中表单的action,添加上admin前缀

修改layouts中的app.blade.php

<ul class="nav navbar-nav navbar-right"> <!-- Authentication Links --> @if (Auth::guest('admin')) <li><a href="{{ url('/login') }}">Login</a></li> <li><a href="{{ url('/register') }}">Register</a></li> @else <li class="dropdown"> <a href="#" class="dropdown-toggle" data-toggle="dropdown" role="button" aria-expanded="false"> {{ Auth::guard('admin')->user()->name }} <span class="caret"></span> </a> <ul class="dropdown-menu" role="menu"> <li><a href="{{ url('/admin/logout') }}"><i class="fa fa-btn fa-sign-out"></i>Logout</a></li> </ul> </li> @endif </ul>

现在试一下前后台分别登录吧!

7.可能遇到的页面跳转问题

如果你遇到了这种情况可以尝试修改一下Middleware\Authenticate.php

return redirect()->guest($guard.'/login');

以上所述是小编给大家介绍的laravel5.2实现区分前后台用户登录的方法,希望对大家有所帮助,如果大家有任何疑问请给我留言,小编会及时回复大家的。在此也非常感谢大家对脚本之家网站的支持!

1.前台登录

直接使用laravel自带的auth

php artisan make:auth

然后可以查看路由文件:

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

执行php artisan migrate

会发现生成了两张表。

2.后台登录

编辑配置文件

config\auth.php

添加guards中的admin和providers中的admins

<?phpreturn [ 'defaults' => [ 'guard' => 'web', 'passwords' => 'users', ], 'guards' => [ 'web' => [ 'driver' => 'session', 'provider' => 'users', ], 'admin' => [ 'driver' => 'session', 'provider' => 'admins', ], 'api' => [ 'driver' => 'token', 'provider' => 'users', ], ], 'providers' => [ 'users' => [ 'driver' => 'eloquent', 'model' => App\User::class, ], 'admins' => [ 'driver' => 'eloquent', 'model' => App\Admin::class, ], ], 'passwords' => [ 'users' => [ 'provider' => 'users', 'email' => 'auth.emails.password', 'table' => 'password_resets', 'expire' => 60, ], ],];

3.创建数据模型

执行php artisan make:model Admin --migration

然后修改database\migrations中的数据表结构,复制user表的就可以

执行php artisan migrate 会发现生成了admin表

4.定义后台路由

这里我直接定义了一个路由组

Route::group(['prefix' => 'admin' ,'middleware' => 'admin'], function () { Route::get('login', 'Admin\AuthController@getLogin'); Route::post('login', 'Admin\AuthController@postLogin'); Route::get('register', 'Admin\AuthController@getRegister'); Route::post('register', 'Admin\AuthController@postRegister'); Route::get('logout', 'Admin\AuthController@logout'); Route::get('/', 'Admin\AdminController@index');});

5.创建控制器

执行

php artisan make:controller Admin/AuthControllerphp artisan make:controller Admin/AdminController

AuthController.php可参照Auth中的AuthController.php

<?phpnamespace App\Http\Controllers\Admin;use App\Admin;use Validator;use App\Http\Controllers\Controller;use Illuminate\Foundation\Auth\ThrottlesLogins;use Illuminate\Foundation\Auth\AuthenticatesAndRegistersUsers;class AuthController extends Controller{ use AuthenticatesAndRegistersUsers, ThrottlesLogins; protected $redirectTo = '/admin'; protected $guard = 'admin'; protected $loginView = 'admin.login'; protected $registerView = 'admin.register'; public function __construct() { $this->middleware('guest:admin', ['except' => 'logout']); } protected function validator(array $data) { return Validator::make($data, [ 'name' => 'required|max:255', 'email' => 'required|email|max:255|unique:admins', 'password' => 'required|confirmed|min:6', ]); } protected function create(array $data) { return Admin::create([ 'name' => $data['name'], 'email' => $data['email'], 'password' => bcrypt($data['password']), ]); }}

AdminController.php

<?phpnamespace App\Http\Controllers\Admin;use Illuminate\Http\Request;use App\Http\Requests;use App\Http\Controllers\Controller;use Auth;class AdminController extends Controller{ public function __construct() { $this->middleware('auth:admin'); } public function index() { $admin = Auth::guard('admin')->user(); return view('admin.home'); }}

6.创建视图

这里直接复制auth中的视图和home.blade.php到一个新的admin文件夹中。

修改login和register中表单的action,添加上admin前缀

修改layouts中的app.blade.php

<ul class="nav navbar-nav navbar-right"> <!-- Authentication Links --> @if (Auth::guest('admin')) <li><a href="{{ url('/login') }}">Login</a></li> <li><a href="{{ url('/register') }}">Register</a></li> @else <li class="dropdown"> <a href="#" class="dropdown-toggle" data-toggle="dropdown" role="button" aria-expanded="false"> {{ Auth::guard('admin')->user()->name }} <span class="caret"></span> </a> <ul class="dropdown-menu" role="menu"> <li><a href="{{ url('/admin/logout') }}"><i class="fa fa-btn fa-sign-out"></i>Logout</a></li> </ul> </li> @endif </ul>

现在试一下前后台分别登录吧!

7.可能遇到的页面跳转问题

如果你遇到了这种情况可以尝试修改一下Middleware\Authenticate.php

return redirect()->guest($guard.'/login');

以上所述是小编给大家介绍的laravel5.2实现区分前后台用户登录的方法,希望对大家有所帮助,如果大家有任何疑问请给我留言,小编会及时回复大家的。在此也非常感谢大家对脚本之家网站的支持!

laravel5.2实现区分前后台用户登录的方法laravel5.2实现区分前后台用户登录的方法

4.

1.前台登录

直接使用laravel自带的auth

php artisan make:auth

然后可以查看路由文件:

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

执行php artisan migrate

会发现生成了两张表。

2.后台登录

编辑配置文件

config\auth.php

添加guards中的admin和providers中的admins

<?phpreturn [ 'defaults' => [ 'guard' => 'web', 'passwords' => 'users', ], 'guards' => [ 'web' => [ 'driver' => 'session', 'provider' => 'users', ], 'admin' => [ 'driver' => 'session', 'provider' => 'admins', ], 'api' => [ 'driver' => 'token', 'provider' => 'users', ], ], 'providers' => [ 'users' => [ 'driver' => 'eloquent', 'model' => App\User::class, ], 'admins' => [ 'driver' => 'eloquent', 'model' => App\Admin::class, ], ], 'passwords' => [ 'users' => [ 'provider' => 'users', 'email' => 'auth.emails.password', 'table' => 'password_resets', 'expire' => 60, ], ],];

3.创建数据模型

执行php artisan make:model Admin --migration

然后修改database\migrations中的数据表结构,复制user表的就可以

执行php artisan migrate 会发现生成了admin表

4.定义后台路由

这里我直接定义了一个路由组

Route::group(['prefix' => 'admin' ,'middleware' => 'admin'], function () { Route::get('login', 'Admin\AuthController@getLogin'); Route::post('login', 'Admin\AuthController@postLogin'); Route::get('register', 'Admin\AuthController@getRegister'); Route::post('register', 'Admin\AuthController@postRegister'); Route::get('logout', 'Admin\AuthController@logout'); Route::get('/', 'Admin\AdminController@index');});

5.创建控制器

执行

php artisan make:controller Admin/AuthControllerphp artisan make:controller Admin/AdminController

AuthController.php可参照Auth中的AuthController.php

<?phpnamespace App\Http\Controllers\Admin;use App\Admin;use Validator;use App\Http\Controllers\Controller;use Illuminate\Foundation\Auth\ThrottlesLogins;use Illuminate\Foundation\Auth\AuthenticatesAndRegistersUsers;class AuthController extends Controller{ use AuthenticatesAndRegistersUsers, ThrottlesLogins; protected $redirectTo = '/admin'; protected $guard = 'admin'; protected $loginView = 'admin.login'; protected $registerView = 'admin.register'; public function __construct() { $this->middleware('guest:admin', ['except' => 'logout']); } protected function validator(array $data) { return Validator::make($data, [ 'name' => 'required|max:255', 'email' => 'required|email|max:255|unique:admins', 'password' => 'required|confirmed|min:6', ]); } protected function create(array $data) { return Admin::create([ 'name' => $data['name'], 'email' => $data['email'], 'password' => bcrypt($data['password']), ]); }}

AdminController.php

<?phpnamespace App\Http\Controllers\Admin;use Illuminate\Http\Request;use App\Http\Requests;use App\Http\Controllers\Controller;use Auth;class AdminController extends Controller{ public function __construct() { $this->middleware('auth:admin'); } public function index() { $admin = Auth::guard('admin')->user(); return view('admin.home'); }}

6.创建视图

这里直接复制auth中的视图和home.blade.php到一个新的admin文件夹中。

修改login和register中表单的action,添加上admin前缀

修改layouts中的app.blade.php

<ul class="nav navbar-nav navbar-right"> <!-- Authentication Links --> @if (Auth::guest('admin')) <li><a href="{{ url('/login') }}">Login</a></li> <li><a href="{{ url('/register') }}">Register</a></li> @else <li class="dropdown"> <a href="#" class="dropdown-toggle" data-toggle="dropdown" role="button" aria-expanded="false"> {{ Auth::guard('admin')->user()->name }} <span class="caret"></span> </a> <ul class="dropdown-menu" role="menu"> <li><a href="{{ url('/admin/logout') }}"><i class="fa fa-btn fa-sign-out"></i>Logout</a></li> </ul> </li> @endif </ul>

现在试一下前后台分别登录吧!

7.可能遇到的页面跳转问题

如果你遇到了这种情况可以尝试修改一下Middleware\Authenticate.php

return redirect()->guest($guard.'/login');

以上所述是小编给大家介绍的laravel5.2实现区分前后台用户登录的方法,希望对大家有所帮助,如果大家有任何疑问请给我留言,小编会及时回复大家的。在此也非常感谢大家对脚本之家网站的支持!

laravel5.2实现区分前后台用户登录的方法laravel5.2实现区分前后台用户登录的方法laravel5.2实现区分前后台用户登录的方法

1.前台登录

直接使用laravel自带的auth

php artisan make:auth

然后可以查看路由文件:

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

执行php artisan migrate

会发现生成了两张表。

2.后台登录

编辑配置文件

config\auth.php

添加guards中的admin和providers中的admins

<?phpreturn [ 'defaults' => [ 'guard' => 'web', 'passwords' => 'users', ], 'guards' => [ 'web' => [ 'driver' => 'session', 'provider' => 'users', ], 'admin' => [ 'driver' => 'session', 'provider' => 'admins', ], 'api' => [ 'driver' => 'token', 'provider' => 'users', ], ], 'providers' => [ 'users' => [ 'driver' => 'eloquent', 'model' => App\User::class, ], 'admins' => [ 'driver' => 'eloquent', 'model' => App\Admin::class, ], ], 'passwords' => [ 'users' => [ 'provider' => 'users', 'email' => 'auth.emails.password', 'table' => 'password_resets', 'expire' => 60, ], ],];

3.创建数据模型

执行php artisan make:model Admin --migration

然后修改database\migrations中的数据表结构,复制user表的就可以

执行php artisan migrate 会发现生成了admin表

4.定义后台路由

这里我直接定义了一个路由组

Route::group(['prefix' => 'admin' ,'middleware' => 'admin'], function () { Route::get('login', 'Admin\AuthController@getLogin'); Route::post('login', 'Admin\AuthController@postLogin'); Route::get('register', 'Admin\AuthController@getRegister'); Route::post('register', 'Admin\AuthController@postRegister'); Route::get('logout', 'Admin\AuthController@logout'); Route::get('/', 'Admin\AdminController@index');});

5.创建控制器

执行

php artisan make:controller Admin/AuthControllerphp artisan make:controller Admin/AdminController

AuthController.php可参照Auth中的AuthController.php

<?phpnamespace App\Http\Controllers\Admin;use App\Admin;use Validator;use App\Http\Controllers\Controller;use Illuminate\Foundation\Auth\ThrottlesLogins;use Illuminate\Foundation\Auth\AuthenticatesAndRegistersUsers;class AuthController extends Controller{ use AuthenticatesAndRegistersUsers, ThrottlesLogins; protected $redirectTo = '/admin'; protected $guard = 'admin'; protected $loginView = 'admin.login'; protected $registerView = 'admin.register'; public function __construct() { $this->middleware('guest:admin', ['except' => 'logout']); } protected function validator(array $data) { return Validator::make($data, [ 'name' => 'required|max:255', 'email' => 'required|email|max:255|unique:admins', 'password' => 'required|confirmed|min:6', ]); } protected function create(array $data) { return Admin::create([ 'name' => $data['name'], 'email' => $data['email'], 'password' => bcrypt($data['password']), ]); }}

AdminController.php

<?phpnamespace App\Http\Controllers\Admin;use Illuminate\Http\Request;use App\Http\Requests;use App\Http\Controllers\Controller;use Auth;class AdminController extends Controller{ public function __construct() { $this->middleware('auth:admin'); } public function index() { $admin = Auth::guard('admin')->user(); return view('admin.home'); }}

6.创建视图

这里直接复制auth中的视图和home.blade.php到一个新的admin文件夹中。

修改login和register中表单的action,添加上admin前缀

修改layouts中的app.blade.php

<ul class="nav navbar-nav navbar-right"> <!-- Authentication Links --> @if (Auth::guest('admin')) <li><a href="{{ url('/login') }}">Login</a></li> <li><a href="{{ url('/register') }}">Register</a></li> @else <li class="dropdown"> <a href="#" class="dropdown-toggle" data-toggle="dropdown" role="button" aria-expanded="false"> {{ Auth::guard('admin')->user()->name }} <span class="caret"></span> </a> <ul class="dropdown-menu" role="menu"> <li><a href="{{ url('/admin/logout') }}"><i class="fa fa-btn fa-sign-out"></i>Logout</a></li> </ul> </li> @endif </ul>

现在试一下前后台分别登录吧!

7.可能遇到的页面跳转问题

如果你遇到了这种情况可以尝试修改一下Middleware\Authenticate.php

return redirect()->guest($guard.'/login');

以上所述是小编给大家介绍的laravel5.2实现区分前后台用户登录的方法,希望对大家有所帮助,如果大家有任何疑问请给我留言,小编会及时回复大家的。在此也非常感谢大家对脚本之家网站的支持!

1.前台登录

直接使用laravel自带的auth

php artisan make:auth

然后可以查看路由文件:

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

执行php artisan migrate

会发现生成了两张表。

2.后台登录

编辑配置文件

config\auth.php

添加guards中的admin和providers中的admins

<?phpreturn [ 'defaults' => [ 'guard' => 'web', 'passwords' => 'users', ], 'guards' => [ 'web' => [ 'driver' => 'session', 'provider' => 'users', ], 'admin' => [ 'driver' => 'session', 'provider' => 'admins', ], 'api' => [ 'driver' => 'token', 'provider' => 'users', ], ], 'providers' => [ 'users' => [ 'driver' => 'eloquent', 'model' => App\User::class, ], 'admins' => [ 'driver' => 'eloquent', 'model' => App\Admin::class, ], ], 'passwords' => [ 'users' => [ 'provider' => 'users', 'email' => 'auth.emails.password', 'table' => 'password_resets', 'expire' => 60, ], ],];

3.创建数据模型

执行php artisan make:model Admin --migration

然后修改database\migrations中的数据表结构,复制user表的就可以

执行php artisan migrate 会发现生成了admin表

4.定义后台路由

这里我直接定义了一个路由组

Route::group(['prefix' => 'admin' ,'middleware' => 'admin'], function () { Route::get('login', 'Admin\AuthController@getLogin'); Route::post('login', 'Admin\AuthController@postLogin'); Route::get('register', 'Admin\AuthController@getRegister'); Route::post('register', 'Admin\AuthController@postRegister'); Route::get('logout', 'Admin\AuthController@logout'); Route::get('/', 'Admin\AdminController@index');});

5.创建控制器

执行

php artisan make:controller Admin/AuthControllerphp artisan make:controller Admin/AdminController

AuthController.php可参照Auth中的AuthController.php

<?phpnamespace App\Http\Controllers\Admin;use App\Admin;use Validator;use App\Http\Controllers\Controller;use Illuminate\Foundation\Auth\ThrottlesLogins;use Illuminate\Foundation\Auth\AuthenticatesAndRegistersUsers;class AuthController extends Controller{ use AuthenticatesAndRegistersUsers, ThrottlesLogins; protected $redirectTo = '/admin'; protected $guard = 'admin'; protected $loginView = 'admin.login'; protected $registerView = 'admin.register'; public function __construct() { $this->middleware('guest:admin', ['except' => 'logout']); } protected function validator(array $data) { return Validator::make($data, [ 'name' => 'required|max:255', 'email' => 'required|email|max:255|unique:admins', 'password' => 'required|confirmed|min:6', ]); } protected function create(array $data) { return Admin::create([ 'name' => $data['name'], 'email' => $data['email'], 'password' => bcrypt($data['password']), ]); }}

AdminController.php

<?phpnamespace App\Http\Controllers\Admin;use Illuminate\Http\Request;use App\Http\Requests;use App\Http\Controllers\Controller;use Auth;class AdminController extends Controller{ public function __construct() { $this->middleware('auth:admin'); } public function index() { $admin = Auth::guard('admin')->user(); return view('admin.home'); }}

6.创建视图

这里直接复制auth中的视图和home.blade.php到一个新的admin文件夹中。

修改login和register中表单的action,添加上admin前缀

修改layouts中的app.blade.php

<ul class="nav navbar-nav navbar-right"> <!-- Authentication Links --> @if (Auth::guest('admin')) <li><a href="{{ url('/login') }}">Login</a></li> <li><a href="{{ url('/register') }}">Register</a></li> @else <li class="dropdown"> <a href="#" class="dropdown-toggle" data-toggle="dropdown" role="button" aria-expanded="false"> {{ Auth::guard('admin')->user()->name }} <span class="caret"></span> </a> <ul class="dropdown-menu" role="menu"> <li><a href="{{ url('/admin/logout') }}"><i class="fa fa-btn fa-sign-out"></i>Logout</a></li> </ul> </li> @endif </ul>

现在试一下前后台分别登录吧!

7.可能遇到的页面跳转问题

如果你遇到了这种情况可以尝试修改一下Middleware\Authenticate.php

return redirect()->guest($guard.'/login');

以上所述是小编给大家介绍的laravel5.2实现区分前后台用户登录的方法,希望对大家有所帮助,如果大家有任何疑问请给我留言,小编会及时回复大家的。在此也非常感谢大家对脚本之家网站的支持!

laravel5.2实现区分前后台用户登录的方法laravel5.2实现区分前后台用户登录的方法laravel5.2实现区分前后台用户登录的方法laravel5.2实现区分前后台用户登录的方法

1.前台登录

直接使用laravel自带的auth

php artisan make:auth

然后可以查看路由文件:

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

执行php artisan migrate

会发现生成了两张表。

2.后台登录

编辑配置文件

config\auth.php

添加guards中的admin和providers中的admins

<?phpreturn [ 'defaults' => [ 'guard' => 'web', 'passwords' => 'users', ], 'guards' => [ 'web' => [ 'driver' => 'session', 'provider' => 'users', ], 'admin' => [ 'driver' => 'session', 'provider' => 'admins', ], 'api' => [ 'driver' => 'token', 'provider' => 'users', ], ], 'providers' => [ 'users' => [ 'driver' => 'eloquent', 'model' => App\User::class, ], 'admins' => [ 'driver' => 'eloquent', 'model' => App\Admin::class, ], ], 'passwords' => [ 'users' => [ 'provider' => 'users', 'email' => 'auth.emails.password', 'table' => 'password_resets', 'expire' => 60, ], ],];

3.创建数据模型

执行php artisan make:model Admin --migration

然后修改database\migrations中的数据表结构,复制user表的就可以

执行php artisan migrate 会发现生成了admin表

4.定义后台路由

这里我直接定义了一个路由组

Route::group(['prefix' => 'admin' ,'middleware' => 'admin'], function () { Route::get('login', 'Admin\AuthController@getLogin'); Route::post('login', 'Admin\AuthController@postLogin'); Route::get('register', 'Admin\AuthController@getRegister'); Route::post('register', 'Admin\AuthController@postRegister'); Route::get('logout', 'Admin\AuthController@logout'); Route::get('/', 'Admin\AdminController@index');});

5.创建控制器

执行

php artisan make:controller Admin/AuthControllerphp artisan make:controller Admin/AdminController

AuthController.php可参照Auth中的AuthController.php

<?phpnamespace App\Http\Controllers\Admin;use App\Admin;use Validator;use App\Http\Controllers\Controller;use Illuminate\Foundation\Auth\ThrottlesLogins;use Illuminate\Foundation\Auth\AuthenticatesAndRegistersUsers;class AuthController extends Controller{ use AuthenticatesAndRegistersUsers, ThrottlesLogins; protected $redirectTo = '/admin'; protected $guard = 'admin'; protected $loginView = 'admin.login'; protected $registerView = 'admin.register'; public function __construct() { $this->middleware('guest:admin', ['except' => 'logout']); } protected function validator(array $data) { return Validator::make($data, [ 'name' => 'required|max:255', 'email' => 'required|email|max:255|unique:admins', 'password' => 'required|confirmed|min:6', ]); } protected function create(array $data) { return Admin::create([ 'name' => $data['name'], 'email' => $data['email'], 'password' => bcrypt($data['password']), ]); }}

AdminController.php

<?phpnamespace App\Http\Controllers\Admin;use Illuminate\Http\Request;use App\Http\Requests;use App\Http\Controllers\Controller;use Auth;class AdminController extends Controller{ public function __construct() { $this->middleware('auth:admin'); } public function index() { $admin = Auth::guard('admin')->user(); return view('admin.home'); }}

6.创建视图

这里直接复制auth中的视图和home.blade.php到一个新的admin文件夹中。

修改login和register中表单的action,添加上admin前缀

修改layouts中的app.blade.php

<ul class="nav navbar-nav navbar-right"> <!-- Authentication Links --> @if (Auth::guest('admin')) <li><a href="{{ url('/login') }}">Login</a></li> <li><a href="{{ url('/register') }}">Register</a></li> @else <li class="dropdown"> <a href="#" class="dropdown-toggle" data-toggle="dropdown" role="button" aria-expanded="false"> {{ Auth::guard('admin')->user()->name }} <span class="caret"></span> </a> <ul class="dropdown-menu" role="menu"> <li><a href="{{ url('/admin/logout') }}"><i class="fa fa-btn fa-sign-out"></i>Logout</a></li> </ul> </li> @endif </ul>

现在试一下前后台分别登录吧!

7.可能遇到的页面跳转问题

如果你遇到了这种情况可以尝试修改一下Middleware\Authenticate.php

return redirect()->guest($guard.'/login');

以上所述是小编给大家介绍的laravel5.2实现区分前后台用户登录的方法,希望对大家有所帮助,如果大家有任何疑问请给我留言,小编会及时回复大家的。在此也非常感谢大家对脚本之家网站的支持!

laravel5.2实现区分前后台用户登录的方法。威尼斯电脑教程

展开全文
相关文章
w66利来国际手机app

1.前台登录

直接使用laravel自带的auth

php artisan make:auth

然后可以查看路由文件:

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

执行php artisan migrate

会发现生成了两张表。

2.后台登录

编辑配置文件

config\auth.php

添加guards中的admin和providers中的admins

<?phpreturn [ 'defaults' => [ 'guard' => 'web', 'passwords' => 'users', ], 'guards' => [ 'web' => [ 'driver' => 'session', 'provider' => 'users', ], 'admin' => [ 'driver' => 'session', 'provider' => 'admins', ], 'api' => [ 'driver' => 'token', 'provider' => 'users', ], ], 'providers' => [ 'users' => [ 'driver' => 'eloquent', 'model' => App\User::class, ], 'admins' => [ 'driver' => 'eloquent', 'model' => App\Admin::class, ], ], 'passwords' => [ 'users' => [ 'provider' => 'users', 'email' => 'auth.emails.password', 'table' => 'password_resets', 'expire' => 60, ], ],];

3.创建数据模型

执行php artisan make:model Admin --migration

然后修改database\migrations中的数据表结构,复制user表的就可以

执行php artisan migrate 会发现生成了admin表

4.定义后台路由

这里我直接定义了一个路由组

Route::group(['prefix' => 'admin' ,'middleware' => 'admin'], function () { Route::get('login', 'Admin\AuthController@getLogin'); Route::post('login', 'Admin\AuthController@postLogin'); Route::get('register', 'Admin\AuthController@getRegister'); Route::post('register', 'Admin\AuthController@postRegister'); Route::get('logout', 'Admin\AuthController@logout'); Route::get('/', 'Admin\AdminController@index');});

5.创建控制器

执行

php artisan make:controller Admin/AuthControllerphp artisan make:controller Admin/AdminController

AuthController.php可参照Auth中的AuthController.php

<?phpnamespace App\Http\Controllers\Admin;use App\Admin;use Validator;use App\Http\Controllers\Controller;use Illuminate\Foundation\Auth\ThrottlesLogins;use Illuminate\Foundation\Auth\AuthenticatesAndRegistersUsers;class AuthController extends Controller{ use AuthenticatesAndRegistersUsers, ThrottlesLogins; protected $redirectTo = '/admin'; protected $guard = 'admin'; protected $loginView = 'admin.login'; protected $registerView = 'admin.register'; public function __construct() { $this->middleware('guest:admin', ['except' => 'logout']); } protected function validator(array $data) { return Validator::make($data, [ 'name' => 'required|max:255', 'email' => 'required|email|max:255|unique:admins', 'password' => 'required|confirmed|min:6', ]); } protected function create(array $data) { return Admin::create([ 'name' => $data['name'], 'email' => $data['email'], 'password' => bcrypt($data['password']), ]); }}

AdminController.php

<?phpnamespace App\Http\Controllers\Admin;use Illuminate\Http\Request;use App\Http\Requests;use App\Http\Controllers\Controller;use Auth;class AdminController extends Controller{ public function __construct() { $this->middleware('auth:admin'); } public function index() { $admin = Auth::guard('admin')->user(); return view('admin.home'); }}

6.创建视图

这里直接复制auth中的视图和home.blade.php到一个新的admin文件夹中。

修改login和register中表单的action,添加上admin前缀

修改layouts中的app.blade.php

<ul class="nav navbar-nav navbar-right"> <!-- Authentication Links --> @if (Auth::guest('admin')) <li><a href="{{ url('/login') }}">Login</a></li> <li><a href="{{ url('/register') }}">Register</a></li> @else <li class="dropdown"> <a href="#" class="dropdown-toggle" data-toggle="dropdown" role="button" aria-expanded="false"> {{ Auth::guard('admin')->user()->name }} <span class="caret"></span> </a> <ul class="dropdown-menu" role="menu"> <li><a href="{{ url('/admin/logout') }}"><i class="fa fa-btn fa-sign-out"></i>Logout</a></li> </ul> </li> @endif </ul>

现在试一下前后台分别登录吧!

7.可能遇到的页面跳转问题

如果你遇到了这种情况可以尝试修改一下Middleware\Authenticate.php

return redirect()->guest($guard.'/login');

以上所述是小编给大家介绍的laravel5.2实现区分前后台用户登录的方法,希望对大家有所帮助,如果大家有任何疑问请给我留言,小编会及时回复大家的。在此也非常感谢大家对脚本之家网站的支持!

澳门石墨制品出售

1.前台登录

直接使用laravel自带的auth

php artisan make:auth

然后可以查看路由文件:

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

执行php artisan migrate

会发现生成了两张表。

2.后台登录

编辑配置文件

config\auth.php

添加guards中的admin和providers中的admins

<?phpreturn [ 'defaults' => [ 'guard' => 'web', 'passwords' => 'users', ], 'guards' => [ 'web' => [ 'driver' => 'session', 'provider' => 'users', ], 'admin' => [ 'driver' => 'session', 'provider' => 'admins', ], 'api' => [ 'driver' => 'token', 'provider' => 'users', ], ], 'providers' => [ 'users' => [ 'driver' => 'eloquent', 'model' => App\User::class, ], 'admins' => [ 'driver' => 'eloquent', 'model' => App\Admin::class, ], ], 'passwords' => [ 'users' => [ 'provider' => 'users', 'email' => 'auth.emails.password', 'table' => 'password_resets', 'expire' => 60, ], ],];

3.创建数据模型

执行php artisan make:model Admin --migration

然后修改database\migrations中的数据表结构,复制user表的就可以

执行php artisan migrate 会发现生成了admin表

4.定义后台路由

这里我直接定义了一个路由组

Route::group(['prefix' => 'admin' ,'middleware' => 'admin'], function () { Route::get('login', 'Admin\AuthController@getLogin'); Route::post('login', 'Admin\AuthController@postLogin'); Route::get('register', 'Admin\AuthController@getRegister'); Route::post('register', 'Admin\AuthController@postRegister'); Route::get('logout', 'Admin\AuthController@logout'); Route::get('/', 'Admin\AdminController@index');});

5.创建控制器

执行

php artisan make:controller Admin/AuthControllerphp artisan make:controller Admin/AdminController

AuthController.php可参照Auth中的AuthController.php

<?phpnamespace App\Http\Controllers\Admin;use App\Admin;use Validator;use App\Http\Controllers\Controller;use Illuminate\Foundation\Auth\ThrottlesLogins;use Illuminate\Foundation\Auth\AuthenticatesAndRegistersUsers;class AuthController extends Controller{ use AuthenticatesAndRegistersUsers, ThrottlesLogins; protected $redirectTo = '/admin'; protected $guard = 'admin'; protected $loginView = 'admin.login'; protected $registerView = 'admin.register'; public function __construct() { $this->middleware('guest:admin', ['except' => 'logout']); } protected function validator(array $data) { return Validator::make($data, [ 'name' => 'required|max:255', 'email' => 'required|email|max:255|unique:admins', 'password' => 'required|confirmed|min:6', ]); } protected function create(array $data) { return Admin::create([ 'name' => $data['name'], 'email' => $data['email'], 'password' => bcrypt($data['password']), ]); }}

AdminController.php

<?phpnamespace App\Http\Controllers\Admin;use Illuminate\Http\Request;use App\Http\Requests;use App\Http\Controllers\Controller;use Auth;class AdminController extends Controller{ public function __construct() { $this->middleware('auth:admin'); } public function index() { $admin = Auth::guard('admin')->user(); return view('admin.home'); }}

6.创建视图

这里直接复制auth中的视图和home.blade.php到一个新的admin文件夹中。

修改login和register中表单的action,添加上admin前缀

修改layouts中的app.blade.php

<ul class="nav navbar-nav navbar-right"> <!-- Authentication Links --> @if (Auth::guest('admin')) <li><a href="{{ url('/login') }}">Login</a></li> <li><a href="{{ url('/register') }}">Register</a></li> @else <li class="dropdown"> <a href="#" class="dropdown-toggle" data-toggle="dropdown" role="button" aria-expanded="false"> {{ Auth::guard('admin')->user()->name }} <span class="caret"></span> </a> <ul class="dropdown-menu" role="menu"> <li><a href="{{ url('/admin/logout') }}"><i class="fa fa-btn fa-sign-out"></i>Logout</a></li> </ul> </li> @endif </ul>

现在试一下前后台分别登录吧!

7.可能遇到的页面跳转问题

如果你遇到了这种情况可以尝试修改一下Middleware\Authenticate.php

return redirect()->guest($guard.'/login');

以上所述是小编给大家介绍的laravel5.2实现区分前后台用户登录的方法,希望对大家有所帮助,如果大家有任何疑问请给我留言,小编会及时回复大家的。在此也非常感谢大家对脚本之家网站的支持!

....

皇冠最新备用投注网址

laravel5.2实现区分前后台用户登录的方法....

皇冠

1.前台登录

直接使用laravel自带的auth

php artisan make:auth

然后可以查看路由文件:

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

执行php artisan migrate

会发现生成了两张表。

2.后台登录

编辑配置文件

config\auth.php

添加guards中的admin和providers中的admins

<?phpreturn [ 'defaults' => [ 'guard' => 'web', 'passwords' => 'users', ], 'guards' => [ 'web' => [ 'driver' => 'session', 'provider' => 'users', ], 'admin' => [ 'driver' => 'session', 'provider' => 'admins', ], 'api' => [ 'driver' => 'token', 'provider' => 'users', ], ], 'providers' => [ 'users' => [ 'driver' => 'eloquent', 'model' => App\User::class, ], 'admins' => [ 'driver' => 'eloquent', 'model' => App\Admin::class, ], ], 'passwords' => [ 'users' => [ 'provider' => 'users', 'email' => 'auth.emails.password', 'table' => 'password_resets', 'expire' => 60, ], ],];

3.创建数据模型

执行php artisan make:model Admin --migration

然后修改database\migrations中的数据表结构,复制user表的就可以

执行php artisan migrate 会发现生成了admin表

4.定义后台路由

这里我直接定义了一个路由组

Route::group(['prefix' => 'admin' ,'middleware' => 'admin'], function () { Route::get('login', 'Admin\AuthController@getLogin'); Route::post('login', 'Admin\AuthController@postLogin'); Route::get('register', 'Admin\AuthController@getRegister'); Route::post('register', 'Admin\AuthController@postRegister'); Route::get('logout', 'Admin\AuthController@logout'); Route::get('/', 'Admin\AdminController@index');});

5.创建控制器

执行

php artisan make:controller Admin/AuthControllerphp artisan make:controller Admin/AdminController

AuthController.php可参照Auth中的AuthController.php

<?phpnamespace App\Http\Controllers\Admin;use App\Admin;use Validator;use App\Http\Controllers\Controller;use Illuminate\Foundation\Auth\ThrottlesLogins;use Illuminate\Foundation\Auth\AuthenticatesAndRegistersUsers;class AuthController extends Controller{ use AuthenticatesAndRegistersUsers, ThrottlesLogins; protected $redirectTo = '/admin'; protected $guard = 'admin'; protected $loginView = 'admin.login'; protected $registerView = 'admin.register'; public function __construct() { $this->middleware('guest:admin', ['except' => 'logout']); } protected function validator(array $data) { return Validator::make($data, [ 'name' => 'required|max:255', 'email' => 'required|email|max:255|unique:admins', 'password' => 'required|confirmed|min:6', ]); } protected function create(array $data) { return Admin::create([ 'name' => $data['name'], 'email' => $data['email'], 'password' => bcrypt($data['password']), ]); }}

AdminController.php

<?phpnamespace App\Http\Controllers\Admin;use Illuminate\Http\Request;use App\Http\Requests;use App\Http\Controllers\Controller;use Auth;class AdminController extends Controller{ public function __construct() { $this->middleware('auth:admin'); } public function index() { $admin = Auth::guard('admin')->user(); return view('admin.home'); }}

6.创建视图

这里直接复制auth中的视图和home.blade.php到一个新的admin文件夹中。

修改login和register中表单的action,添加上admin前缀

修改layouts中的app.blade.php

<ul class="nav navbar-nav navbar-right"> <!-- Authentication Links --> @if (Auth::guest('admin')) <li><a href="{{ url('/login') }}">Login</a></li> <li><a href="{{ url('/register') }}">Register</a></li> @else <li class="dropdown"> <a href="#" class="dropdown-toggle" data-toggle="dropdown" role="button" aria-expanded="false"> {{ Auth::guard('admin')->user()->name }} <span class="caret"></span> </a> <ul class="dropdown-menu" role="menu"> <li><a href="{{ url('/admin/logout') }}"><i class="fa fa-btn fa-sign-out"></i>Logout</a></li> </ul> </li> @endif </ul>

现在试一下前后台分别登录吧!

7.可能遇到的页面跳转问题

如果你遇到了这种情况可以尝试修改一下Middleware\Authenticate.php

return redirect()->guest($guard.'/login');

以上所述是小编给大家介绍的laravel5.2实现区分前后台用户登录的方法,希望对大家有所帮助,如果大家有任何疑问请给我留言,小编会及时回复大家的。在此也非常感谢大家对脚本之家网站的支持!

....

沙龙娱乐场,沙龙国际

1.前台登录

直接使用laravel自带的auth

php artisan make:auth

然后可以查看路由文件:

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

执行php artisan migrate

会发现生成了两张表。

2.后台登录

编辑配置文件

config\auth.php

添加guards中的admin和providers中的admins

<?phpreturn [ 'defaults' => [ 'guard' => 'web', 'passwords' => 'users', ], 'guards' => [ 'web' => [ 'driver' => 'session', 'provider' => 'users', ], 'admin' => [ 'driver' => 'session', 'provider' => 'admins', ], 'api' => [ 'driver' => 'token', 'provider' => 'users', ], ], 'providers' => [ 'users' => [ 'driver' => 'eloquent', 'model' => App\User::class, ], 'admins' => [ 'driver' => 'eloquent', 'model' => App\Admin::class, ], ], 'passwords' => [ 'users' => [ 'provider' => 'users', 'email' => 'auth.emails.password', 'table' => 'password_resets', 'expire' => 60, ], ],];

3.创建数据模型

执行php artisan make:model Admin --migration

然后修改database\migrations中的数据表结构,复制user表的就可以

执行php artisan migrate 会发现生成了admin表

4.定义后台路由

这里我直接定义了一个路由组

Route::group(['prefix' => 'admin' ,'middleware' => 'admin'], function () { Route::get('login', 'Admin\AuthController@getLogin'); Route::post('login', 'Admin\AuthController@postLogin'); Route::get('register', 'Admin\AuthController@getRegister'); Route::post('register', 'Admin\AuthController@postRegister'); Route::get('logout', 'Admin\AuthController@logout'); Route::get('/', 'Admin\AdminController@index');});

5.创建控制器

执行

php artisan make:controller Admin/AuthControllerphp artisan make:controller Admin/AdminController

AuthController.php可参照Auth中的AuthController.php

<?phpnamespace App\Http\Controllers\Admin;use App\Admin;use Validator;use App\Http\Controllers\Controller;use Illuminate\Foundation\Auth\ThrottlesLogins;use Illuminate\Foundation\Auth\AuthenticatesAndRegistersUsers;class AuthController extends Controller{ use AuthenticatesAndRegistersUsers, ThrottlesLogins; protected $redirectTo = '/admin'; protected $guard = 'admin'; protected $loginView = 'admin.login'; protected $registerView = 'admin.register'; public function __construct() { $this->middleware('guest:admin', ['except' => 'logout']); } protected function validator(array $data) { return Validator::make($data, [ 'name' => 'required|max:255', 'email' => 'required|email|max:255|unique:admins', 'password' => 'required|confirmed|min:6', ]); } protected function create(array $data) { return Admin::create([ 'name' => $data['name'], 'email' => $data['email'], 'password' => bcrypt($data['password']), ]); }}

AdminController.php

<?phpnamespace App\Http\Controllers\Admin;use Illuminate\Http\Request;use App\Http\Requests;use App\Http\Controllers\Controller;use Auth;class AdminController extends Controller{ public function __construct() { $this->middleware('auth:admin'); } public function index() { $admin = Auth::guard('admin')->user(); return view('admin.home'); }}

6.创建视图

这里直接复制auth中的视图和home.blade.php到一个新的admin文件夹中。

修改login和register中表单的action,添加上admin前缀

修改layouts中的app.blade.php

<ul class="nav navbar-nav navbar-right"> <!-- Authentication Links --> @if (Auth::guest('admin')) <li><a href="{{ url('/login') }}">Login</a></li> <li><a href="{{ url('/register') }}">Register</a></li> @else <li class="dropdown"> <a href="#" class="dropdown-toggle" data-toggle="dropdown" role="button" aria-expanded="false"> {{ Auth::guard('admin')->user()->name }} <span class="caret"></span> </a> <ul class="dropdown-menu" role="menu"> <li><a href="{{ url('/admin/logout') }}"><i class="fa fa-btn fa-sign-out"></i>Logout</a></li> </ul> </li> @endif </ul>

现在试一下前后台分别登录吧!

7.可能遇到的页面跳转问题

如果你遇到了这种情况可以尝试修改一下Middleware\Authenticate.php

return redirect()->guest($guard.'/login');

以上所述是小编给大家介绍的laravel5.2实现区分前后台用户登录的方法,希望对大家有所帮助,如果大家有任何疑问请给我留言,小编会及时回复大家的。在此也非常感谢大家对脚本之家网站的支持!

....

相关资讯
热门资讯