網站首頁 編程語言 正文
- 前面使用easyswoole將laravel中的blade模板進行了集成,這里也將集成到Hyperf框架中
- 引入模板渲染
composer require hyperf/view
- 引入task并進行打開server.php進行配置
composer require hyperf/task
return [
// 這里省略了其它不相關的配置項
'settings' => [
// Task Worker 數量,根據您的服務器配置而配置適當的數量
'task_worker_num' => 8,
// 因為 `Task` 主要處理無法協程化的方法,所以這里推薦設為 `false`,避免協程下出現數據混淆的情況
'task_enable_coroutine' => false,
// 靜態資源
'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
文件實現模板接口
<?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
文件進行模板配置,如不存在自行創建
<?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
相關推薦
- 2022-05-03 python中的Pytorch建模流程匯總_python
- 2022-06-09 FreeRTOS軟件定時器apollo中斷狀態判斷_操作系統
- 2022-05-28 C++?超詳細講解stack與queue的使用_C 語言
- 2022-05-17 Python映射類型之dict詳解_python
- 2022-05-29 C++掃盲篇之指針詳解_C 語言
- 2024-07-13 根據腳手架archetype快速構建spring boot/cloud項目
- 2022-11-23 Qt采用線程以隊列方式實現下發數據_C 語言
- 2022-12-23 Kubernetes存儲系統數據持久化管理詳解_云其它
- 最近更新
-
- window11 系統安裝 yarn
- 超詳細win安裝深度學習環境2025年最新版(
- Linux 中運行的top命令 怎么退出?
- MySQL 中decimal 的用法? 存儲小
- get 、set 、toString 方法的使
- @Resource和 @Autowired注解
- Java基礎操作-- 運算符,流程控制 Flo
- 1. Int 和Integer 的區別,Jav
- spring @retryable不生效的一種
- Spring Security之認證信息的處理
- Spring Security之認證過濾器
- Spring Security概述快速入門
- Spring Security之配置體系
- 【SpringBoot】SpringCache
- Spring Security之基于方法配置權
- redisson分布式鎖中waittime的設
- maven:解決release錯誤:Artif
- restTemplate使用總結
- Spring Security之安全異常處理
- MybatisPlus優雅實現加密?
- Spring ioc容器與Bean的生命周期。
- 【探索SpringCloud】服務發現-Nac
- Spring Security之基于HttpR
- Redis 底層數據結構-簡單動態字符串(SD
- arthas操作spring被代理目標對象命令
- Spring中的單例模式應用詳解
- 聊聊消息隊列,發送消息的4種方式
- bootspring第三方資源配置管理
- GIT同步修改后的遠程分支