# 快速开始
# 安装
Herophp 4.0 以上的版本依赖 PHP8.1 以上的版本。
# 1. 创建应用
composer create-project herosphp/app demo # demo 指代应用名称(App Name)
# 2. 运行项目
首先进入项目根目录 cd demo
- Debug 方式运行(用于开发调试)
php web.php start
- Daemon 方式运行(用于正式环境)
php web.php start -d
# 3. 访问测试
浏览器访问 http://localhost:2345
# 目录结构
├── app # 应用目录
│ ├── cmd # 命令行控制器目录
│ ├── controller # Web控制器目录
│ ├── exception # 应用异常目录
│ ├── middleware # 中间件目录
│ ├── service # 服务目录
├── boot.php # 公共启动文件,主要定义了框架的一些全局常量
├── client.php # 命令行服务启动文件
├── config # 应用配置目录
│ ├── app.config.php # 应用通用配置文件
│ ├── middleware.config.php # 中间件配置文件
│ ├── process.config.php # 自定义进程配置文件
│ ├── redis.config.php # redis 配置文件
│ ├── session.config.php # session 配置文件
│ └── storage.config.php # 文件上传插件配置
├── Monitor.php # 源码监控文件,用于源码热加载
├── process # 自定义进程目录
├── process.php # 自定义进程服务启动文件
├── public # 公共静态资源目录
├── runtime # 应用的运行时目录,需要可写权限
├── script # 回调脚本目录
│ └── Installer.php # 安装回调脚本
├── views # 视图目录
│ └── default # 皮肤目录
└── web.php # Web 服务启动文件
# 简单示例
首先, 在 app/controller
目录中新建控制器 HelloController.php
:
<?php
declare(strict_types=1);
namespace app\controller;
use herosphp\annotation\Controller;
use herosphp\annotation\Get;
use herosphp\core\BaseController;
use herosphp\core\HttpRequest;
use herosphp\core\HttpResponse;
#[Controller(HelloController::class)]
class HelloController extends BaseController
{
}
# 1. 返回字符串
在控制器(HelloController.php
)中添加一个返回字符串的路由方法:
#[Get(uri: ['/', '/hello/string'])]
public function hello(HttpRequest $request): string
{
$name = $request->get('name', 'world');
return "hello, $name";
}
浏览器访问 http://localhost:2345/hello/string?name=herosphp
, 将输出字符串 hello, herosphp
。
# 2. 返回 JSON
在控制器中添加一个返回 JSON 的路由方法:
#[Get(uri: '/hello/json')]
public function helloJson(): HttpResponse
{
return $this->json(['text' => 'hello world']);
}
浏览器访问 http://localhost:2345/hello/json
, 将输出 JSON 字符串 {"text":"hello world"}
。
# 3. 返回 HTML
在控制器中添加一个返回 HTML 的路由方法
#[Get(uri: '/hello/html')]
public function helloHtml()
{
return $this->html('hello', [
'title' => 'Hello, World.',
'colors' => ['red', 'green', 'yellow']
]);
}
然后新建模板 app/view/default/hello.html
:
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8" />
<title>herosphp</title>
</head>
<body>
<h1>{$title}</h1>
<h1>Colors</h1>
{loop $colors $val}
<div>{$val}</div>
{/loop}
</body>
</html>
浏览器访问 http://localhost:2345/hello/html
将返回一个 HTML 视图,herosphp 内置实现了一个非常轻量的模板引擎,提供了很多好用的模板标签,想要要了解更多的话,请参考视图文档。
# 4. 完整的控制器代码
<?php
declare(strict_types=1);
namespace app\controller;
use herosphp\annotation\Controller;
use herosphp\annotation\Get;
use herosphp\core\BaseController;
use herosphp\core\HttpRequest;
use herosphp\core\HttpResponse;
#[Controller(HelloController::class)]
class HelloController extends BaseController
{
#[Get(uri: ['/', '/hello/string'])]
public function hello(HttpRequest $request): string
{
$name = $request->get('name', 'world');
return "hello, $name";
}
#[Get(uri: '/hello/json')]
public function helloJson(): HttpResponse
{
return $this->json(['text' => 'hello world']);
}
#[Get(uri: '/hello/html')]
public function helloHtml()
{
return $this->html('hello', [
'title' => 'Hello, World.',
'colors' => ['red', 'green', 'yellow']
]);
}
}