laravel command命令行 - youxin - 博客园

生成类

为了创建一个新命令,你可以使用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');

复制代码; "复制代码")

<?php use 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 to@qq.com  -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),

);

}

}

复制代码; "复制代码")

运行:

php artisan  commanname argx --option=bar --option=baz

参考:

http://www.golaravel.com/docs/4.0/commands/

https://phphub.org/topics/34

http://www.sitepoint.com/create-laravel-css-minify-command/


Original url: Access
Created at: 2018-12-20 13:43:03
Category: default
Tags: none

请先后发表评论
  • 最新评论
  • 总共0条评论