昨天接触到开发任务是使用到了tp5的command,上网找了一下相关的资料,蛮简单的。。。
第一步、先创建一个类,继承Command,此处我在application\sapi\Command\文件夹下创建getData.php文件
<?php namespace app\sapi\Command; use think\console\Command; use think\console\Input; use think\console\Output; class getData extends Command { protected function configure() { $this->setName('getData')//定义命令的名字 ->setDescription('获取线索数据');//定义命令的描述 } protected function execute(Input $input, Output $output) { //具体执行的代码逻辑 $this->getData(); } } 其中 configure和execute方法是默认要有的,大体可以理解为configure是用来注册执行命令,execute是执行具体的代码逻辑。
第二步、注册command,路径是在application\command.php
<?php return [ 'app\sapi\Command\getData', ];
第三步、确认注册的执行命令
cmd中输入php think,如果注册成功的话,就可以看到具体的执行命令
第四步、执行命令
可以在宝塔上添加计划任务,执行shell脚本
su -c "/www/server/php/56/bin/php /www/wwwroot/xxxxxxx.com(项目域名路径)/think getData(任务名称)" -s /bin/sh www
我用的是PHPstorm编辑器,可以使用底部的命令来执行,cmd也可以
command的使用很广泛,具体的使用场景根据自己的需要来定