Skip to content

[BUG] Invalid middleware, it has to provide a process() method. #7656

@HBSpy

Description

@HBSpy

Execute the command and paste the result below.

Command: uname -a && php -v && composer info | grep hyperf && php --ri swoole

/opt # uname -a && php -v && composer info | grep hyperf && php --ri swoole
Linux ef2fda1bfad8 6.17.8-orbstack-00308-g8f9c941121b1 #1 SMP PREEMPT Thu Nov 20 09:34:02 UTC 2025 aarch64 Linux
PHP 8.1.27 (cli) (built: Feb 21 2024 14:48:59) (NTS)
Copyright (c) The PHP Group
Zend Engine v4.1.27, Copyright (c) Zend Technologies
    with Zend OPcache v8.1.27, Copyright (c), by Zend Technologies
96qbhy/hyperf-auth                 3.1.2   hyperf 的 auth 组件
hyperf/async-queue                 3.1.64  A async queue component for hyperf.
hyperf/cache                       3.1.63  A cache component for hyperf.
hyperf/code-parser                 3.1.63  A code parser component for Hyperf.
hyperf/codec                       3.1.63  A codec component for Hyperf.
hyperf/collection                  3.1.64  Hyperf Collection package which come from illuminate/collections
hyperf/command                     3.1.64  Command for hyperf
hyperf/conditionable               3.1.63  Hyperf Macroable package which come from illuminate/conditionable
hyperf/config                      3.1.63  An independent component that provides configuration container.
hyperf/context                     3.1.63  A coroutine/application context library.
hyperf/contract                    3.1.63  The contracts of Hyperf.
hyperf/coordinator                 3.1.63  Hyperf Coordinator
hyperf/coroutine                   3.1.63  Hyperf Coroutine
hyperf/crontab                     3.1.63  A crontab component for Hyperf.
hyperf/database                    3.1.63  A flexible database library.
hyperf/db-connection               3.1.63  A hyperf db connection handler for hyperf/database.
hyperf/devtool                     3.1.63  A Devtool for Hyperf.
hyperf/di                          3.1.63  A DI for Hyperf.
hyperf/dispatcher                  3.1.63  A HTTP Server for Hyperf.
hyperf/engine                      2.15.0  Coroutine engine provided by swoole.
hyperf/engine-contract             1.14.0  Contract for Coroutine Engine
hyperf/event                       3.1.63  an event manager that implements PSR-14.
hyperf/exception-handler           3.1.63  Exception handler for hyperf
hyperf/framework                   3.1.63  A coroutine framework that focuses on hyperspeed and flexible, specifically...
hyperf/guzzle                      3.1.63  Swoole coroutine handler for guzzle
hyperf/http-message                3.1.63  microservice framework base on swoole
hyperf/http-server                 3.1.63  A HTTP Server for Hyperf.
hyperf/logger                      3.1.63  A logger component for hyperf.
hyperf/macroable                   3.1.63  Hyperf Macroable package which come from illuminate/macroable
hyperf/memory                      3.1.63  An independent component that use to operate and manage memory.
hyperf/model-listener              3.1.63  A model listener for Hyperf.
hyperf/paginator                   3.1.63  A paginator component for hyperf.
hyperf/pipeline                    3.1.63  Hyperf Macroable package which come from illuminate/pipeline
hyperf/pool                        3.1.63  An independent universal connection pool component.
hyperf/process                     3.1.63  A process component for hyperf.
hyperf/redis                       3.1.63  A redis component for hyperf.
hyperf/serializer                  3.1.63  A serializer component for Hyperf.
hyperf/server                      3.1.63  A base server library for Hyperf.
hyperf/signal                      3.1.63  A signal library for Hyperf.
hyperf/stdlib                      3.1.63  A stdlib component for Hyperf.
hyperf/stringable                  3.1.63  Hyperf Stringable package which come from illuminate/support
hyperf/support                     3.1.63  A support component for Hyperf.
hyperf/tappable                    3.1.63  Hyperf Macroable package which come from illuminate/tappable
hyperf/testing                     3.1.63  Testing for hyperf
hyperf/translation                 3.1.63  An independent translation component, forked by illuminate/translation.
hyperf/validation                  3.1.64  hyperf validation

swoole

