繁体   English   中英

Laravel 6 Artisan 使特性自定义命令不起作用

[英]Laravel 6 Artisan make traits custom command not working

我想使用 php artisan 命令创建一个特征,但不知道为什么它不起作用。

应用程序/控制台/存根/trait.stub

namespace App\Traits;

trait DummyTrait
{

}

应用程序/控制台/命令/TraitMakeCommand.php

namespace App\Console\Commands;

use Illuminate\Console\Command;
use function app_path;

class TraitMakeCommand extends Command
{
    /**
     * The name and signature of the console command.
     *
     * @var string
     */
    protected $signature = 'make:trait {name : Traits name you want to use.}';

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

    /**
     * The type of class being generated.
     *
     * @var string
     */
    protected $type = 'Trait';

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

    /**
     * Execute the console command.
     *
     * @return mixed
     */
    public function handle()
    {
        return $this->getStub();
    }

    /**
     * Get the stub file for the generator.
     *
     * @return string
     */
    protected function getStub()
    {
        return app_path('Console/Stubs/trait.stub');
    }

    /**
     * Get the default namespace for the class.
     *
     * @param string $rootNamespace
     *
     * @return string
     */
    protected function getDefaultNamespace($rootNamespace)
    {
        return $rootNamespace . '\Traits';
    }
}

应用程序/控制台/Kernel.php

class Kernel extends ConsoleKernel
{

    ...

    /**
     * The Artisan commands provided by your application.
     *
     * @var array
     */
    protected $commands = [
        Commands\TraitMakeCommand::class,
    ];

    ...
}

工匠输出: pa make:trait -h

Description:
  Create a new trait

Usage:
  make:trait <name>

Arguments:
  name                  Traits name you want to use.

Options:
  -h, --help            Display this help message
  -q, --quiet           Do not output any message
  -V, --version         Display this application version
      --ansi            Force ANSI output
      --no-ansi         Disable ANSI output
  -n, --no-interaction  Do not ask any interactive question
      --env[=ENV]       The environment the command should run under
  -v|vv|vvv, --verbose  Increase the verbosity of messages: 1 for normal output, 2 for more verbose output and 3 for debug

您可以使用GeneratorCommand为存根生成文件。 这个基类实现了大部分逻辑。 所有自定义设置都可以通过覆盖某些方法来实现。 看看下面的例子:

应用程序/控制台/命令/TraitMakeCommand.php

namespace App\Console\Commands;

use Illuminate\Console\GeneratorCommand;

class TraitMakeCommand extends GeneratorCommand
{
    protected $name = 'make:trait';
    /**
     * The console command description.
     *
     * @var string
     */
    protected $description = 'Create a new trait';
    /**
     * The name and signature of the console command.
     *
     * @var string
     */
    protected $signature = 'make:trait {name : Traits name you want to use.}';


    /**
     * The type of class being generated.
     *
     * @var string
     */
    protected $type = 'Trait';

    /**
     * Get the stub file for the generator.
     *
     * @return string
     */
    protected function getStub()
    {
        return app_path('Console/Stubs/trait.stub');
    }


    /**
     * Get the default namespace for the class.
     *
     * @param string $rootNamespace
     *
     * @return string
     */
    protected function getDefaultNamespace($rootNamespace)
    {
        return $rootNamespace . '\Traits';
    }
}

并更新存根:

应用\\控制台\\存根\\trait.stub

<?php

namespace App\Traits;

trait DummyClass
{

}

然后用法是:

php artisan make:trait Example

这将导致文件: app/Traits/Example.php具有以下内容:

<?php

namespace App\Traits;

trait Example
{

}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM