標籤:
產生類
為了建立一個新命令,你可以使用Artisan中的 command:make 命令產生一個骨架作為你的起點:
產生一個命令類
php artisan command:make FooCommand
預設情況下,產生的類檔案被存放在 app/commands 目錄下,同時你也可以指定自訂目錄和命名空間:
php artisan command:make FooCommand --path=app/classes --namespace=Classes
註冊命令
一旦你的命令完成後,你需要使用 Artisan 進行註冊,這樣才能夠被使用。這通常在 app/start/artisan.php檔案中完成。在這個檔案中,你可以使用 Artisan::add 函數註冊命令:
註冊一個 Artisan 命令
Artisan::add(new CustomCommand);
如果你的命令在應用程式的 IoC 容器 中註冊,你可以使用 Artisan::resolve 函數使它對 Artisan 可用:
註冊一個在 IoC 容器中的命令
Artisan::resolve(‘binding.name‘);
一個發郵件範例:
<?phpuse Illuminate\Console\Command;use Symfony\Component\Console\Input\InputOption;use Symfony\Component\Console\Input\InputArgument;class SendMailCommand extends Command { /** * The console command name. * * @var string */ protected $name = ‘SendMailCommand:sendMail‘;//命令名,命令列調用使用php artisian 這裡的$name值 /** * The console command description. * * @var string */ protected $description = ‘send mail.‘; /** * Create a new command instance. * * @return void */ public function __construct() { parent::__construct(); } /** * Execute the console command. * * @return mixed */ public function fire() { // $command=" C:/sendEmail -f from.sina.com -t [email protected] -s smtp.sina.com -xu username -xp pasword -u test "; $message=$this->argument(‘message‘); $str="$command -m $message "; $this->info($str); system($str); $this->info("It‘s Done, have a good day."); } /** * Get the console command arguments. * * @return array */ protected function getArguments() { return array( array(‘message‘, InputArgument::REQUIRED, ‘An example argument.‘), ); } /** * Get the console command options. * * @return array */ protected function getOptions() { return array( //array(‘example‘, null, InputOption::VALUE_OPTIONAL, ‘An example option.‘, null), ); }}
參考:
http://www.golaravel.com/docs/4.0/commands/
https://phphub.org/topics/34
http://www.sitepoint.com/create-laravel-css-minify-command/
laravel command命令列