命令行 Console
通过 lee 入口脚本执行命令。
入口
项目根目录下的 lee 文件:
php lee list # 列出所有命令
php lee run # 启动内置开发服务器
php lee run --port=8080 # 指定端口
php lee run --host=0.0.0.0 # 指定监听地址内置命令
| 命令 | 说明 |
|---|---|
list | 列出所有可用命令 |
run | 启动 PHP 内置开发服务器(自动处理 public/ 静态文件) |
route:list | 列出所有已注册的路由 |
make:rest | 创建资源控制器(同时生成 Model 与 Validate) |
make:base | 创建基础控制器(不集成资源路由,含 #[Route] 注解) |
config:publish | 发布模块配置文件到 config/ 目录 |
migrate:run | 执行数据库迁移 |
migrate:rollback | 回滚迁移 |
seed:run | 执行数据填充 |
cron:run | 执行定时任务 |
queue:work | 队列消费 |
run 开发服务器
run 命令启动 PHP 内置服务器,public/ 目录作为文档根:
- 静态文件(CSS、JS、图片等)直接从
public/提供,不经过 PHP - 其余请求交由应用路由处理
php lee run # http://127.0.0.1:8000
php lee run --port=9000 # http://127.0.0.1:9000
php lee run --host=0.0.0.0 --port=80 # 监听所有网卡route:list 路由列表
route:list 命令列出所有已注册的路由,包括 HTTP 方法、路径和对应的控制器方法:
php lee route:list输出示例:
Registered routes: (8)
GET /users App\controller\UserController@index
POST /users App\controller\UserController@save
GET /users/{id} App\controller\UserController@read
PUT /users/{id} App\controller\UserController@update
PATCH /users/{id} App\controller\UserController@update
DELETE /users/{id} App\controller\UserController@delete
DELETE /users App\controller\UserController@batch_delete
GET / App\controller\IndexController@indexmake:rest 创建资源控制器
make:rest 命令一键生成资源控制器及其配套的 Model 和 Validate 文件,三个文件同时创建、互相引用:
php lee make:rest User执行后会生成以下三个文件:
| 文件 | 说明 |
|---|---|
app/controller/UserController.php | 继承 ResourceController,自动绑定 Model 与 Validate,预置 6 个资源方法 |
app/model/User.php | 继承 Lychee\orm\Model,表名按类名自动推断 |
app/validate/UserValidate.php | 继承 Lychee\validate\Validate,预置空的 $rule 与 $message |
控制器自动绑定的资源路径为模型名的小写形式(User → /user),并生成以下方法骨架,方便直接修改业务逻辑:
#[Resource('/user')]
class UserController extends ResourceController
{
protected string $modelClass = User::class;
protected string $validateClass = UserValidate::class;
public function index(): JsonResponse // → $this->baseIndex($where, $current, $pageSize)
public function save(Request $request): JsonResponse // → $this->baseSave($request)
public function read(int $id): JsonResponse // → $this->baseRead($id)
public function update(Request $request, int $id): JsonResponse // → $this->baseUpdate($request, $id)
public function delete(int $id): JsonResponse // → $this->baseDelete($id)
public function batch_delete(Request $request): JsonResponse // → $this->baseBatchDelete($request)
}模型名支持省略
Controller后缀,php lee make:rest User与php lee make:rest UserController等效。 若控制器文件已存在则命令中止并报错;若同名 Model 或 Validate 已存在则跳过创建,不会覆盖已有文件。
make:base 创建基础控制器
make:base 命令生成一个不集成资源路由的基础控制器,不继承任何基类,通过 #[Route] 注解注册路由,仅预置一个返回空 JsonResponse 的 index 方法,适合自定义业务逻辑的场景:
php lee make:base Index生成的文件结构:
<?php
declare(strict_types=1);
namespace App\controller;
use Lychee\http\JsonResponse;
use Lychee\routing\Route;
#[Route('/index')]
class IndexController
{
#[Route('/')]
public function index(): JsonResponse
{
return new JsonResponse(null);
}
}类上的
#[Route('/index')]作为路由前缀,方法上的#[Route('/')]将该方法注册为路由(默认 GET),最终路由为GET /index。 控制器名支持省略Controller后缀,php lee make:base Index与php lee make:base IndexController等效。 若控制器文件已存在则命令中止并报错。
config:publish 发布模块配置
config:publish 命令将框架内置模块的配置文件发布到项目的 config/ 目录,方便开发者快速生成基础配置并按需修改。
# 发布所有模块的配置文件
php lee config:publish
# 发布指定模块的配置文件
php lee config:publish cache
# 覆盖已存在的配置文件(默认不会覆盖)
php lee config:publish cache --force支持的模块:
| 模块 | 说明 |
|---|---|
app | 应用基础配置(时区、路由前缀、异常渲染等) |
cache | 缓存驱动配置(file / redis) |
command | 自定义命令注册配置 |
captcha | 验证码配置(图形、短信、邮箱) |
cron | 定时任务配置 |
database | 数据库连接配置(mysql / sqlite) |
filesystem | 文件系统磁盘配置 |
i18n | 国际化多语言配置 |
log | 日志频道配置 |
plugin | 插件注册配置 |
queue | 队列连接配置(sync / redis) |
satoken | SaToken 认证配置 |
session | 会话 Session 配置 |
view | Twig 模板引擎配置 |
websocket | WebSocket 服务配置 |
若目标配置文件已存在,命令会跳过该模块并输出提示;使用
--force(或-f)选项可强制覆盖。
自定义命令
继承 Lychee\console\Command:
// app/command/HelloCommand.php
namespace App\command;
use Lychee\console\Command;
use Lychee\console\Input;
use Lychee\console\Output;
class HelloCommand extends Command
{
protected string $name = 'hello';
protected string $description = 'Say hello';
protected function configure(): void
{
$this->setName($this->name);
$this->setDescription($this->description);
$this->addArgument('name', \Lychee\console\input\Argument::OPTIONAL, 'Your name', 'World');
}
protected function execute(Input $input, Output $output): int
{
$name = $input->getArgument('name');
$output->writeln("<info>Hello, {$name}!</info>");
return 0;
}
}注册自定义命令
推荐通过 config/command.php 配置文件注册,无需修改入口脚本:
# 发布命令配置文件(若尚未创建)
php lee config:publish command// config/command.php
return [
'commands' => [
\App\command\HelloCommand::class,
],
];应用级命令在所有框架内置命令与插件命令之后注册,因此可覆盖同名的框架或插件命令。
也可以在入口脚本
lee中手动注册:php$console->addCommand(\App\command\HelloCommand::class);
输出样式
$output->writeln('<info>成功</info>');
$output->writeln('<comment>提示</comment>');
$output->writeln('<error>错误</error>');