Write the Code. Change the World.

9月 30

使用 laravel 开发项目的时候, artisan 是个好东西。这个好东西是真的好。在做 command 的时候,您可以方便的输出想要的内容在终端上,比如警告,错误,选择等等。这是因为 command 它继承了Command 类,该类使用了 trait Illuminate\Console\Concerns\InteractsWithIO。而它最终实现了 Symfony\Component\Console\Output\OutputInterface 。 Symfony\Component\Console\Output\ConsoleOutput 是 cli output 的源头类。 只要我们做好 Symfony\Component\Console\Output\ConsoleOutput 的文章,不也可以输出吗。

ConsoleOutput is the default class for all CLI output. It uses STDOUT and STDERR.

来 123, 跟着步骤走起来

# 先生成一个 seeder
php artisan make:seeder TestSeeder

# 在该 seeder 的 run 函数中加入以下代码
    public function run()
    {
        $output = new \Symfony\Component\Console\Output\ConsoleOutput();
        $section = $output->section();
        $section->writeln('人生若只如初见,何事秋风悲画扇');
    }

# 那么开始测试一下
php artisan db:seed --class=TestSeeder

是不是就可以看见你想看见的东东了。当然你可以封装,搞出更多花样。也还有更多命令待使用。

发表回复

您的电子邮箱地址不会被公开。 必填项已用*标注