Laravel 中間件“僅”為每條路線觸發 (Laravel middlware 'only' fires for every route)


問題描述

Laravel 中間件“僅”為每條路線觸發 (Laravel middlware 'only' fires for every route)

無論我做什麼 crud middlware 總是被解僱。但是,只有在聲明了 $crud 數組並且只針對它包含的路由時才應該觸發它。但是,並非每次都會觸發。即使我說 $crud = []; 但是如果我聲明 ['only' => ['route1', 'route2']] 然後它按預期工作。

<?php

class BaseController extends Controller
{
    /**
     * Routes which DO NOT load users notifications.
     * @var Array Routes without notifications.
     */
    public $notifications;
    /**
     * Routes which DONT require users account to be configured.
     * @var Array Routes needing configuration.
     */
    public $configured;
    /**
     * Routes which REQUIRE ownership of resource.
     * @var Array CRUD routes.
     */
    public $crud;

    public function __construct()
    {
        $this‑>middleware('auth', ['except' => $this‑>routes]);
        $this‑>middleware('configured', ['except' => $this‑>configured]);
        $this‑>middleware('notifications', ['except' => $this‑>notifications]);
        $this‑>middleware('crud', ['only' => $this‑>crud]);
    }
}

參考解法

方法 1:

Looking at Laravel code it seems that when you use:

$this‑>middleware('crud', ['only' => []]);

Laravel will always use this middleware (for all Controller methods) so you should not middleware with empty only option.

So you should modify this contructor:

public function __construct()
{
    $this‑>middleware('auth', ['except' => $this‑>routes]);
    $this‑>middleware('configured', ['except' => $this‑>configured]);
    $this‑>middleware('notifications', ['except' => $this‑>notifications]);
    if ($this‑>crud) {
        $this‑>middleware('crud', ['only' => $this‑>crud]);
    }
}

and in child controllers that extend from BaseController you should do something like this in constructor:

public function __construct() {
   // here you set values for properties
   $this‑>routes = ['a','b'];
   $this‑>configured = ['c'];
   $this‑>notifications = ['d'];
   $this‑>crud = ['e','f'];

   // here you run parent contructor
   parent::__construct();
}

(by Sterling DuchessMarcin Nabiałek)

參考文件

  1. Laravel middlware 'only' fires for every route (CC BY‑SA 2.5/3.0/4.0)

#laravel-middleware #laravel-5 #Laravel #PHP #laravel-routing






相關問題

使用中間件進行表操作 laravel 5.1 (Using middleware for table manipulation laravel 5.1)

Laravel 中間件“僅”為每條路線觸發 (Laravel middlware 'only' fires for every route)

在 laravel 5.1 中檢查管理員角色 (Checking admin roles in laravel 5.1)

在laravel 5中使用中間件重定向循環 (redirect loop with middleware in laravel 5)

Laravel - 中間件後清空 $request (Laravel - Empty $request after middleware)

$request->user()->role 錯誤 - 試圖獲取非對象的屬性 ($request->user()->role errror - trying to get property of non-object)

檢測到 Laravel 中間件但未執行 (Laravel middleware detected but not executed)

將 Auth 中間件應用於所有 Laravel 路由 (Apply Auth Middleware to All Laravel Routes)

路由組內的功能 [laravel-passport] (Function inside route group [laravel-passport])

當我嘗試訪問主頁時,Laravel 不會重定向到登錄頁面 (Laravel does not redirect to login page when I try to access to home page)

控制器沒有來自中間件的更改請求 (Controller doesnt have the alter request from the middleware)

訂閱者中間件路由允許公眾查看所有受限頁面 (Subscriber middleware route allowing public to view all restricted pages)







留言討論