[英]How do I add artisan to my Package and Improve my Laravel Package Development Workflow?
[英]how to add artisan command to my package laravel
所以我在Laravel 5.4中開發了一個自定義程序包,我添加了我的路線,我的控制器,但是我不知道如何添加一個工匠命令,這是我的代碼:
namespace MyPackage;
use Illuminate\Support\ServiceProvider;
class MyPackageServiceProvider extends ServiceProvider
{
/**
* Bootstrap the application services.
*
* @return void
*/
public function boot()
{
include __DIR__.'/routes.php';
}
/**
* Register the application services.
*
* @return void
*/
public function register()
{
$this->app->make('MyPackage\Controllers\MyPackageServiceController');
}
}
通常,在通常情況下,我會添加一個新的artisan命令並將其添加到app / Console / Commands / Kernel.php中 ,那么如何在我的程序包中執行此操作?
我在項目中的實現方式是在app/Console/Kernel.php
添加了一個Kernel.php
類,該類擴展了ConsoleKernel
,然后在app/Console/Commands/
中的單獨類中添加了命令及其功能,其中一個受保護的變量簽名protected $signature = 'import:zip {path : Zip code file path}';
其中包含命令,我將在此處發布kernel.php和一個命令文件。
這就是我的kernel.php的樣子
命名空間App \\ Console;
使用Illuminate \\ Console \\ Scheduling \\ Schedule; 使用Illuminate \\ Foundation \\ Console \\ Kernel作為ConsoleKernel;
class Kernel extends ConsoleKernel
{
/**
* The Artisan commands provided by your application.
*
* @var array
*/
protected $commands = [
\App\Console\Commands\ProcessZip5UniqueChanges::class,
\App\Console\Commands\ProcessZip5NonUniqueChanges::class,
\App\Console\Commands\ImportZipCodes::class
];
/**
* Define the application's command schedule.
*
* @param \Illuminate\Console\Scheduling\Schedule $schedule
* @return void
*/
protected function schedule(Schedule $schedule)
{
// $schedule->command('inspire')
// ->hourly();
}
/**
* Register the Closure based commands for the application.
*
* @return void
*/
protected function commands()
{
require base_path('routes/console.php');
}
}
這是我的命令\\App\\Console\\Commands\\ProcessZip5UniqueChanges::class
命名空間App \\ Console \\ Commands;
使用Illuminate \\ Console \\ Command; 使用Illuminate \\ Database \\ QueryException;
class ImportZipCodes extends Command
{
/**
* The name and signature of the console command.
*
* @var string
*/
protected $signature = 'import:zip {path : Zip code file path}';
/**
* The console command description.
*
* @var string
*/
protected $description = 'Import zip codes';
/**
* Create a new command instance.
*
* @return void
*/
public function __construct()
{
parent::__construct();
}
/**
* Execute the console command.
*
* @return mixed
*/
public function handle()
{
$path = $this->argument('path');
if(file_exists($path)){
$this->import($path);
} else {
$this->error('File not exists');
}
}
現在我可以運行php artisan import:zip /path/to/the/uploaded/file
,希望php artisan import:zip /path/to/the/uploaded/file
幫助
編輯
打包時請嘗試這樣,因為命令應該自動加載
namespace Vendor\Package;
class MyServiceProvider extends ServiceProvider {
protected $commands = [
'Vendor\Package\Commands\MyCommand::Class',
'Vendor\Package\Commands\FooCommand::Class',
'Vendor\Package\Commands\BarCommand::Class',
];
public function register(){
$this->commands($this->commands);
}
}
您可以像其他任何類一樣將其綁定,但是要使用prefix command.
到實際的命令名稱。
如果您的命令名稱是mycommand
,那么注冊就是這樣。
$this->app->singleton('command.mycommand', function () {
return new MyCommand;
});
根據laravel如何注冊命令來找到此信息。
vendor/laravel/framework/src/Illuminate/Foundation/Providers/ArtisanServiceProvider.php
對於Laravel 5.3及更高版本,在PackageServiceProvider的啟動方法中添加以下內容。 確保將“ SetupCommand”更改為您的命令類名稱
if ($this->app->runningInConsole()) {
$this->commands([
SetupCommand::class
]);
}
確保您已導入命令,如
use vendor\package\Commands\SetupCommand;
安裝軟件包后,您將在php artisan命令列表中找到命令
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.