php - Laravel 6 Artisan 使特征自定义命令不起作用
问题描述
我想使用 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
{
}
推荐阅读
- php - 错误消息:调用未定义的方法 CI_DB_mysqli_result::results()
- python - 在方法中的for循环中更改实例属性
- python - 如何使用python selenium单击iframe内的按钮
- css - 计算器屏幕上的大数字扩大了边界
- php - PHP:如何在保留另一个页面的数据的同时重定向到同一页面
- keyboard-shortcuts - iterm2 用于在窗口之间导航的键盘快捷键
- sql-server - 如何根据参数使用不同的“where”子句?
- ruby-on-rails - 基于相对光标的分页在 Shopify api gem 中不起作用
- bash - BASH:我必须循环 20 miljon 文件并验证它们
- java - Maven 验证构建失败但 0 次失败