Skip to content

命令行 Console ​

通过 lee 入口脚本执行命令。

入口 ​

项目根目录下的 lee 文件:

bash
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
  • 其余请求交由应用路由处理
bash
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 方法、路径和对应的控制器方法:

bash
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@index

make:rest 创建资源控制器 ​

make:rest 命令一键生成资源控制器及其配套的 Model 和 Validate 文件,三个文件同时创建、互相引用:

bash
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),并生成以下方法骨架,方便直接修改业务逻辑:

php
#[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 方法,适合自定义业务逻辑的场景:

bash
php lee make:base Index

生成的文件结构:

php
<?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/ 目录,方便开发者快速生成基础配置并按需修改。

bash
# 发布所有模块的配置文件
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)
satokenSaToken 认证配置
session会话 Session 配置
viewTwig 模板引擎配置
websocketWebSocket 服务配置

若目标配置文件已存在,命令会跳过该模块并输出提示;使用 --force(或 -f)选项可强制覆盖。

自定义命令 ​

继承 Lychee\console\Command:

php
// 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 配置文件注册,无需修改入口脚本:

bash
# 发布命令配置文件(若尚未创建)
php lee config:publish command
php
// config/command.php
return [
    'commands' => [
        \App\command\HelloCommand::class,
    ],
];

应用级命令在所有框架内置命令与插件命令之后注册,因此可覆盖同名的框架或插件命令。

也可以在入口脚本 lee 中手动注册:

php
$console->addCommand(\App\command\HelloCommand::class);

输出样式 ​

php
$output->writeln('<info>成功</info>');
$output->writeln('<comment>提示</comment>');
$output->writeln('<error>错误</error>');

Released under the MIT License.