可以像laravel那样使用 Artisan::call('自定义命令');来在代码中使用自定义命令,不了解什么是自定义命令的,可以查看:https://www.workerman.net/doc/webman/plugin/console.html#%E8%87%AA%E5%AE%9A%E4%B9%89%E5%91%BD%E4%BB%A4
使用自定义命令必须先安装webman/console:
composer require webman/console
效果如下,可以使用代码调用 config:mysql
// 每隔10秒执行一次
\Workerman\Timer::add(10, function () {
CommandUtil::call("config:mysql");
});
复制到php文件中,这里取名为 CommandUtil
<?php
namespace app\common;
use Symfony\Component\Console\Command\LazyCommand;
use Symfony\Component\Console\Input\ArgvInput;
use Symfony\Component\Console\Output\ConsoleOutput;
use Throwable;
use Webman\Console\Command;
use Webman\Console\Util;
class CommandUtil extends Command
{
public function __construct(string $name = 'UNKNOWN', string $version = 'UNKNOWN')
{
parent::__construct($name, $version);
if (is_dir($command_path = Util::guessPath(app_path(), '/command', true))) {
$this->installCommands($command_path);
}
}
/**
* 执行命令
* @param string $commandStr
* @return int
* @throws Throwable
*/
public function handle(string $commandStr): int
{
$command = $this->find($commandStr);
if ($command instanceof LazyCommand) {
$command = $command->getCommand();
}
$input = new ArgvInput();
$output = new ConsoleOutput();
return $this->doRunCommand($command, $input, $output);
}
/**
* 调用命令的静态方法
* @param string $commandStr
* @return void
* @throws Throwable
*/
public static function call(string $commandStr)
{
$command = new self();
$command->handle($commandStr);
}
}
此方法只能实现在后端调用,也就是不能完全像laravel那样,可以通过访问浏览器的某个控制器中的方法来调用,会报错,报错如下:
ErrorException: fwrite(): Write of 127 bytes failed with errno=22 Invalid argument in D:\4.PHP\Code\webman-v1\vendor\symfony\var-dumper\Dumper\AbstractDumper.php:178
我这里调用是没问题的,下面是代码
app/command/Test.php
6
没看出你的和我的有什么区别,应该是不用使用dump方法吧,你这个测试里面没有使用到dump方法的
echo和dump都一样的,都是输出打印文本,只是让你知道我触发了api接口
有报错我就重写了啊,我给你发我的composer.json得了