lavavel 定时任务 (command的第二个参数)

之前好像没有写过,记录一下

$schedule->command()方法

第一个参数不用说,可以传纯字符串或者类::class,不过第二个参数确很少人提到

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
/**
     * Add a new Artisan command event to the schedule.
     *
     * @param  string  $command
     * @param  array  $parameters
     * @return \Illuminate\Console\Scheduling\Event
     */
    public function command($command, array $parameters = [])
    {
        if (class_exists($command)) {
            $command = Container::getInstance()->make($command)->getName();
        }
 
        return $this->exec(
            Application::formatCommandString($command), $parameters
        );
    }

 如上第二个参数是数组,看了源码是拼接在命令后面,如果你传二维数组,它也是只会解析排列在命令后面,

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
/**
     * Compile parameters for a command.
     *
     * @param  array  $parameters
     * @return string
     */
    protected function compileParameters(array $parameters)
    {
        return collect($parameters)->map(function ($value, $key) {
            if (is_array($value)) {
                $value = collect($value)->map(function ($value) {
                    return ProcessUtils::escapeArgument($value);
                })->implode(' ');
            } elseif (! is_numeric($value) && ! preg_match('/^(-.$|--.*)/i', $value)) {
                $value = ProcessUtils::escapeArgument($value);
            }
 
            return is_numeric($key) ? $value : "{$key}={$value}";
        })->implode(' ');
    }

 具体是这个方法

1
2
传入 [‘a’, 'b'] 解析完 'a' 'b'
传入 [[‘a’, 'b'], 'c'] 解析完 'a' 'b' 'c'

  

 也可以在方法里触发定时任务

1
Artisan::call("order:log");

  

posted @   程序生(Codey)  阅读(521)  评论(0编辑  收藏  举报
编辑推荐:
· AI与.NET技术实操系列:基于图像分类模型对图像进行分类
· go语言实现终端里的倒计时
· 如何编写易于单元测试的代码
· 10年+ .NET Coder 心语,封装的思维:从隐藏、稳定开始理解其本质意义
· .NET Core 中如何实现缓存的预热?
阅读排行:
· 25岁的心里话
· 闲置电脑爆改个人服务器(超详细) #公网映射 #Vmware虚拟网络编辑器
· 零经验选手,Compose 一天开发一款小游戏!
· 通过 API 将Deepseek响应流式内容输出到前端
· 因为Apifox不支持离线,我果断选择了Apipost!
历史上的今天:
2017-08-16 解决Class 'swoole_server' not found
点击右上角即可分享
微信分享提示