可以像laravel那樣使用 Artisan::call('自定義命令');來在代碼中使用自定義命令,不了解什么是自定義命令的,可以查看:http://m.wtbis.cn/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
效果如下,可以使用代碼調(diào)用 config:mysql
// 每隔10秒執(zhí)行一次
\Workerman\Timer::add(10, function () {
CommandUtil::call("config:mysql");
});
復(fù)制到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);
}
}
/**
* 執(zhí)行命令
* @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);
}
/**
* 調(diào)用命令的靜態(tài)方法
* @param string $commandStr
* @return void
* @throws Throwable
*/
public static function call(string $commandStr)
{
$command = new self();
$command->handle($commandStr);
}
}
此方法只能實現(xiàn)在后端調(diào)用,也就是不能完全像laravel那樣,可以通過訪問瀏覽器的某個控制器中的方法來調(diào)用,會報錯,報錯如下:
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
有報錯我就重寫了啊,我給你發(fā)我的composer.json得了
"php": ">=7.4",
"workerman/webman-framework": "^1.5.0",
"monolog/monolog": "^2.0",
"webman/console": "^1.3",
"webman/admin": "^0.6.33",
"webman/event": "^1.0",
"mathieuviossat/arraytotexttable": "^1.0",
"webman/think-cache": "^1.0",
"symfony/cache": "^5.4",
"robmorgan/phinx": "^0.14.0",
"next/var-dumper": "^0.1.0",
"psr/container": "1.1.1",
"php-di/php-di": "6",
"doctrine/annotations": "1.14",
"ratchet/pawl": "^0.4.1",
"workerman/crontab": "^1.0",
"amphp/amp": "^2.6",
"amphp/sync": "^1.4",
"phpseclib/phpseclib": "^3.0",
"topthink/think-template": "^2.0",
"topthink/think-validate": "^2.0",
"symfony/process": "^5.4",
"shopwwi/webman-filesystem": "^1.1",
"phpoffice/phpspreadsheet": "^1.12",
"saithink/laravelorm-log": "^1.0",
"overtrue/wechat": "~5.0",
"webman/redis-queue": "^1.3",
"illuminate/redis": "^8.0",
"webman-tech/symfony-lock": "^2.0"
我這里調(diào)用是沒問題的,下面是代碼
app/command/Test.php