tp5命令行基础介绍
- 作者: 五速梦信息网
- 时间: 2026年03月05日 13:17
- 查看指令
- 生成模块
- 生成文件
- 生成类库映射文件
- 生成路由缓存文件
- 生成数据表字段缓存文件
- 指令扩展示例
- 命令行调试
- 命令行颜色支持
- 调用命令
查看指令
php.exePath
think// 定义项目路径
define('APP_PATH', './application/');
// 加载框架命令行引导文件
require './thinkphp/console.php';
think
thinkphp think
会显示当前支持的所有指令:
>php think
Think Console version 0.1
Usage:
command [options] [arguments]
Options:
-h, --help Display this help message
-V, --version Display this console version
-q, --quiet Do not output any message
--ansi Force ANSI output
--no-ansi Disable ANSI output
-n, --no-interaction Do not ask any interactive question
-v|vv|vvv, --verbose Increase the verbosity of messages: 1 for normal output, 2 for more verbose output and 3 for debug
Available commands:
build Build Application Dirs
help Displays help for a command
list Lists commands
make
make:controller Create a new controller class
make:model Create a new model class
optimize
optimize:autoload Optimizes PSR0 and PSR4 packages to be loaded with classmaps too, good for production.
PowerShell
使用
>php think list
可以获得相同的结果。
如果输入一个不存在的指令,系统会自动搜索相关的指令并提示,例如:
>php think make
会显示
生成模块
testapplicationbuild.phpreturn [
// 定义test模块的自动生成
'test' => [
'__dir__' => ['controller', 'model', 'view'],
'controller' => ['User', 'UserType'],
'model' => ['User', 'UserType'],
'view' => ['index/index', 'index/test'],
],
];
然后在命令行下面,执行:
>php think build
Successed
如果显示Successed则表示生成成功。
>php think Build
applicationtesttest
├─controller
│ ├─Index.php
│ ├─User.php
│ └─UserType.php
├─model
│ ├─User.php
│ └─UserType.php
├─view
│ └─index
│ ├─index.html
│ └─test.html
├─common.php
└─config.php
接下来,我们可以访问
http://tp5.com/test/
会显示:

build.phpIndexIndex
生成文件
makephp think make:controller test/Blog
会自动为test模块生成一个 Blog控制器文件。
注意,默认生成的控制器类是属于资源控制器,并且继承了\think\Controller。
如果希望生成一个空的控制器,可以使用
php think make:controller test/Blog --plain
又或者生成一个模型文件
php think make:model test/Blog
生成类库映射文件
在生成类库文件之后,我们强烈建议使用命令行生成类库映射文件,可以提高自动加载的性能,用法如下:
>php think optimize:autoload
RUNTIME_PATHclassmap.php
生成路由缓存文件
如果你的应用定义了大量的路由规则,那么建议在实际部署后生成路由缓存文件,可以免去路由注册的开销,从而改善路由的检测效率,用法如下:
>php think optimize:route
RUNTIME_PATHroute.php
注意
路由缓存文件只会缓存在application/route.php文件中配置和动态注册的路由规则,因此请确保你没有在其它的文件中进行路由的注册。
生成数据表字段缓存文件
如果你希望提高查询的性能,可以通过生成字段缓存来减少查询。用法如下:
>php think optimize:schema
RUNTIME_PATHdatabase.table.php
也可以指定数据库生成字段缓存(必须有用户权限),例如,下面指定生成demo数据库下面的所有数据表的字段缓存信息。
php think optimize:schema --db demo
还可以读取模块的模型类来生成数据表字段缓存(这个适合多数据库连接的情况),如下:
php think optimize:schema --module index
会读取index模块的模型来生成数据表字段缓存。
注意
如果模型类没有继承think\Model或者是抽象类的话,不会生成对应模型的字段缓存。
更新数据表字段缓存也是同样的方式,每次执行都会重新生成缓存。如果需要单独更新某个数据表的缓存,可以使用:
php think optimize:schema --table think_user
支持指定数据库名称
php think optimize:schema --table demo.think_user
指令扩展示例
clear
clearclear
app\console\Clear<?php
namespace app\console;
use think\console\Command;
use think\console\Input;
use think\console\input\Option;
use think\console\Output;
class Clear extends Command
{
protected function configure()
{
// 指令配置
$this
->setName('clear')
->addOption('path', 'd', Option::VALUE_OPTIONAL, 'path to clear', null)
->setDescription('Clear runtime file');
}
protected function execute(Input $input, Output $output)
{
$path = $input->getOption('path') ?: RUNTIME_PATH;
$files = scandir($path);
if ($files) {
foreach ($files as $file) {
if ('.' != $file && '..' != $file && is_dir($path . $file)) {
array_map('unlink', glob($path . $file . '/*.*'));
} elseif (is_file($path . $file)) {
unlink($path . $file);
}
}
}
$output->writeln("Clear Successed");
}
}
think\console\command\Commandconfigureexecute
command.phpreturn [
'\app\console\Clear',
];
ClearlistClear>php think list
运行后如果看到

