Skip to content

Instantly share code, notes, and snippets.

@orange634nty
Created January 13, 2018 12:56
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save orange634nty/85921162d45fa51862befa4916553b99 to your computer and use it in GitHub Desktop.
Save orange634nty/85921162d45fa51862befa4916553b99 to your computer and use it in GitHub Desktop.
laravelコマンド実行方法比較
<?php
namespace App\Console\Commands;
use Illuminate\Console\Command;
use \Artisan;
class CallCommand extends Command
{
/**
* The name and signature of the console command.
*
* @var string
*/
protected $signature = 'command:call';
/**
* The console command description.
*
* @var string
*/
protected $description = 'Command description';
/**
* Create a new command instance.
*
* @return void
*/
public function __construct()
{
parent::__construct();
}
/**
* Execute the console command.
*
* @return mixed
*/
public function handle()
{
// run called command
$this->info('***** run command using exec *****');
$this->info('execResult: ' . exec('php artisan command:called --path=exec') . PHP_EOL);
$this->info('***** run command using call *****');
$this->info('callResult: ' . $this->call('command:called', ['--path' => 'call']) . PHP_EOL);
$this->info('***** run command using artisan *****');
$this->info('artisanResult: ' . Artisan::call('command:called', ['--path' => 'artisan']) . PHP_EOL);
}
}
<?php
namespace App\Console\Commands;
use Illuminate\Console\Command;
class CalledCommand extends Command
{
/**
* The name and signature of the console command.
*
* @var string
*/
protected $signature = 'command:called {--path=default}';
/**
* The console command description.
*
* @var string
*/
protected $description = 'Command description';
/**
* Create a new command instance.
*
* @return void
*/
public function __construct()
{
parent::__construct();
}
/**
* Execute the console command.
*
* @return mixed
*/
public function handle()
{
$path = $this->option('path');
echo 'run child command, path: ' . $path . PHP_EOL;
$this->info('run child command, path: ' . $path);
$this->error('throw error on child command, path: ' . $path);
}
}
$ php artisan command:call
***** run command using exec *****
execResult: throw error on child command, path: exec
***** run command using call *****
run child command, path: call
run child command, path: call
throw error on child command, path: call
callResult: 0
***** run command using artisan *****
run child command, path: artisan
artisanResult: 0
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment