hyperf:路由配置:访问地址不存在情况

一,查看现有路由:

$ php bin/hyperf.php describe:route
[DEBUG] [command] Commands registered by Hyperf\Command\Listener\RegisterCommandListener
[DEBUG] Event Hyperf\Framework\Event\BootApplication handled by Hyperf\Command\Listener\RegisterCommandListener listener.
[DEBUG] Event Hyperf\Framework\Event\BootApplication handled by Hyperf\Config\Listener\RegisterPropertyHandlerListener listener.
[DEBUG] Event Hyperf\Framework\Event\BootApplication handled by Hyperf\DbConnection\Listener\RegisterConnectionResolverListener listener.
[DEBUG] Event Hyperf\Framework\Event\BootApplication handled by Hyperf\ExceptionHandler\Listener\ExceptionHandlerListener listener.
[DEBUG] Event Hyperf\Framework\Event\BootApplication handled by Hyperf\ExceptionHandler\Listener\ErrorExceptionHandler listener.
+--------+---------------+---------------+--------------------------------------------+------------------------------------+
| Server | Method        | URI           | Action                                     | Middleware                         |
+--------+---------------+---------------+--------------------------------------------+------------------------------------+
| http   | GET|POST|HEAD | /             | App\Controller\IndexController@index       |                                    |
+--------+---------------+---------------+--------------------------------------------+------------------------------------+
| http   | GET           | /favicon.ico  | Closure                                    |                                    |
+--------+---------------+---------------+--------------------------------------------+------------------------------------+
| http   | GET           | /image/list   | App\Controller\ImageController@listimage   | App\Middleware\AccesslogMiddleware |
+--------+---------------+---------------+--------------------------------------------+------------------------------------+
| http   | GET           | /image/detail | App\Controller\ImageController@imagedetail | App\Middleware\AccesslogMiddleware |
+--------+---------------+---------------+--------------------------------------------+------------------------------------+
[DEBUG] Event Hyperf\Command\Event\AfterExecute handled by App\Listener\ResumeExitCoordinatorListener listener.

现象:访问不存在的路由时:

二,解决

1, config/autoload/exceptions.php

<?php

declare(strict_types=1);
/**
 * This file is part of Hyperf.
 *
 * @link     https://www.hyperf.io
 * @document https://hyperf.wiki
 * @contact  group@hyperf.io
 * @license  https://github.com/hyperf/hyperf/blob/master/LICENSE
 */
return [
    'handler' => [
        'http' => [
            Hyperf\HttpServer\Exception\Handler\HttpExceptionHandler::class,
            App\Exception\Handler\AppExceptionHandler::class,
        ],
    ],
];

第一个解决方法:注释掉第一行的HttpExceptionHandler::class,
发生异常时会统一由第二行的AppExceptionHandler::class进行处理

第二个解决方法:自定义一个类代替第一行的HttpExceptionHandler::class,

修改配置:

<?php

declare(strict_types=1);
/**
 * This file is part of Hyperf.
 *
 * @link     https://www.hyperf.io
 * @document https://hyperf.wiki
 * @contact  group@hyperf.io
 * @license  https://github.com/hyperf/hyperf/blob/master/LICENSE
 */
return [
    'handler' => [
        'http' => [
            //Hyperf\HttpServer\Exception\Handler\HttpExceptionHandler::class,
            App\Exception\Handler\HttpExceptionHandler::class,
            App\Exception\Handler\AppExceptionHandler::class,
        ],
    ],
];

2,App\Exception\Handler\HttpExceptionHandler.php

<?php


declare(strict_types=1);
/**
 * This file is part of Hyperf.
 *
 * @link     https://www.hyperf.io
 * @document https://hyperf.wiki
 * @contact  group@hyperf.io
 * @license  https://github.com/hyperf/hyperf/blob/master/LICENSE
 */

namespace App\Exception\Handler;

use Hyperf\Contract\StdoutLoggerInterface;
use Hyperf\ExceptionHandler\ExceptionHandler;
use Hyperf\ExceptionHandler\Formatter\FormatterInterface;
use Hyperf\HttpMessage\Exception\HttpException;
use Hyperf\HttpMessage\Stream\SwooleStream;
use Swow\Psr7\Message\ResponsePlusInterface;
use Throwable;

class HttpExceptionHandler extends ExceptionHandler
{
    public function __construct(protected StdoutLoggerInterface $logger, protected FormatterInterface $formatter)
    {
    }

    /**
     * Handle the exception, and return the specified result.
     * @param HttpException $throwable
     */
    public function handle(Throwable $throwable, ResponsePlusInterface $response)
    {
        $this->logger->debug($this->formatter->format($throwable));

        $this->stopPropagation();

        // 格式化输出,支掉了, JSON_UNESCAPED_UNICODE
        $data = json_encode([
            'code' => $throwable->getCode(),
            'message' => $throwable->getMessage(),
        ]);
        return $response->setStatus($throwable->getStatusCode())->setBody(new SwooleStream($data));
    }

    /**
     * Determine if the current exception handler should handle the exception.
     *
     * @return bool If return true, then this exception handler will handle the exception,
     *              If return false, then delegate to next handler
     */
    public function isValid(Throwable $throwable): bool
    {
        return $throwable instanceof HttpException;
    }
}

三,测试效果:

 

posted @ 2025-02-15 11:08  刘宏缔的架构森林  阅读(47)  评论(0)    收藏  举报