Hyperf 视图使用

admin   ·   发表于 6个月前   ·   编程代码

安装视图

composer require hyperf/view

配置

新建 config/autoload/view.php 配置文件

<?php
declare(strict_types=1);

use Hyperf\View\Mode;
use Hyperf\View\Engine\BladeEngine;

return [
    // 使用的渲染引擎
    'engine' => BladeEngine::class,
    // 不填写则默认为 Task 模式,推荐使用 Task 模式
    'mode' => Mode::TASK,
    'config' => [
        // 若下列文件夹不存在请自行创建
        'view_path' => BASE_PATH . '/storage/view/',
        'cache_path' => BASE_PATH . '/runtime/view/',
    ],
];

根据文档 在根目录新建 /storage/view 和 /runtime/view 文件夹

使用 Task 模式时,需引入 hyperf/task 组件且必须配置 task_enable_coroutine 为 false,否则会出现协程数据混淆的问题,更多请查阅 Task 组件文档。
若使用 Sync 模式渲染视图时,请确保相关引擎是协程安全的,否则会出现数据混淆的问题,建议使用更加数据安全的 Task 模式

安装 Task

composer require hyperf/task

配置

在 server.php 增加 Task 相关的配置。

<?phpdeclare(strict_types=1);

use Hyperf\Server\SwooleEvent;

return [
    // 这里省略了其它不相关的配置项
    'settings' => [
        // Task Worker 数量,根据您的服务器配置而配置适当的数量
        'task_worker_num' => 8,
        // 因为 `Task` 主要处理无法协程化的方法,所以这里推荐设为 `false`,避免协程下出现数据混淆的情况
        'task_enable_coroutine' => false,
    ],
    'callbacks' => [
        // Task callbacks
        SwooleEvent::ON_TASK => [Hyperf\Framework\Bootstrap\TaskCallback::class, 'onTask'],
        SwooleEvent::ON_FINISH => [Hyperf\Framework\Bootstrap\FinishCallback::class, 'onFinish'],
    ],
];

配置静态资源

如果您希望 Swoole 来管理静态资源,请在 config/autoload/server.php 配置中增加以下配置。

return [    'settings' => [        ...
        // 静态资源
        'document_root' => BASE_PATH . '/public',
        'static_handler_locations' => ['/'],
        'enable_static_handler' => true,
    ],
];

必须在根目录新建 public 文件夹,否则会出现 xxxx/public does not exist

以下是配置文件 server.php 的完整内容

<?phpdeclare(strict_types=1);
/**
 * This file is part of Hyperf.
 *
 * @link     https://www.hyperf.io
 * @document https://doc.hyperf.io
 * @contact  group@hyperf.io
 * @license  https://github.com/hyperf-cloud/hyperf/blob/master/LICENSE
 */

use Hyperf\Server\Server;
use Hyperf\Server\SwooleEvent;

return [
    'mode' => SWOOLE_PROCESS,
    'servers' => [
        [
            'name' => 'http',
            'type' => Server::SERVER_HTTP,
            'host' => '0.0.0.0',
            'port' => 9501,
            'sock_type' => SWOOLE_SOCK_TCP,
            'callbacks' => [
                SwooleEvent::ON_REQUEST => [Hyperf\HttpServer\Server::class, 'onRequest'],
            ],
        ],
    ],
    'settings' => [
        'enable_coroutine' => true,
        'worker_num' => swoole_cpu_num(),
        'pid_file' => BASE_PATH . '/runtime/hyperf.pid',
        'open_tcp_nodelay' => true,
        'max_coroutine' => 100000,
        'open_http2_protocol' => true,
        'max_request' => 100000,
        'socket_buffer_size' => 2 * 1024 * 1024,

        // Task Worker 数量,根据您的服务器配置而配置适当的数量
        'task_worker_num' => 8,
        // 因为 `Task` 主要处理无法协程化的方法,所以这里推荐设为 `false`,避免协程下出现数据混淆的情况
        'task_enable_coroutine' => false,

         // 静态资源
        'document_root' => BASE_PATH . '/public',
        'static_handler_locations' => ['/'],
        'enable_static_handler' => true,
    ],
    'callbacks' => [
        SwooleEvent::ON_BEFORE_START => [Hyperf\Framework\Bootstrap\ServerStartCallback::class, 'beforeStart'],
        SwooleEvent::ON_WORKER_START => [Hyperf\Framework\Bootstrap\WorkerStartCallback::class, 'onWorkerStart'],
        SwooleEvent::ON_PIPE_MESSAGE => [Hyperf\Framework\Bootstrap\PipeMessageCallback::class, 'onPipeMessage'],
        // Task callbacks
        SwooleEvent::ON_TASK => [Hyperf\Framework\Bootstrap\TaskCallback::class, 'onTask'],
        SwooleEvent::ON_FINISH => [Hyperf\Framework\Bootstrap\FinishCallback::class, 'onFinish'],
    ],
];

安装视图渲染引擎

以 blade 为例

composer require duncan3dc/blade

使用

在 storage\view 中新建 index.blade.php 文件

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Hyperf</title>
</head>
<body>
Hello, {{ $name }}. You are using blade template now.
</body>
</html>

以 IndexController 为例,实例化 RenderInterface 调用 render 即可

<?php

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

namespace App\Controller;

use Hyperf\View\RenderInterface;

class IndexController extends Controller
{
    public function index(RenderInterface $render)
    {
        return $render->render('index', ['name' => 'Hyperf']);
    }
}

重启服务、应该就可以访问到视图页面了

上面说到 由 Hyperf 接管静态路由,那么在视图中该怎样访问呢? 比如 public/css/app.css 该怎样引入呢?

<link rel="stylesheet"  href="/css/app.css">

直接 / 即可

建议通读一遍文档、按文档一步一步来,就可以、否则你会遇到一对错误。比如 task 问题、public 文件不存在等等。

0 Reply   |  Until 6个月前 | 623 View
LoginCan Publish Content