【问题标题】:Laravel 6 Artisan make traits custom command not workingLaravel 6 Artisan 使特征自定义命令不起作用
【发布时间】:2020-05-31 02:04:48
【问题描述】:

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

app/Console/Stubs/trait.stub

namespace App\Traits;

trait DummyTrait
{

}

app/Console/Commands/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';
    }
}

app/Console/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

【问题讨论】:

  • 调用它会发生什么?
  • 它运行命令没有错误但什么也不输出。我的意思是不在 app/Traits/ 文件夹下创建 Trait 文件

标签: php laravel


【解决方案1】:

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

app/Console/Commands/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';
    }
}

并更新存根:

app\Console\Stubs\trait.stub

<?php

namespace App\Traits;

trait DummyClass
{

}

然后用法是:

php artisan make:trait Example

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

<?php

namespace App\Traits;

trait Example
{

}

【讨论】:

  • 太棒了。完成了工作。一样东西。它将文件名设置为给定名称,而不是特征名称/。有什么想法吗?
  • 引用 MakeControllerCommand 我已经想出重命名文件和特征名称。感谢您的帮助。
猜你喜欢
  • 2020-01-06
  • 2021-08-11
  • 1970-01-01
  • 2014-01-26
  • 2021-05-10
  • 2017-10-10
  • 1970-01-01
  • 2016-04-01
  • 1970-01-01
相关资源
最近更新 更多