一, 后台 Admin 模块
后台管理是有管理员的, 甚至超级管理员, 所以在设计数据表的时候, 就会有 2 个方案, 一个方案是共用 users 数据表, 添加 is_admin,is_superAdmin 字段来进行验证, 或者将用户编到不同的组里面, 另一个方案是, 单独创建 admins 数据表来进行管理(这样前台和后台是两个事件, 前台用户是没有机会操作后台相关功能的, 也就是完全隔离了)
我采用第二种(实习快四个月了, 跟了 2 个完整项目是这样~)
我在上一篇说到, 我使用的是 https://github.com/houdunwang/laravel-module , 所以相关初始配置不再赘述.
使用 artisan hd:module Admin 后, 可访问后台界面 partTime.com/admin
二, 动态分层管理后台菜单
三, 创建 Guard 守卫实现前后台账号分离
1. 守卫 Guard 配置
请仔细看下面代码中的注释~
- <?PHP
- return [
- /*
- |--------------------------------------------------------------------------
- | Authentication Defaults
- |--------------------------------------------------------------------------
- |
- | This option controls the default authentication "guard" and password
- | reset options for your application. You may change these defaults
- | as required, but they're a perfect start for most applications.
- | defaults: 默认使用 web 守卫者
- | guard: 守卫者, 守卫 Web(也就是前台的所有操作)
- | passwords: 守卫者对应的数据表, 也即是根据哪一张来进行守卫
- |
- */
- 'defaults' => [
- 'guard' => 'web',
- 'passwords' => 'users',
- ],
- /*
- |--------------------------------------------------------------------------
- | Authentication Guards
- |--------------------------------------------------------------------------
- |
- | Next, you may define every authentication guard for your application.
- | Of course, a great default configuration has been defined for you
- | here which uses session storage and the Eloquent user provider.
- |
- | All authentication drivers have a user provider. This defines how the
- | users are actually retrieved out of your database or other storage
- | mechanisms used by this application to persist your user's data.
- |
- | Supported: "session", "token"
- | 这部分是定义守卫者
- | driver: 通过什么方式来检索用户, 要么 session 会话, 要么 token 令牌
- | provider: 对应的数据表
- |
- */
- 'guards' => [
- 'web' => [
- 'driver' => 'session',
- 'provider' => 'users',
- ],
- 'admin' => [
- 'driver' => 'session',
- 'provider' => 'admins',
- ],
- 'api' => [
- 'driver' => 'token',
- 'provider' => 'users',
- ],
- ],
- /*
- |--------------------------------------------------------------------------
- | User Providers
- |--------------------------------------------------------------------------
- |
- | All authentication drivers have a user provider. This defines how the
- | users are actually retrieved out of your database or other storage
- | mechanisms used by this application to persist your user's data.
- |
- | If you have multiple user tables or models you may configure multiple
- | sources which represent each model / table. These sources may then
- | be assigned to any extra authentication guards you have defined.
- |
- | Supported: "database", "eloquent"
- | 因为 Laravel 对数据表的操作都是通过模型 Model, 所以需要再此定义
- | Admin 模型定义: PHP artisan make:model Admin -fm(f 代表模型工厂, m 代表数据迁移表) 这种方式很方便, 比用一个一个去创建了~~ 不使用此方法的话, 万一表定义错了, 你还得告诉模型你使用的是拿一张表
- |
- */
- 'providers' => [
- 'users' => [
- 'driver' => 'eloquent',
- 'model' => App\User::class,
- ],
- 'admins' => [
- 'driver' => 'eloquent',
- 'model' => App\Admin::class,
- ],
- // 'users' => [make
- // 'driver' => 'database',
- // 'table' => 'users',
- // ],
- ],
- /*
- |--------------------------------------------------------------------------
- | Resetting Passwords
- |--------------------------------------------------------------------------
- |
- | You may specify multiple password reset configurations if you have more
- | than one user table or model in the application and you want to have
- | separate password reset settings based on the specific user types.
- |
- | The expire time is the number of minutes that the reset token should be
- | considered valid. This security feature keeps tokens short-lived so
- | they have Less time to be guessed. You may change this as needed.
- |
- */
- 'passwords' => [
- 'users' => [
- 'provider' => 'users',
- 'table' => 'password_resets',
- 'expire' => 60,
- ],
- ],
- ];
auth.PHP
注意:
此时创建出来的 Admin.PHP 如下, 其实是不对的哟, 往下看~
- <?PHP
- namespace App;
- use Illuminate\Database\Eloquent\Model;
- class Admin extends Model
- {
- //
- }
2. View,Controller 的配置
View 可以使用和前台一样的界面, 也可以自己修改界面, 无非就是个样式问题
Controller 和前台一样
所以如果一样的话, 直接复制粘贴, 修改控制器的 namespace 就好啦~
如果你使用了不同 UI, 却发现界面不对的话, 在 LoginController 添加以下方法
- // 这是指定显示哪一个界面
- public function showLoginForm()
- {
- return view('admin::auth.login');
- }
3. 路由配置
方法一:
- // middleware 中间件
- // prefix: 前缀 (可以使用 artisan route:list 查看) 比如: 前台登陆: http://parttime.com/login, 后台登陆: http://parttime.com/admin/login(其中的 admin 就是属性 prefix 指定的)
- Route::group(
- ['middleware' => 'web', 'prefix' => 'admin', 'namespace' => 'Modules\Admin\Http\Controllers'],
- function () {
- Auth::routes();
- }
- );
如果用这种方式的话, 那么 form 表单的 action 属性就得写成 action="/admin/login"
方法二:
- Route::group(
- ['middleware' => 'web', 'prefix' => 'admin', 'namespace' => 'Modules\Admin\Http\Controllers'],
- function () {
- Route::name('admin.')->group(
- function () {
- Auth::routes();
- }
- );
- }
- );
如果用这种方式的话, 那么 form 表单的 action 属性就得写成 action="{{route('admin.login')}}"
4. 两个错误
错误一: 邮箱不能为空
LoginController 中 use 了一个 AuthenticatesUsers, 它里面有那个方法 username()是指定验证的用户名是谁, 默认是 email, 我们可以对它进行重写
- public function username()
- {
- return 'name';
- }
错误二: 用户名密码错误
为了方便测试, 我们将 admins 表中的第一天数据进行了修改, 因此排除 Factory,AdminSeeder 的错误
其实 AuthenticatesUsers 还有一个 guard()方法, 它指定使用哪一个守卫者, 不传参默认使用 Web, 所以需要对它进行重写
- protected function guard()
- {
- return \Auth::guard('admin');
- }
错误三: 好多英文~(还记得三, 1 的注意嘛)
截至目前, Admin.PHP 中只是继承了 Model, 它只是实现了对表的一些操作, 比如增删改查等等, 并没有守卫者
那我们来看一下系统默认的 User.PHP 模型是怎么样的
- <?PHP
- namespace App;
- use Illuminate\Notifications\Notifiable;
- use Illuminate\Foundation\Auth\User as Authenticatable;
- class User extends Authenticatable
- {
- use Notifiable;
- /**
- * The attributes that are mass assignable.
- *
- * @var array
- */
- protected $fillable = [
- 'name', 'email', 'password',
- ];
- /**
- * The attributes that should be hidden for arrays.
- *
- * @var array
- */
- protected $hidden = [
- 'password', 'remember_token',
- ];
- }
User.PHP
我们可以模仿它来改造我们的 Admin.PHP 模型
- <?PHP
- namespace App;
- use Illuminate\Notifications\Notifiable;
- use Illuminate\Foundation\Auth\User as Authenticatable;
- class Admin extends Authenticatable
- {
- use Notifiable;
- //
- }
搞定, 后台也可以登陆啦~ 然后你又会发现一个问题, 为啥跳转到了前台登陆界面
错误四: 跳转错误
其实这里暗藏了两个问题: 1. 为什么跳转错误(肯定是和后台相关的一些配置出了问题)2. 为什么跳转到的是前台的登陆界面
咱们先解决第 2 个问题:
这是前台路由配置, Laravel 有中间件机制, 当前台用户没有登陆的时候, 他访问了网址, 就会自动跳转到登陆界面, 而控制这个 Controller 是前台的 HomeController.PHP
- <?PHP
- /*
- |--------------------------------------------------------------------------
- | Web Routes
- |--------------------------------------------------------------------------
- |
- | Here is where you can register Web routes for your application. These
- | routes are loaded by the RouteServiceProvider within a group which
- | contains the "web" middleware group. Now create something great!
- |
- */
- // 主页面
- Route::get(
- '/',
- function () {
- return view('welcome');
- }
- );
- // 前台登陆, 注册, 找回密码
- Auth::routes();
- Route::get('/home', 'HomeController@index')->name('home');
Web.PHP
- <?PHP
- namespace App\Http\Controllers;
- class HomeController extends Controller
- {
- /**
- * Create a new controller instance.
- *
- * @return void
- */
- public function __construct()
- {
- $this->middleware('auth');
- }
- /**
- * Show the application dashboard.
- *
- * @return \Illuminate\Http\Response
- */
- public function index()
- {
- return view('home');
- }
- }
HomeController.PHP
再解决第一个问题:
很简单, 再 Admin 模块的 LoginController.PHP 中, 有一个 $redirectTo 属性就是控制跳转的~
补充:
无论前台还是后台, 用户登陆成功了以后, 再通过地址栏访问登陆界面是不应该出现它的, 所以中间件的魅力就可以凸显~
四, 中间件设置前后台跳转不同路由
中间件有 Laravel 已经配置好的(App/Http/Kernel.PHP), 也可以自己自定义
前后台的 LoginController.PHP 的构造方法如下
- public function __construct()
- {
- $this->middleware('guest')->except('logout');// except: 指定的方法或方法组不进行验证
- }
guest 中间件是对游客的限制, 那么就会有一个问题: 如何指定这个有可能即将登陆的游客是什么角色呢, 是前台用户, 还是后台管理员? 所以 guard 守卫者再次派上用场
middleware('guest:admin')当不指定 admin 的时候默认采用 Web(前台)守卫者, 否则采用指定的守卫者(这里是 admin 守卫者)
那么又产生一个问题: 我验证成功了, 如何针对不同角色的用户实现不同的跳转呢?
下面是 guest 中间件所对应的类实现的方法(暂时忽略 $next, 这个和设计模式有关, 现在还用不到)
逻辑很简单, 判断是否有传进来的守卫者, 没有就用默认的 Web, 然后进行跳转 / home(问题就在这里: 不管前台还是后台都是跳转 / home, 显然是不对的, 因此需要对其进行修改)
- <?PHP
- namespace App\Http\Middleware;
- use Closure;
- use Illuminate\Support\Facades\Auth;
- class RedirectIfAuthenticated
- {
- /**
- * Handle an incoming request.
- *
- * @param \Illuminate\Http\Request $request
- * @param \Closure $next
- * @param string|null $guard
- * @return mixed
- */
- public function handle($request, Closure $next, $guard = null)
- {
- if (Auth::guard($guard)->check()) {
- return redirect('/home');
- }
- return $next($request);
- }
- }
修改后:
- <?PHP
- namespace App\Http\Middleware;
- use Closure;
- use Illuminate\Support\Facades\Auth;
- class RedirectIfAuthenticated
- {
- /**
- * Handle an incoming request.
- *
- * @param \Illuminate\Http\Request $request
- * @param \Closure $next
- * @param string|null $guard
- *
- * @return mixed
- */
- public function handle($request, Closure $next, $guard = null)
- {
- if (Auth::guard($guard)->check()) {
- // 可以自行决定跳转到哪里~
- return redirect($guard == 'admin' ? '/admin' : '/home');
- }
- return $next($request);
- }
- }
- AuthenticatesUsers
Laravel 系列之 CMS 系统学习 - 角色, 权限配置[1]
来源: http://www.bubuko.com/infodetail-2872243.html