2019-07-18 16:20:02 +08:00
|
|
|
|
# 视图
|
|
|
|
|
|
|
|
|
|
## 安装
|
|
|
|
|
|
|
|
|
|
```
|
|
|
|
|
composer require hyperf/view
|
|
|
|
|
```
|
|
|
|
|
|
|
|
|
|
## 配置
|
|
|
|
|
|
|
|
|
|
| 配置 | 类型 | 默认值 | 备注 |
|
|
|
|
|
|:-----------------:|:------:|:--------------------------------------:|:----------------:|
|
2019-07-22 02:06:27 +08:00
|
|
|
|
| engine | string | Hyperf\View\Engine\BladeEngine::class | 视图渲染引擎 |
|
2019-07-18 16:20:02 +08:00
|
|
|
|
| mode | string | Mode::TASK | 视图渲染模式 |
|
|
|
|
|
| config.view_path | string | 无 | 视图文件默认地址 |
|
|
|
|
|
| config.cache_path | string | 无 | 视图文件缓存地址 |
|
|
|
|
|
|
|
|
|
|
```php
|
|
|
|
|
<?php
|
|
|
|
|
|
|
|
|
|
declare(strict_types=1);
|
|
|
|
|
|
|
|
|
|
use Hyperf\View\Mode;
|
|
|
|
|
use Hyperf\View\Engine\BladeEngine;
|
|
|
|
|
|
|
|
|
|
return [
|
2019-07-22 02:06:27 +08:00
|
|
|
|
// 使用的渲染引擎
|
2019-07-18 16:20:02 +08:00
|
|
|
|
'engine' => BladeEngine::class,
|
2019-07-22 02:06:27 +08:00
|
|
|
|
// 不填写则默认为 Task 模式
|
2019-07-18 16:20:02 +08:00
|
|
|
|
'mode' => Mode::TASK,
|
|
|
|
|
'config' => [
|
2019-07-22 02:06:27 +08:00
|
|
|
|
// 若不存在请自行创建
|
2019-07-18 16:20:02 +08:00
|
|
|
|
'view_path' => BASE_PATH . '/storage/view/',
|
|
|
|
|
'cache_path' => BASE_PATH . '/runtime/view/',
|
|
|
|
|
],
|
|
|
|
|
];
|
|
|
|
|
```
|
|
|
|
|
|
2019-07-22 02:06:27 +08:00
|
|
|
|
> 使用 `Task` 模式时,需引入 [hyperf/task](https://github.com/hyperf-cloud/task) 组件且必须配置 `task_enable_coroutine` 为 `false`,否则会出现协程数据混淆的问题,更多请查阅 [Task](zh/task.md) 组件文档。
|
2019-07-18 16:27:00 +08:00
|
|
|
|
|
2019-07-18 16:20:02 +08:00
|
|
|
|
## 使用
|
|
|
|
|
|
|
|
|
|
以下以 `BladeEngine` 为例,首先在对应的目录里创建视图文件 `index.blade.php`。
|
|
|
|
|
|
|
|
|
|
```blade
|
|
|
|
|
<!DOCTYPE html>
|
|
|
|
|
<html lang="en">
|
|
|
|
|
<head>
|
|
|
|
|
<meta charset="UTF-8">
|
|
|
|
|
<title>Hyperf</title>
|
|
|
|
|
</head>
|
|
|
|
|
<body>
|
2019-07-22 02:06:27 +08:00
|
|
|
|
Hello, {{ $name }}. You are using blade template now.
|
2019-07-18 16:20:02 +08:00
|
|
|
|
</body>
|
|
|
|
|
</html>
|
|
|
|
|
```
|
|
|
|
|
|
|
|
|
|
控制器中获取 `Hyperf\View\Render` 示例,然后返回渲染数据即可。
|
|
|
|
|
|
|
|
|
|
```php
|
|
|
|
|
<?php
|
|
|
|
|
|
|
|
|
|
declare(strict_types=1);
|
|
|
|
|
|
|
|
|
|
namespace App\Controller;
|
|
|
|
|
|
|
|
|
|
use Hyperf\HttpServer\Annotation\AutoController;
|
2019-07-22 02:06:27 +08:00
|
|
|
|
use Hyperf\View\RenderInterface;
|
2019-07-18 16:20:02 +08:00
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* @AutoController
|
|
|
|
|
*/
|
|
|
|
|
class ViewController
|
|
|
|
|
{
|
2019-07-22 02:06:27 +08:00
|
|
|
|
public function index(RenderInterface $render)
|
2019-07-18 16:20:02 +08:00
|
|
|
|
{
|
2019-07-22 02:06:27 +08:00
|
|
|
|
return $render->render('index', ['name' => 'Hyperf']);
|
2019-07-18 16:20:02 +08:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
```
|
|
|
|
|
|
2019-07-22 02:06:27 +08:00
|
|
|
|
访问对应的 URL,即可获得如下所示的视图页面:
|
2019-07-18 16:20:02 +08:00
|
|
|
|
|
|
|
|
|
```
|
|
|
|
|
Hello, Hyperf. You are using blade template now.
|
|
|
|
|
```
|