laravel博客项目实战5--自定义命令

213 阅读2分钟

有的时候我们需要命令行进行操作,比如定时给用户发邮件通知短信通知,我们写个命令行脚本,加个定时任务,搞定!那么laravel中怎么写命令行脚本的呢?听我慢慢讲! 我们觉得每次手动创建repository层太麻烦,我们定义个命令用于创建我们的repository层。执行一下命令

php artisan make:command Repository

我们的命令放在了


app/Console/Commands/Repository.php

内容如下


<?php

namespace App\Console\Commands;

use Illuminate\Console\Command;

class Repository extends Command
{
    /**
     * The name and signature of the console command.
     *
     * @var string
     */
    protected $signature = 'make:repository {repository?}';

    /**
     * The console command description.
     *
     * @var string
     */
    protected $description = 'Create Repository';

    /**
     * Create a new command instance.
     *
     * @return void
     */
    public function __construct()
    {
        parent::__construct();
    }

    /**
     * Execute the console command.
     *
     * @return mixed
     */
    public function handle()
    {
    }
}


这句话代表了我们命令的名称,{repository?}表示是个可选参数,如果是必选参数,可以去掉“?”


    protected $signature = 'make:repository {repository}';

这句话是对命令的描述,告诉大家这个命令是做什么用的

    protected $description = 'Create Repository';

这里表示我们命令的主体部分,可以写命令的内容


    public function handle()
    {
    
    }

我们有时候需要控制台进行交互,这句话,可以通过控制台交互接受命令行内容:

   $name = $this->ask('请输入repository的名字,目录采用/分割');


我们最终代码如下所示


<?php

namespace App\Console\Commands;

use Illuminate\Console\Command;

class Repository extends Command
{
    /**
     * The name and signature of the console command.
     *
     * @var string
     */
    protected $signature = 'make:repository {repository?}';

    /**
     * The console command description.
     *
     * @var string
     */
    protected $description = 'Create Repository';

    /**
     * Create a new command instance.
     *
     * @return void
     */
    public function __construct()
    {
        parent::__construct();
    }

    /**
     * Execute the console command.
     *
     * @return mixed
     */
    public function handle()
    {
        $name = $this->ask('请输入repository的名字,目录采用/分割');
        $arr = explode('/', str_replace('\\', '/', $name));
        //
        $name = ucfirst($arr[count($arr) - 1]);
        $name = rtrim($name, 'Repository');
        if (count($arr) > 1) {
            array_pop($arr);
            $space = join('\\', $arr);
            $tmp = app_path() . "/Repository";
            $namespace = 'App\Repository\\' . $space;
            foreach ($arr as $value) {
                $tmp = $tmp . '/' . $value;
                if (!file_exists($tmp)) {
                    mkdir($tmp);
                }
            }
            $filename = app_path() . "/Repository/{$space}/{$name}Repository.php";
        } else {
            $namespace = 'App\Repository';
            $filename = app_path() . "/Repository/{$name}Repository.php";
        }
        $tpl = <<<EOF
<?php

namespace $namespace;


class {$name}Repository
{

}

EOF;
        if (PHP_OS === "WINNT") {
            $filename = str_replace('/', '\\', $filename);
        }
        if (!file_exists($filename)) {
            file_put_contents($filename, $tpl);
        }

    }
}


我们执行命令行

php artisan make:repository

我们输入Api\v1\HanyunRepository,就创建了我们子的repository文件


app/Repository/Api/v1/HanyunRepository.php

终于可以愉快的编码了,再也不用手动创建repository了O(∩_∩)O哈哈~。具体命令行怎么使用,大家需要灵活运用!