zoukankan      html  css  js  c++  java
  • laravel5.4之artisan使用总结一

    Artisan是laravel自带的命令行接口:

    php artisan list

    编写命令

    生成命令:

    • 可以使用Artisan命令,
    php artisan make:command ConsoleTest

    执行完这个命令后,会在app/Console/Commands 目录下创建ConsoleTest命令类。会包含默认的属性设置以及所有命令都共有的方法。

    • 需要在ConsoleTest填写这个类的signature和description属性。其中的handle方法会在命令执行时被调用。将所有命令逻辑都放在这个方法里面。
    • 在编写好命令后需要在该命令可以通过Artisan CLI执行之前 注册这个命令。

    如下填写这个类的signature和description属性:

    <?php
    
    namespace AppConsoleCommands;
    
    use AppHttpControllersSettlementController;
    use IlluminateConsoleCommand;
    
    class ConsoleTest extends Command
    {
        /**
         * The name and signature of the console command.
         *
         * @var string
         */
        protected $signature = 'console:test';
    
        /**
         * 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()
        {
           //TODO 需要的一些逻辑
    
        }
    }

    闭包命令:

    在app/Console/Kernel.php文件的comands方法中,Laravel加载了routes/console.php文件。

    <?php
    
    namespace AppConsole;
    
    use IlluminateConsoleSchedulingSchedule;
    use IlluminateFoundationConsoleKernel as ConsoleKernel;
    
    class Kernel extends ConsoleKernel
    {   /**
         * Register the Closure based commands for the application.
         *
         * @return void
         */
        protected function commands()
        {
            require base_path('routes/console.php');
        }
    }

    注册命令:

    • 在命令编写后,需要注册到Artisan才可以使用,需要在app/Console/Kernel.php 文件中完成。
    • 在这个文件中的commands属性中,在里面注册命令,只需要将上面ConsoleTest所在路径写上,如下:
    /**
         * The Artisan commands provided by your application.
         *
         * @var array
         */
        protected $commands = [
            'AppConsoleCommandsConsoleTest'
        ];
    • 最后在终端里cd到项目根目录下,执行如下命令
    php artisan console:test

    会去执行上面ConsoleTest 中的handle()方法。

  • 相关阅读:
    OC-数组类
    OC-字符串函数
    C——位操作
    C——字符串练习
    C语言——指针习题
    指针数组和数组指针
    数组指针和指针数组的区别
    cocos2d-iphone中兼容iphone/ipad的问题
    一个Universal程序还是iPhone、iPad俩个版本 ?
    objective-c获取自1970年以来的毫秒数
  • 原文地址:https://www.cnblogs.com/linst/p/8044971.html
Copyright © 2011-2022 走看看