Swoole => enabled
Author => Swoole Team <[email protected]>
Version => 6.0.2
Built => Aug  2 2025 04:02:05
coroutine => enabled with boost asm context
epoll => enabled
eventfd => enabled
signalfd => enabled
cpu_affinity => enabled
spinlock => enabled
rwlock => enabled
openssl => OpenSSL 3.1.8 11 Feb 2025
dtls => enabled
http2 => enabled
json => enabled
curl-native => enabled
curl-version => 8.12.1
c-ares => 1.19.1
zlib => 1.2.13
brotli => E16777225/D16777225
mutex_timedlock => enabled
pthread_barrier => enabled
coroutine_pgsql => enabled
coroutine_odbc => enabled
coroutine_sqlite => enabled

Directive => Local Value => Master Value
swoole.enable_library => On => On
swoole.enable_fiber_mock => Off => Off
swoole.enable_preemptive_scheduler => Off => Off
swoole.display_errors => On => On
swoole.use_shortname => Off => Off
swoole.unixsock_buffer_size => 8388608 => 8388608

Description:

/opt # composer test

co-phpunit --prepend test/bootstrap.php --colors=always
PHPUnit 10.5.58 by Sebastian Bergmann and contributors.

Runtime: PHP 8.1.27
Configuration: /opt/phpunit.xml.dist

// 这里我 dump 了一下,为啥是PriorityMiddleware
^ Hyperf\HttpServer\PriorityMiddleware^ {#186
+middleware: "App\Middleware\AuthMiddleware"
+priority: 0
}
[ERROR] Invalid middleware, it has to provide a process() method.[44] in /opt/vendor/hyperf/dispatcher/src/AbstractRequestHandler.php
[ERROR] #0 /opt/vendor/hyperf/dispatcher/src/HttpRequestHandler.php(27): Hyperf\Dispatcher\AbstractRequestHandler->handleRequest()
#1 /opt/vendor/hyperf/dispatcher/src/HttpDispatcher.php(35): Hyperf\Dispatcher\HttpRequestHandler->handle()
#2 /opt/vendor/hyperf/testing/src/Http/Client.php(200): Hyperf\Dispatcher\HttpDispatcher->dispatch()
#3 /opt/vendor/hyperf/testing/src/Http/Client.php(138): Hyperf\Testing\Http\Client->execute()
#4 /opt/vendor/hyperf/coroutine/src/Waiter.php(48): Hyperf\Testing\Http\Client->Hyperf\Testing\Http{closure}()
#5 /opt/vendor/hyperf/coroutine/src/Coroutine.php(80): Hyperf\Coroutine\Waiter->Hyperf\Coroutine{closure}()
#6 [internal function]: Hyperf\Coroutine\Coroutine::Hyperf\Coroutine{closure}()
#7 {main}

Steps To Reproduce:

composer test 有问题,正常 composer start 没有

<?php

declare(strict_types=1);

namespace App\Controller;

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

#[Controller]
#[Middleware(AuthMiddleware::class)]
class IndexController extends AbstractController
{
    #[RequestMapping('/')]
    public function index()
    {
        $user = $this->auth->user();

        return [
            'message' => "Hello {$user->username}.",
        ];
    }
}
<?php

declare(strict_types=1);

namespace App\Middleware;

use App\Model\User;
use Hyperf\Context\Context;
use Psr\Http\Message\ResponseInterface;
use Psr\Http\Message\ServerRequestInterface;
use Psr\Http\Server\MiddlewareInterface;
use Psr\Http\Server\RequestHandlerInterface;
use Qbhy\HyperfAuth\AuthMiddleware as BaseAuthMiddleware;

use function Hyperf\Support\env;

class AuthMiddleware extends BaseAuthMiddleware implements MiddlewareInterface
{
    public function process(ServerRequestInterface $request, RequestHandlerInterface $handler): ResponseInterface
    {
        if ($token = ($request->getQueryParams()['***'] ?? '')) {
            if ($token == env('***')) {
                $token = $this->auth->login(User::find(1));
                Context::override(
                    ServerRequestInterface::class,
                    fn (ServerRequestInterface $request) => $request->withHeader('Authorization', 'Bearer ' . $token)
                );
            }
        }

        return parent::process($request, $handler);
    }
}

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