hyperf 3.0写一个视图
时间: 2024-03-03 08:53:18 浏览: 130
好的,以下是一个简单的 Hyperf 3.0 的视图示例:
1. 创建一个控制器
在 Hyperf 应用程序中创建一个控制器,例如 `app/Controller/IndexController.php`,并添加一个 `index` 方法:
```php
<?php
declare(strict_types=1);
namespace App\Controller;
use Hyperf\HttpServer\Annotation\Controller;
use Hyperf\HttpServer\Annotation\GetMapping;
use Hyperf\View\RenderInterface;
/**
* @Controller
*/
class IndexController
{
/**
* @GetMapping("/")
*/
public function index(RenderInterface $renderer)
{
return $renderer->render('index', ['name' => 'Hyperf']);
}
}
```
2. 创建一个视图
在 Hyperf 应用程序的 `resources/views` 目录中创建一个名为 `index.blade.php` 的文件,该文件将显示一个简单的欢迎消息和 `$name` 变量的值:
```html
<!DOCTYPE html>
<html>
<head>
<title>Welcome to Hyperf</title>
</head>
<body>
<h1>Hello, {{ $name }}!</h1>
</body>
</html>
```
3. 访问视图
现在,您可以在浏览器中访问 `http://localhost:9501`,并应该看到一个简单的欢迎消息,其中显示 `Hyperf` 变量的值。
希望这个示例对您有所帮助!
阅读全文
相关推荐

















