zoukankan      html  css  js  c++  java
  • Laravel5.1 自定义Artisan命令

    如果我们想生成自己的artisan命令 首先在cd到项目目录生成console:

    php artisan make:console TestArtisan

    这行命令就会生成一个名为 TestArtisan 的console,我们在这个目录就可以找到它:appConsoleCommands:

    class TestArtisan extends Command
    {
        /**
         * The name and signature of the console command.
         *
         * @var string
         */
        protected $signature = 'command:name';
    
        /**
         * The console command description.
         *
         * @var string
         */
        protected $description = 'Command description';
    
        /**
         * Create a new command instance.
         *
         * @return void
         */
        public function __construct()
        {
            parent::__construct();
        }
    
        /**
         * Execute the console command.
         *
         * @return mixed
         */
        public function handle()
        {
            //
        }
    }

    自定义命令

    $signature 这个变量就是我们在命令行中输入的命令 比如make:controller,make:model等,我们将它改为我们想要的命令:

        protected $signature = 'learn_laravel:console';

    $description这个变量没有什么好说的,就是说明:

        protected $description = 'learn how to customize commands';

    具体的业务逻辑是定义在handle方法中的,我们先来定义简单的打印一串文字:

        public function handle()
        {
            $this->info('test Hello');
        }

    最后一步就是将这个console注册,我们在appConsoleKernal.php文件中注册:

        protected $commands = [
            AppConsoleCommandsInspire::class,
            // 注册我们自己的命令
            AppConsoleCommandsTestArtisan::class,
        ];

    如何查看我们有没有注册成功呢?我们使用 php artisan 命令就会看到一大堆的命令,这其中如果包含了我们自定义的artisan,那么就注册成功了。

    我们现在可以使用我们自定义的命令来测试了:

    php artisan learn_laravel:console

    传入参数

    在诸多的artisan命令中 我们常常会传入一些参数 比如:php artisan make:model Articles。那么如何传入参数呢?我们来修改下$signature:

        protected $signature = 'learn_laravel:console {arguments}';

    没错,只需要添加一个{}里面跟上参数名就可以了。

    我们可以在handle方法中接收参数:

        public function handle()
        {
            $this->info('test Hello '.$this->argument('arguments'));
        }

    可选参数

    当我们指定了参数后就必须传入参数,在有些时候参数是可以不设置的,那么我们需要这么写:

        protected $signature = 'learn_laravel:console {arguments?}';
        public function handle()
        {
            $this->info('test Hello '.$this->argument('arguments'));
        }

    这样就不会报错了。

    默认参数

    如果指定了默认参数值,当传入参数后使用传入后的参数,没有指定则使用默认值:

        protected $signature = 'learn_laravel:console {arguments=default}';
  • 相关阅读:
    使用Fiddle修改请求数据
    Fiddle抓包应用概述
    s = -1 #作用域之外的数字是不会改的
    python list.reverse() 方法 不可以 ss = li.reverse() ,这个列表翻转方法没有返回值, ss=None
    python 两个tuple元组之间连接判断是否有一个为空, and 和 & ,只能用and 不能用&
    http 协议最大url是不限制的,但实际上不会很长,有服务器的限制
    这个居然也可以python >>>geturl()["a"]
    python的字典可以这样子 print(dic["ab"+cd]) 震惊!!!
    mysql 远程 死活连不上 阿里云搞得个什么鬼
    txt默认的是个什么格式,anex,什么的,另存为utf-8或者unicode中文就不乱了
  • 原文地址:https://www.cnblogs.com/Alex-sk/p/6593257.html
Copyright © 2011-2022 走看看