【问题标题】:Laravel Middleware page authorityLaravel 中间件页面权限
【发布时间】:2020-03-24 22:51:34
【问题描述】:

我的想法是我想使用中间件将它们的角色过滤到我的ektp 路由中。因此只有已登录的用户才能访问ektp 页面,否则将其重定向到home 并提示“您需要登录”。

web.php

route::get('/ektp','PagesController@ektp');

我尝试添加->middleware(users) 就像我的登录和注册路线一样,所以它只会显示在客人身上。但是如果我这样做的话,我是如何得到一个错误Auth guard [users] is not defined. 的。 当我尝试在ektp model 中添加protected $guard ='user'; 时,它仍然显示相同的错误。

//register
route::get('/register','AuthController@getRegister')->middleware('guest');
route::any('/register','AuthController@postRegister')->middleware('guest');

//login
route::get('/login','AuthController@getLogin')->middleware('guest');
route::any('/login','AuthController@postLogin')->middleware('guest');

编辑

验证.php

<?php

namespace App\Http\Middleware;

use Illuminate\Auth\Middleware\Authenticate as Middleware;

class Authenticate extends Middleware
{
    /**
     * Get the path the user should be redirected to when they are not authenticated.
     *
     * @param  \Illuminate\Http\Request  $request
     * @return string
     */
    protected function redirectTo($request)
    {
        if (! $request->expectsJson()) {
            return route('home')->with('warning', 'Need to login');
        }


    }
}

【问题讨论】:

  • 这是auth中间件的用途,用户必须经过身份验证才能到达路由
  • @lagbox 和普通的中间件不一样?
  • 什么是“普通”中间件? ... auth 中间件是检查当前用户是否通过身份验证的中间件; laravel.com/docs/6.x/authentication#protecting-routes
  • 哇,谢谢,所以我需要为我的 ektp 创建新的控制器,所以除了 ektp 之外的其他路由没有被锁定。
  • 您不必通过控制器分配中间件,如果您这样做,您可以选择仅将其应用于某些方法或排除某些方法

标签: php laravel laravel-5 laravel-middleware


【解决方案1】:

如果您想向未登录的用户显示注册页面 很简单,只是不使用任何中间件或将该路由放在任何中间件组中

如果您只想向已登录的用户显示 ektp 页面,请这样做

Route::get('/ektp','PagesController@ektp')->middleware(['auth']);

Route::group(['middleware'=>'auth], function(){
  Route::get('/ektp','PagesController@ektp');
  // Others routes for auth middleware
});

【讨论】:

    猜你喜欢
    • 2016-12-17
    • 2014-06-03
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-09-11
    • 2011-11-15
    相关资源
    最近更新 更多