laravel框架中间件 except 和 only 的用法示例

本文实例讲述了laravel框架中间件 except 和 only 的用法。分享给大家供大家参考,具体如下:


except


except:为黑名单机制,除了show页面不经过中间件Auth过滤,其他都需要过滤,如果没有通过验证,则跳转到指定的页面


only


only:为白名单机制,除了edit页面需要经过中间件Auth过滤,其他都不需要过滤,如果没有通过验证,则跳转到指定的页面


except用法:

class UserController extends Controller
{
  public function __construct()
  {
    $this->middleware('auth', ['except' => 'show']);
   }
  public function show(User $user)
  {
    return view('users.show', compact('user'));
  }
 public function edit(User $user)
  {
     return view('users.edit', compact('user'));
  }
}

except:为黑名单机制,除了show页面不经过中间件Auth过滤,其他都需要过滤,如果没有通过验证,则跳转到指定的页面


only用法:

class UserController extends Controller
{
  public function __construct()
  {
    $this->middleware('auth', ['only' => 'edit']);
   }
  public function show(User $user)
  {
    return view('users.show', compact('user'));
  }
 public function edit(User $user)
  {
     return view('users.edit', compact('user'));
  }
}

only:为白名单机制,除了edit页面需要经过中间件Auth过滤,其他都不需要过滤,如果没有通过验证,则跳转到指定的页面

更多关于Laravel相关内容感兴趣的读者可查看本站专题:《
》、《
》、《
》、《
》及《

希望本文所述对大家基于Laravel框架的PHP程序设计有所帮助。


您可能感兴趣的文章:

赞(0) 打赏

觉得文章有用就打赏一下文章作者

支付宝扫一扫打赏

微信扫一扫打赏