可以使用下面的指令生成一个中间件类。
>php think make:middleware Auth
会自动生成一个 app\middleware\Auth
类文件。
可以使用
>php think make:validate index@User
生成一个 app\index\validate\User
验证器类,然后添加自己的验证规则和错误信息。
如果需要清除应用的缓存文件,可以使用下面的命令:
php think clear
不带任何参数调用clear
命令的话,会清除runtime
目录(包括模板缓存、日志文件及其子目录)下面的所有的文件,但会保留目录。
如果不需要保留空目录,可以使用
php think clear --dir
清除日志目录
php think clear --log
清除日志目录并删除空目录
php think clear --log --dir
清除数据缓存目录
php think clear --cache
清除数据缓存目录并删除空目录
php think clear --cache --dir
如果需要清除某个指定目录下面的文件,可以使用:
php think clear --path d:\www\tp\runtime\log\
假设你的路由定义文件内容为:
Route::get('think', function () { return 'hello,ThinkPHP6!';});Route::resource('blog', 'Blog');Route::get('hello/:name', 'index/hello')->ext('html');
可以使用下面的指令查看定义的路由列表
php think route:list
如果是多应用模式的话,需要改成
php think route:list index
输出结果类似于下面的显示:
+----------------+-------------+--------+-------------+| Rule | Route | Method | Name | +----------------+-------------+--------+-------------+| think | <Closure> | get | | | hello/<name> | index/hello | get | index/hello | | blog | Blog/index | get | Blog/index | | blog | Blog/save | post | Blog/save | | blog/create | Blog/create | get | Blog/create | | blog/<id>/edit | Blog/edit | get | Blog/edit | | blog/<id> | Blog/read | get | Blog/read || blog/<id> | Blog/update | put | Blog/update | | blog/<id> | Blog/delete | delete | Blog/delete | +----------------+-------------+--------+-------------+
并且同时会在runtime目录下面生成一个route_list.php
的文件,内容和上面的输出结果一致,方便你随时查看。
如果你的路由定义发生改变的话, 则需要重新调用该指令,会自动更新上面生成的缓存文件。
支持定义不同的样式输出,例如:
php think route:list box
输出结果变为:
┌────────────────┬─────────────┬────────┬─────────────┐│ Rule │ Route │ Method │ Name │ ├────────────────┼─────────────┼────────┼─────────────┤│ think │ <Closure> │ get │ │ │ hello/<name> │ index/hello │ get │ index/hello ││ blog │ Blog/index │ get │ Blog/index ││ blog │ Blog/save │ post │ Blog/save ││ blog/create │ Blog/create │ get │ Blog/create ││ blog/<id>/edit │ Blog/edit │ get │ Blog/edit ││ blog/<id> │ Blog/read │ get │ Blog/read │ │ blog/<id> │ Blog/update │ put │ Blog/update ││ blog/<id> │ Blog/delete │ delete │ Blog/delete │└────────────────┴─────────────┴────────┴─────────────┘php think route:list box-double
输出结果变为:
╔════════════════╤═════════════╤════════╤═════════════╗║ Rule │ Route │ Method │ Name ║╠────────────────╪─────────────╪────────╪─────────────╣║ think │ <Closure> │ get │ ║║ hello/<name> │ index/hello │ get │ index/hello ║║ blog │ Blog/index │ get │ Blog/index ║║ blog │ Blog/save │ post │ Blog/save ║║ blog/create │ Blog/create │ get │ Blog/create ║║ blog/<id>/edit │ Blog/edit │ get │ Blog/edit ║║ blog/<id> │ Blog/read │ get │ Blog/read ║║ blog/<id> │ Blog/update │ put │ Blog/update ║║ blog/<id> │ Blog/delete │ delete │ Blog/delete ║╚════════════════╧═════════════╧════════╧═════════════╝php think route:list markdown
输出结果变为:
| Rule | Route | Method | Name ||----------------|-------------|--------|-------------|| think | <Closure> | get | || hello/<name> | index/hello | get | index/hello || blog | Blog/index | get | Blog/index || blog | Blog/save | post | Blog/save || blog/create | Blog/create | get | Blog/create || blog/<id>/edit | Blog/edit | get | Blog/edit || blog/<id> | Blog/read | get | Blog/read | | blog/<id> | Blog/update | put | Blog/update || blog/<id> | Blog/delete | delete | Blog/delete |
如果你希望生成的路由列表按照路由规则排序,可以使用
php think route:list -s rule
输出结果变成:
+----------------+-------------+--------+-------------+| Rule | Route | Method | Name | +----------------+-------------+--------+-------------+| blog | Blog/index | get | Blog/index || blog | Blog/save | post | Blog/save | | blog/<id> | Blog/read | get | Blog/read || blog/<id> | Blog/update | put | Blog/update || blog/<id> | Blog/delete | delete | Blog/delete | | blog/<id>/edit | Blog/edit | get | Blog/edit | | blog/create | Blog/create | get | Blog/create || hello/<name> | index/hello | get | index/hello || think | <Closure> | get | |+----------------+-------------+--------+-------------+
同样的,你还可以按照请求类型排序
php think route:list -s method
输出结果变为:
+----------------+-------------+--------+-------------+| Rule | Route | Method | Name | +----------------+-------------+--------+-------------+| blog/<id> | Blog/delete | delete | Blog/delete || think | <Closure> | get | || hello/<name> | index/hello | get | index/hello || blog | Blog/index | get | Blog/index || blog/create | Blog/create | get | Blog/create | | blog/<id>/edit | Blog/edit | get | Blog/edit || blog/<id> | Blog/read | get | Blog/read || blog | Blog/save | post | Blog/save || blog/<id> | Blog/update | put | Blog/update | +----------------+-------------+--------+-------------+
支持排序的字段名包括:
rule
、route
、name
、method
和domain
(全部小写)。
如果你希望看到更多的路由参数和变量规则,可以使用
php think route:list -m
输出结果变为:
+----------------+-------------+--------+-------------+--------+-------------------------+---------+| Rule | Route | Method | Name | Domain | Option | Pattern |+----------------+-------------+--------+-------------+--------+-------------------------+---------+| think | <Closure> | get | | | [] | [] || hello/<name> | index/hello | get | index/hello | | {"ext":"html"} | [] || blog | Blog/index | get | Blog/index | | {"complete_match":true} | [] || blog | Blog/save | post | Blog/save | | {"complete_match":true} | [] || blog/create | Blog/create | get | Blog/create | | [] | [] || blog/<id>/edit | Blog/edit | get | Blog/edit | | [] | [] || blog/<id> | Blog/read | get | Blog/read | | [] | [] || blog/<id> | Blog/update | put | Blog/update | | [] | [] || blog/<id> | Blog/delete | delete | Blog/delete | | [] | [] |+----------------+-------------+--------+-------------+--------+-------------------------+---------+
第一步,创建一个自定义命令类文件,运行指令
php think make:command Hello hello
会生成一个app\command\Hello
命令行指令类,我们修改内容如下:
<?phpnamespace app\command;use think\console\Command;use think\console\Input;use think\console\input\Argument;use think\console\input\Option;use think\console\Output;class Hello extends Command{ protected function configure() { $this->setName('hello') ->addArgument('name', Argument::OPTIONAL, "your name") ->addOption('city', null, Option::VALUE_REQUIRED, 'city name') ->setDescription('Say Hello'); } protected function execute(Input $input, Output $output) { $name = trim($input->getArgument('name')); $name = $name ?: 'thinkphp'; if ($input->hasOption('city')) { $city = PHP_EOL . 'From ' . $input->getOption('city'); } else { $city = ''; } $output->writeln("Hello," . $name . '!' . $city); }}
这个文件定义了一个叫hello
的命令,并设置了一个name
参数和一个city
选项。
第二步,配置config/console.php
文件
<?phpreturn [ 'commands' => [ 'hello' => 'app\command\Hello', ]];
第三步,测试-命令帮助-命令行下运行
php think
输出
Think Console version 0.1Usage: 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 debugAvailable commands: build Build Application Dirs clear Clear runtime file hello Say Hello help Displays help for a command list Lists commands make make:controller Create a new resource 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. optimize:config Build config and common file cache. optimize:schema Build database schema cache.
第四步,运行hello
命令
php think hello
输出
Hello thinkphp!
添加命令参数
php think hello kancloud
输出
Hello kancloud!
添加city
选项
php think hello kancloud --city shanghai
输出
Hello kancloud!From shanghai
注意看参数和选项的调用区别
如果需要生成一个指定的命名空间,可以使用:
php think make:command app\index\Command second
支持在控制器的操作方法中直接调用命令,例如:
<?phpnamespace app\index\controller;use think\facade\Console;class Index{ public function hello($name) { $output = Console::call('hello', [$name]); return $output->fetch(); }}
访问该操作方法后,例如:
http://serverName/index/hello/name/thinkphp
页面会输出
Hello thinkphp!