Laravel 5.1使用命令行模式(artisan)運行php腳本

 

 

Laravel有內置命令調度器,能夠方便的實現Cron.php

任務調度定義在app/Console/Kernel.php文件的schedule方法中,該方法已經包含了一個示例.Laravel裏有兩種方法執行Cron,第一種方法是讓Cron每分鐘調用Laravel命令調度,而後讓Laravel來根據具體的命令來實現;須要在crontab裏面加入以下內容:laravel

1
* * * * * php  /path/to/artisan  schedule:run 1>>  /dev/null  2>&1

本文主要講第二種方法,既Artisan控制檯.Artisan是Laravel自帶的命令行接口名稱,它提供了不少有用的命令想要查看全部可用的Artisan命令,可以使用list命令查看:shell

1
php artisan list

每一個命令均可以用help指令顯示命令描述及命令參數和選項。想要查看幫助界面,只須要在命令前加上help就能夠了,例如:json

1
php artisan help migrate

除了Artisan提供的命令以外,還能夠構建本身的命令。能夠將自定義命令存放在app/Console/Commands目錄;固然,也能夠本身選擇存放位置,只要改命令能夠基於composer.json被自動加載。bash

要建立一個新命令,可使用Artisan命令make:console,好比我要創一個發送郵件的artisan命令,能夠這樣:app

1
php artisan  make :console SendEmails

上述命令將會生成一個類app/Console/Commands/SendEmails.php,當建立命令時,--command選項可用於分配終端命令名(在終端調用命令時用):composer

1
php artisan  make :console SendEmails -- command =emails:send

命令生成之後,須要填寫該類的signaturedescription屬性,這兩個屬性在調用list顯示命令的時候會被用到。handle方法在命令執行時被調用,能夠將全部命令邏輯都放在這個方法裏面,咱們能夠在命令控制器的構造函數中注入任何依賴.這個SendEmails.php裏面內容參考以下:ide

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
<?php  namespace  App\Console\Commands;
 
use  Illuminate\Console\Command;
 
class  SendEmails  extends  Command {
 
     /**
      * The console command name.
      *
      * @var string
      */
     protected  $name  'emails:send' ;
 
     /**
      * The console command description.
      *
      * @var string
      */
     protected  $description  '這是發郵件的命令.' ;
 
     /**
      * Create a new command instance.
      *
      * @return void
      */
     public  function  __construct()
     {
         parent::__construct();
     }
 
     /**
      * Execute the console command.
      *
      * @return mixed
      */
     public  function  handle()
     {
         //TODO:發送郵件邏輯
     }
 
}

$name是這個命令的名稱,即artisan調用時的命令,如本例命令設爲了emails:send,那麼實際調用時要這麼用:函數

1
php artisan emails:send

執行上面這條命令就是執行handle()方法,固然,這裏還漏了一個很是重要的關鍵步驟,那就是須要把命令注入到app/Console/Kernel.php文件中,不然這個命令artisan是找不到的,示例以下:url

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
<?php  namespace  App\Console;
 
use  Illuminate\Console\Scheduling\Schedule;
use  Illuminate\Foundation\Console\Kernel  as  ConsoleKernel;
 
class  Kernel  extends  ConsoleKernel {
 
     /**
      * The Artisan commands provided by your application.
      *
      * @var array
      */
     protected  $commands  = [
         'App\Console\Commands\Inspire' ,
         'App\Console\Commands\SendEmails' ,
     ];
 
     /**
      * Define the application's command schedule.
      *
      * @param  \Illuminate\Console\Scheduling\Schedule  $schedule
      * @return void
      */
     protected  function  schedule(Schedule  $schedule )
     {
         $schedule ->command( 'inspire' )
                  ->hourly();
     }
 
}

關鍵就是在$commands裏面把要用到的SendEmails類放進去,好了,這樣就能夠經過artisan命令來執行腳本了.

因爲artisan命令須要在Laravel的目錄裏面才能執行,因此實際要用crontab調用artisan命令時須要注意Crontab的shell代碼要這樣寫,切記很是重要,不然是執行了不會運行實際處理邏輯的.

1
30 1 * * *    php  /www/projects/laravelapp/artisan  emails:send

上面/www/projects/laravelapp/是項目的路徑

相關文章
相關標籤/搜索