hyperf 入门笔记

87 阅读1分钟
  1. 静态文件访问

修改配置文件 config/autoload/server.php

    'settings' => [
        'document_root' =>  BASE_PATH.'/public',
        'static_files' => [
            '.jpg', '.jpeg', '.png', '.gif', '.ico', '.css', '.js', '.woff', '.ttf', '.eot', '.svg', '.map',
        ],
    ]
  1. 数据库查询,将返回对象转为数组
  1. 没有就新建监数据ResultToArray.php
  2. 将监听器添加到 config/autoload/listeners.php
namespace App\Listener;

use Hyperf\Database\Events\StatementPrepared;
use Hyperf\Event\Annotation\Listener;
use Hyperf\Event\Contract\ListenerInterface;

/**
 * @Listener
 */
class ResultToArray implements ListenerInterface
{
    public function listen(): array
    {
        return [
            StatementPrepared::class,
        ];
    }

    public function process(object $event): void
    {
        if ($event instanceof StatementPrepared) {
            $event->statement->setFetchMode(\PDO::FETCH_ASSOC);
        }
    }
}

return [
    Hyperf\ExceptionHandler\Listener\ErrorExceptionHandler::class,
    Hyperf\Command\Listener\FailToHandleListener::class,
    \App\Listener\ResultToArray::class
];