網(wǎng)站首頁 編程語言 正文
- 前面使用easyswoole將laravel中的blade模板進行了集成,這里也將集成到Hyperf框架中
- 引入模板渲染
composer require hyperf/view
- 引入task并進行打開server.php進行配置
composer require hyperf/task
return [
// 這里省略了其它不相關(guān)的配置項
'settings' => [
// Task Worker 數(shù)量,根據(jù)您的服務(wù)器配置而配置適當(dāng)?shù)臄?shù)量
'task_worker_num' => 8,
// 因為 `Task` 主要處理無法協(xié)程化的方法,所以這里推薦設(shè)為 `false`,避免協(xié)程下出現(xiàn)數(shù)據(jù)混淆的情況
'task_enable_coroutine' => false,
// 靜態(tài)資源
'document_root' => BASE_PATH . '/public',
'enable_static_handler' => true,
],
'callbacks' => [
// 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
新建App\TemplateEngine.php
文件實現(xiàn)模板接口
<?php
declare(strict_types=1);
namespace App\Engine;
use Hyperf\View\Engine\BladeEngine;
use Hyperf\View\Engine\EngineInterface;
class TemplateEngine implements EngineInterface
{
public function render($template, $data, $config): string
{
$engine = new BladeEngine();
return $engine->render($template, $data, $config);
}
}
- 打開
config/autoload/view.php
文件進行模板配置,如不存在自行創(chuàng)建
<?php
declare(strict_types=1);
use App\Engine\TemplateEngine;
return[
'engine'=>TemplateEngine::class,//視圖渲染引擎
'mode'=>\Hyperf\View\Mode::TASK,//視圖渲染模式
'config'=>[
'view_path'=>BASE_PATH.'/storage/view',//視圖文件默認地址
'cache_path'=>BASE_PATH.'/runtime/view'//視圖文件緩存地址
]
];
- 控制器進行使用
<?php
namespace App\Controller;
use App\Model\Name;
use Hyperf\Di\Annotation\Inject;
use Hyperf\HttpServer\Annotation\AutoController;
use Hyperf\View\RenderInterface;
/**
* @AutoController()
*/
class ViewController
{
/**
* @Inject()
* @var RenderInterface
*/
private $render;
public function index()
{
$name = Name::all();
return $this->render->render('index',compact('name'));
}
public function test(RenderInterface $engine){
$name = Name::query()->find(1);
return $engine->render('two',compact('name'));
}
}
- index.blade.php模板
<!DOCTYPE html >
<html lang="en">
<head>
<meta charset="UTF-8">
<title> Hyperf</title>
</head>
<body>
<table>
<thead>
<tr>
<td>name</td>
<td>age</td>
</tr>
</thead>
<tbody>
@foreach($name as $n)
<tr>
<td>{{$n->name}}</td>
<td>{{$n->age}}</td>
</tr>
@endforeach
</tbody>
</table>
</body>
</html>
原文鏈接:https://blog.csdn.net/weixin_43674113/article/details/108831604
相關(guān)推薦
- 2022-10-07 Go語言設(shè)計模式之實現(xiàn)觀察者模式解決代碼臃腫_Golang
- 2022-04-11 Matplotlib直方圖繪制中的參數(shù)bins和rwidth的實現(xiàn)_python
- 2022-05-01 你需要掌握的20個Python常用技巧_python
- 2022-07-11 Python字節(jié)碼與程序執(zhí)行過程詳解_python
- 2022-08-03 GoFrame框架使用避坑指南和實踐干貨_Golang
- 2022-05-21 基于Python實現(xiàn)Hash算法_python
- 2022-12-04 詳解Go?依賴管理?go?mod?tidy_Golang
- 2022-05-23 C#多線程之線程同步WaitHandle_C#教程
- 最近更新
-
- window11 系統(tǒng)安裝 yarn
- 超詳細win安裝深度學(xué)習(xí)環(huán)境2025年最新版(
- Linux 中運行的top命令 怎么退出?
- MySQL 中decimal 的用法? 存儲小
- get 、set 、toString 方法的使
- @Resource和 @Autowired注解
- Java基礎(chǔ)操作-- 運算符,流程控制 Flo
- 1. Int 和Integer 的區(qū)別,Jav
- spring @retryable不生效的一種
- Spring Security之認證信息的處理
- Spring Security之認證過濾器
- Spring Security概述快速入門
- Spring Security之配置體系
- 【SpringBoot】SpringCache
- Spring Security之基于方法配置權(quán)
- redisson分布式鎖中waittime的設(shè)
- maven:解決release錯誤:Artif
- restTemplate使用總結(jié)
- Spring Security之安全異常處理
- MybatisPlus優(yōu)雅實現(xiàn)加密?
- Spring ioc容器與Bean的生命周期。
- 【探索SpringCloud】服務(wù)發(fā)現(xiàn)-Nac
- Spring Security之基于HttpR
- Redis 底層數(shù)據(jù)結(jié)構(gòu)-簡單動態(tài)字符串(SD
- arthas操作spring被代理目標(biāo)對象命令
- Spring中的單例模式應(yīng)用詳解
- 聊聊消息隊列,發(fā)送消息的4種方式
- bootspring第三方資源配置管理
- GIT同步修改后的遠程分支