laravel command命令列

來源:互聯網
上載者:User

標籤:

產生類

為了建立一個新命令,你可以使用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命令列

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.