Skip to content

[BUG] Annotations like #[Controller] and #[Middleware] don't work with invokable controllers #7323

@binaryfire

Description

@binaryfire

Hi. The #[Controller] and #[Middleware] annotations don't work with the __invoke method in invokable controllers.

This doesn't work:

<?php

declare(strict_types=1);

namespace App\Controllers;

use Hyperf\HttpServer\Annotation\Controller;
use Hyperf\HttpServer\Annotation\Middleware;
use Hyperf\HttpServer\Annotation\RequestMapping;
use App\Middleware\RestrictToLocalhost;

#[Controller]
class HealthCheckController
{
    #[RequestMapping(path: "/health", methods: "get")]
    #[Middleware(RestrictToLocalhost::class)]
    public function __invoke(): array
    {
        return [
            'status' => 'ok',
            'timestamp' => time(),
        ];
    }
}

This works correctly:

<?php

declare(strict_types=1);

namespace App\Controllers;

use Hyperf\HttpServer\Annotation\Controller;
use Hyperf\HttpServer\Annotation\Middleware;
use Hyperf\HttpServer\Annotation\RequestMapping;
use App\Middleware\RestrictToLocalhost;

#[Controller]
class HealthCheckController
{
    #[RequestMapping(path: "/health", methods: "get")]
    #[Middleware(RestrictToLocalhost::class)]
    public function check(): array
    {
        return [
            'status' => 'ok',
            'timestamp' => time(),
        ];
    }
}

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugSomething isn't working

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions