2016-12-01 50 views
1

I am following this tutorial每分鐘安排一項功能。以下是我在localhost上的代碼。任務計劃:Laravel 5.3

class Kernel extends ConsoleKernel 
{ 
    protected $commands = [ 
     'App\Console\Commands\Inspire', 
    ]; 

    protected function schedule(Schedule $schedule) 
    { 
     $schedule->call($this->WriteFile())->everyMinute(); 
    } 

    protected function commands() 
    { 
     require base_path('routes/console.php'); 
    } 

    private function WriteFile() { 
     $myfile = fopen("newfile.txt", "w") or die("Unable to open file!"); 
     $txt = "John Doe\n"; 
     fwrite($myfile, $txt); 
     fclose($myfile); 
    } 
} 

我看到txt文件沒有顯示內容。我把公共文件夾中的txt文件。我錯過了什麼嗎?

+0

您是否添加了cron項到你的服務器? – istaro

+0

這是關於localhost的。 – Pankaj

回答

2

在文件console.phpConsole Routes我有這個命令:

Artisan::command('writeFile', function() { 
    Storage::disk('local')->put('file.txt', 'this is text !'); 
}); 
Kernal.php

我有這樣的:

protected function schedule(Schedule $schedule) 
{ 
    $schedule->command('writeFile') 
      ->everyMinute(); 
} 

還需要運行:php artisan schedule:run

文件將在路徑和文件將在路徑:"/storage/app/file.txt"

可以讀取文件:Storage::get('file.txt');

工作得很好,我..希望這將與您攜手:)

+0

它應該是這樣的:'Artisan :: command('writeFile',function($ project)Storage :: disk('local') - > put('newfile.txt',$ txt); }) ;' 請點擊此鏈接查看[更多](https://laravel.com/docs/5.3/artisan) –

+0

您需要添加cron作業才能使'php artisan schedule:run'每分鐘運行 –

+0

需要編輯你的'crontab'使用cmd命令'crontab -e'然後添加這個命令'* * * * * php/path/to/artisan schedule:run >>/dev/null 2>&1 ' 你也可以看這個[視頻](https://www.youtube.com/watch?v=mp-XZm7INl8)的完整示例,註冊運行爲cron作業 –