Try the view function of hyperf today
First look at the documentation, you need to install the hyperf/view component, composer require hyperf/view. An error was reported, saying that the version of other components is too low to install, because my own version is hyperf2.0, which is currently 2.1 version , Then update the instructions for installing the official documentation. https://hyperf.wiki/2.1/#/zh-cn/upgrade/2.1?id=%e4%bf%ae%e6%94%b9-hyperf-%e7%bb%84%e4%bb%b6% e7%89%88%e6%9c%ac
The upgrade version is very simple, just modify composer.json
the hyperf/*
unity in directly 2.1.*
.
Execute after modification composer update -o,升级到2.1成功
然后重新
composer require hyperf/view, ok installation is successful, the next step is to configure or operate according to the document. php bin/hyperf.php vendor:publish hyperf/view
Okay, view.php is generated under config/autoload
Configure after opening
<?php
declare(strict_types=1);
/**
* This file is part of Hyperf.
*
* @link https://www.hyperf.io
* @document https://doc.hyperf.io
* @contact [email protected]
* @license https://github.com/hyperf/hyperf/blob/master/LICENSE
*/
return [
'engine' => Hyperf\ViewEngine\HyperfViewEngine::class,
'mode' => Hyperf\View\Mode::SYNC,
'config' => [
'view_path' => BASE_PATH . '/storage/view/',
'cache_path' => BASE_PATH . '/runtime/view/',
],
];
The configuration is complete. In this case, because the engine needs to be installed by itself, the corresponding components must be installed before the configuration composer require hyperf/view-engine
After the configuration is complete, it is ready to use. First, create a folder according to the directory in the configuration. I created a new test.blade.php under the storage/view/index directory.
The content is as follows
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Hyperf</title>
</head>
<body>
@foreach($data as $v)
{
{$v}}
@endforeach
</body>
</html>
Then in IndexController, render the page through the $render function and return. Note that you must first inject RenderInterface $render in index()
public function index(RenderInterface $render)
{
// var_dump($this->calculatorService->add(1,2));
$user = $this->request->input('user', 'Hyperf');
$method = $this->request->getMethod();
$arr = ["哈哈","嘿嘿","嚯嚯"];
return $render->render('index.test', ['data' => $arr]);
}
OK, then configure routing in route
Router::addRoute(['GET', 'POST', 'HEAD'], '/', 'App\Controller\IndexController@index');
Just start hyperf