表示指令注册成功,接下来可以测试下该指令:
>php think clear
Clear Successed
该指令并不会删除目录,仅仅删除目录下面(包括子目录)的文件。
clear--path>php think clear --path d:\www\tp5\runtime\log\
Clear Successed
--path-d>php think clear -d d:\www\tp5\runtime\log\
Clear Successed
命令行调试
-vpathp>php think clear
会显示如下错误:
我们需要查看具体的Trace信息,可以使用
>php think clear -v
会看到类似下面的错误信息:

命令行颜色支持
think
windows 10.0.10580
color<?php
namespace app\console;
use think\console\command\Command;
use think\console\Input;
use think\console\Output;
use think\console\output\formatter\Style;
class Color extends Command
{
protected function configure()
{
$this
->setName('color')
->setDescription('Show Color text');
}
protected function execute(Input $input, Output $output)
{
// 输出info样式
$output->writeln("<info>this is info</info>");
// 输出error样式
$output->writeln("<error>this is error</error>");
// 输出comment样式
$output->writeln("<comment>this is comment</comment>");
// 输出question样式
$output->writeln("<question>this is question</question>");
// 输出highlight样式
$output->writeln("<highlight>this is highlight</highlight>");
// 输出warning样式
$output->writeln("<warning>this is warning</warning>");
// 输出混合样式
$output->writeln("this is <info>info</info>, this is <error>error</error>,this is <comment>comment</comment>,this is <question>question</question>,this is <highlight>highlight</highlight>, this is <warning>warning</warning>");
// 自定义输出样式
$output->getFormatter()->setStyle('custom', new Style('black', 'white'));
$output->writeln("<custom>this is style</custom>");
}
}
command.phpreturn [
'\app\console\Clear',
'\app\console\Color',
];
然后执行下面的指令:
>php think color
运行后就可以看到:

调用命令
在代码里面可以直接调用执行命令行的某个命令,例如:
namespace app\index\controller;
use think\Console;
class Index
{
public function index()
{
// 调用命令行的指令
$output = Console::call('make:model',['index/Blog']);
return $output->fetch();
}
}
Console::call('build',['--module', 'demo']);
当访问
http://tp5.com
页面会输出
Model created successfully.
application/index/model/Blog
当我们再次刷新页面的话,会看到页面输出
Model already exists!
表示模型已经创建过了,无需再次创建。
Console::call
相关文章
-
TP5使用Composer安装phpofficephpspreadsheet,导出Excel文件
TP5使用Composer安装phpofficephpspreadsheet,导出Excel文件
- 互联网
- 2026年03月05日
-
tp6 ueditor编辑器
tp6 ueditor编辑器
- 互联网
- 2026年03月05日
-
TreeView树形控件递归绑定数据库里的数据
TreeView树形控件递归绑定数据库里的数据
- 互联网
- 2026年03月05日
-
Tosca 添加 modules,添加Library,引用重复步骤
Tosca 添加 modules,添加Library,引用重复步骤
- 互联网
- 2026年03月05日
-
TortoiseGit与github实现项目的上传
TortoiseGit与github实现项目的上传
- 互联网
- 2026年03月05日
-
Torbrowser官网博客
Torbrowser官网博客
- 互联网
- 2026年03月05日


