hyperf实现视图smarty模板

发布时间:2021-12-30 14:01:07 阅读:1153次

一直到现在用hyperf一直写的是接口

如何用hyperf视图呢

以下为代码

安装视图

composer require hyperf/view
安装 Smarty 引擎

composer require smarty/smarty
安装task

composer require hyperf/task
控制器 app/Controller/ViewController.php

<?php

declare(strict_types=1);

namespace App\Controller;

use Hyperf\HttpServer\Contract\RequestInterface;
use Hyperf\HttpServer\Contract\ResponseInterface;
use Hyperf\View\RenderInterface;
use Hyperf\HttpServer\Annotation\AutoController;

/**
* @AutoController;
*/
class ViewController
{
    public function index(RenderInterface $render)
    {
        return $render->render('index.tpl', ['name' => 'Huyongjian']);
    }
}
模板 storage/view/index.tpl
<!DOCTYPE html>
<html>
<head>
    <meta charset="UTF-8">
    <style>
        body {
            background-color: lightblue;
        }
       
        h1 {
            color: white;
            text-align: center;
        }
        p {
            font-family: verdana;
            font-size: 20px;
        }
    </style>
</head>
<body>
​
<h1>我的第一个 CSS 实例</h1>
<p>这是一个段落。</p>
​
Hello,{$name},this is smarty template.
</body>
</html>
​
模板配置文件 config/autoload/view.php

<?php

declare(strict_types=1);

use Hyperf\View\Engine\SmartyEngine;
use Hyperf\View\Mode;

return [
    'engine' => SmartyEngine::class,
    'mode' => Mode::TASK,
    'config' => [
        'view_path' => BASE_PATH . '/storage/view/',
        'cache_path' => BASE_PATH . '/runtime/view/',
    ],
];
server配置文件 config/autoload/server.php

<?php

declare(strict_types=1);

use Hyperf\Server\Event;
use Hyperf\Server\Server;
use Swoole\Constant;

return [
    'mode' => SWOOLE_PROCESS,
    'servers' => [
        [
            'name' => 'http',
            'type' => Server::SERVER_HTTP,
            'host' => '0.0.0.0',
            'port' => 9501,
            'sock_type' => SWOOLE_SOCK_TCP,
            'callbacks' => [
                Event::ON_REQUEST => [Hyperf\HttpServer\Server::class, 'onRequest'],
            ],
        ],
    ],
    'settings' => [
        Constant::OPTION_ENABLE_COROUTINE => true,
        Constant::OPTION_WORKER_NUM => swoole_cpu_num(),
        Constant::OPTION_PID_FILE => BASE_PATH . '/runtime/hyperf.pid',
        Constant::OPTION_OPEN_TCP_NODELAY => true,
        Constant::OPTION_MAX_COROUTINE => 100000,
        Constant::OPTION_OPEN_HTTP2_PROTOCOL => true,
        Constant::OPTION_MAX_REQUEST => 100000,
        Constant::OPTION_SOCKET_BUFFER_SIZE => 2 * 1024 * 1024,
        Constant::OPTION_BUFFER_OUTPUT_SIZE => 2 * 1024 * 1024,

        //增加配置
        // Task Worker 数量,根据您的服务器配置而配置适当的数量
        'task_worker_num' => 8,
        // 因为 `Task` 主要处理无法协程化的方法,所以这里推荐设为 `false`,避免协程下出现数据混淆的情况
        'task_enable_coroutine' => false,
    ],
    'callbacks' => [
        Event::ON_WORKER_START => [Hyperf\Framework\Bootstrap\WorkerStartCallback::class, 'onWorkerStart'],
        Event::ON_PIPE_MESSAGE => [Hyperf\Framework\Bootstrap\PipeMessageCallback::class, 'onPipeMessage'],
        Event::ON_WORKER_EXIT => [Hyperf\Framework\Bootstrap\WorkerExitCallback::class, 'onWorkerExit'],


         //增加配置
        // Task callbacks
        Event::ON_TASK => [Hyperf\Framework\Bootstrap\TaskCallback::class, 'onTask'],
        Event::ON_FINISH => [Hyperf\Framework\Bootstrap\FinishCallback::class, 'onFinish'],
    ],
];
访问测试

curl 118.195.173.53:9501/view/index
返回结果


<!DOCTYPE html>
<html>
<head>
    <meta charset="UTF-8">
    <style>
        body {
            background-color: lightblue;
        }
        ​
        h1 {
            color: white;
            text-align: center;
        }
        ​
        p {
            font-family: verdana;
            font-size: 20px;
        }
    </style>
</head>
<body>
​
<h1>我的第一个 CSS 实例</h1>
<p>这是一个段落。</p>
​
Hello,Huyongjian,this is smarty template.
</body>
</html>
​
转https://www.cnblogs.com/hu308830232/p/15323764.html

如有问题,可以QQ搜索群1028468525加入群聊,欢迎一起研究技术

支付宝 微信

有疑问联系站长,请联系QQ:QQ咨询

转载请注明:hyperf实现视图smarty模板 出自老鄢博客 | 欢迎分享