美文网首页
Lumen创建自定义make命令

Lumen创建自定义make命令

作者: 墨丘利lh | 来源:发表于2019-10-15 10:22 被阅读0次

    1:检查框架自带 artisan 支持的make命令

    php artisan list

    系统自带的 artisan make 命令对应的PHP程序放在 Illuminate\Foundation\Console 目录下,我们参照 Illuminate\Foundation\Console\ProviderMakeCommand 类来定义自己的 artisan make:controller 命令

    2:创建命令类

    创建 \app\Console\Commands\ControllerMakeCommand.php 文件

    代码如下

    /**

    * Created by PhpStorm.

    * Date: 2019/10/15

    * Time: 10:13

    */

    namespace App\Console\Commands;

    use Illuminate\Console\GeneratorCommand;

    class ControllerMakeCommand extends GeneratorCommand {

        /**

    * create a user defined controller.

    *

        * @var string

    */

        protected $name = 'make:controller';  // @todo:要添加的命令

        /**

    * The console command description.

    *

        * @var string

    */

        protected $description = 'Create a new lumen controller '; // @todo: 命令描述

        /**

    * The type of class being generated.

    *

        * @var string

    */

        protected $type = 'Controller';  // command type

        /**

    * Get the stub file for the generator.

    *

        * @return string

    */

        protected function getStub() {

            return dirname(__DIR__) . '/stubs/controller.stub';  // @todo: 要生成的文件的模板

        }

        /**

    * Get the default namespace for the class.

    *

        * @param  string  $rootNamespace

        * @return string

    */

        protected function getDefaultNamespace($rootNamespace) {

            return $rootNamespace . '\Http\Controllers';//@todo:这里是定义要生成的类的命名空间

        }

    }

    3:创建命令类对应的模版文件

    创建模版文件 app\Console\stubs\controller.stub 文件( make 命令生成的类文件的模版),用来定义要生成的类文件的通用部分:

    namespace  DummyNamespace;

    use App\Http\Controllers\Controller;

    class DummyClass extends Controller

    {

    public function index() {

    }

    }

    后缀一定要是 .stub 

    4:注册命令类

    将 ControllerMakeCommand 添加到 App\Console\Kernel.php 中

    protected $commands = [ Commands\ControllerMakeCommand::class, ];

    5:查看命令

    php artisan list

    相关文章

      网友评论

          本文标题:Lumen创建自定义make命令

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