Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Can't specify middleware within a controller class as described in the documentation #54181

Closed
RomainMazB opened this issue Jan 14, 2025 · 4 comments · Fixed by laravel/docs#10123

Comments

@RomainMazB
Copy link

RomainMazB commented Jan 14, 2025

Laravel Version

11.37

PHP Version

8.2

Database Driver & Version

No response

Description

The documentation states:

Or, you may find it convenient to specify middleware within your controller class. To do so, your controller should implement the HasMiddleware interface, which dictates that the controller should have a static middleware method. From this method, you may return an array of middleware that should be applied to the controller's actions:

<?php

namespace App\Http\Controllers;

use App\Http\Controllers\Controller;
use Illuminate\Routing\Controllers\HasMiddleware;
use Illuminate\Routing\Controllers\Middleware;

class UserController extends Controller implements HasMiddleware
{
    /**
     * Get the middleware that should be assigned to the controller.
     */
    public static function middleware(): array
    {
        return [
            'auth',
            new Middleware('log', only: ['index']),
            new Middleware('subscribed', except: ['store']),
        ];
    }

But this code is purely not valid because we can't use the HasMiddleware trait on the Controller class.
The Controller's middleware method is not static:

public function middleware($middleware, array $options = [])

While the trait's method is:
public static function middleware();

The resulting error is this one:

Declaration of 'Controller->middleware(middleware: array|\Closure|string, [options: array = [...]])' must be compatible with 'HasMiddleware::middleware()', cannot make interface method non-static

Steps To Reproduce

Create a blank project
Create a controller
Use the HasMiddleware trait on the controller's class

@RomainMazB RomainMazB changed the title Can't specify middleware within a controller class Can't specify middleware within a controller class as described in the documentation Jan 14, 2025
@crynobone
Copy link
Member

Hey there, thanks for reporting this issue.

We'll need more info and/or code to debug this further. Can you please create a repository with the command below, commit the code that reproduces the issue as one separate commit on the main/master branch and share the repository here?

Please make sure that you have the latest version of the Laravel installer in order to run this command. Please also make sure you have both Git & the GitHub CLI tool properly set up.

laravel new bug-report --github="--public"

Do not amend and create a separate commit with your custom changes. After you've posted the repository, we'll try to reproduce the issue.

Thanks!

@MMMahmoodian
Copy link

MMMahmoodian commented Jan 14, 2025

I can confirm this. This is caused because the definition of the controller in the tests missed extends Controller. It actually never worked.

This is the link to the repo to reproduce the issue

I opened a PR to fix the test. I'm open to fixing this if you give me the green light.
I suggest to change the name of HasMiddleware::middleware to setMiddleware to fix this conflict.

Edit: I took the liberty of fixing the entire issue, including the test, to ensure the problem is fully resolved. I understand this was done without prior approval, so I welcome any feedback or further suggestions. Please let me know if there's anything that needs adjustment!

@crynobone
Copy link
Member

In Laravel 11, App\Http\Controllers\Controller no longer extends Illuminatte\Routing\Controller and therefore the documentation isn't entirely incorrect, but only missing a warning if you extends Illuminate\Routing\Controller.

@RomainMazB
Copy link
Author

In Laravel 11, App\Http\Controllers\Controller no longer extends Illuminatte\Routing\Controller and therefore the documentation isn't entirely incorrect, but only missing a warning if you extends Illuminate\Routing\Controller.

Hmmm, you' right!

The app I'm working on is a L10 upgraded to L11 and this modification wasn't applied.
I guess this issue can be closed.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
3 participants