美文网首页
laravel创建自定义command命令笔记!

laravel创建自定义command命令笔记!

作者: DragonersLi | 来源:发表于2020-09-24 17:53 被阅读0次
    image.png
    创建生成自定义的command命令:

    php artisan make:command LogicCommandApp\Console\Commands下生成LogicCommand.php

    App\Console\Command\LogicCommand.php内容替换如下:
    <?php
    
    namespace App\Console\Commands;
    
    use Illuminate\Console\GeneratorCommand;
    
    class LogicCommand extends GeneratorCommand
    {
        /**
         * The console command name.
         *
         * @var string
         */
        protected $name = 'make:logic';
    
        /**
         * The console command description.
         *
         * @var string
         */
        protected $description = 'Create a new logic class';
    
        /**
         * The type of class being generated.
         *
         * @var string
         */
        protected $type = 'Logic';
    
        /**
         * Get the stub file for the generator.
         *
         * @return string
         */
        protected function getStub()
        {
            return __DIR__.DIRECTORY_SEPARATOR.'Stubs'.DIRECTORY_SEPARATOR.$this->type.'.stub';
        }
    
        /**
         * Get the default namespace for the class.
         *
         * @param  string  $rootNamespace
         * @return string
         */
        protected function getDefaultNamespace($rootNamespace)
        {
            return $rootNamespace.DIRECTORY_SEPARATOR.'Logics';
        }
    }
    
    生成命令的模板文件App\Console\Commands\Stubs\Logic.stub:
    <?php
    // +----------------------------------------------------------------------
    // | 逻辑层【逻辑层】
    // +----------------------------------------------------------------------
    // | Author:
    // +----------------------------------------------------------------------
    // | Date:
    // +----------------------------------------------------------------------
    namespace DummyNamespace;
    
        class DummyClass
        {
    
            /**
             * 构造方法
             * @return string
             */
            public function __construct()
            {
                //just do it...
            }
            /**
             * 列表
             * @return string
             */
            public function index()
            {
                //just do it...
            }
            /**
             * 添加
             * @return string
             */
            public function create()
            {
                //just do it...
            }
            /**
             * 存储
             * @return string
             */
            public function store()
            {
                //just do it...
            }
            /**
             * 编辑
             * @return string
             */
            public function edit()
            {
                //just do it...
            }
            /**
             * 保存
             * @return string
             */
            public function update()
            {
                //just do it...
            }
            /**
             * 详情
             * @return string
             */
            public function show()
            {
                //just do it...
            }
    
            /**
             * 删除
             * @return string
             */
            public function destroy()
            {
                //just do it...
            }
    
        }
    
    App\Console\Kernel.php
        protected $commands = [
            //php artisan make:command commandName  
            Commands\LogicCommand::class,//创建逻辑层命令
        ];
    
    php artisan make:logic TestLogic 根据模板生成自定义的命令:
    <?php
    // +----------------------------------------------------------------------
    // | 逻辑层【逻辑层】
    // +----------------------------------------------------------------------
    // | Author:
    // +----------------------------------------------------------------------
    // | Date:
    // +----------------------------------------------------------------------
    namespace App\Logics;
    
        class HelloLogic
        {
    
            /**
             * 构造方法
             * @return string
             */
            public function __construct()
            {
                //just do it...
            }
            /**
             * 列表
             * @return string
             */
            public function index()
            {
                //just do it...
            }
            /**
             * 添加
             * @return string
             */
            public function create()
            {
                //just do it...
            }
            /**
             * 存储
             * @return string
             */
            public function store()
            {
                //just do it...
            }
            /**
             * 编辑
             * @return string
             */
            public function edit()
            {
                //just do it...
            }
            /**
             * 保存
             * @return string
             */
            public function update()
            {
                //just do it...
            }
            /**
             * 详情
             * @return string
             */
            public function show()
            {
                //just do it...
            }
    
            /**
             * 删除
             * @return string
             */
            public function destroy()
            {
                //just do it...
            }
    
        }
    

    相关文章

      网友评论

          本文标题:laravel创建自定义command命令笔记!

          本文链接:https://www.haomeiwen.com/subject/hylprxtx.html