2016-12-01 101 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作业 –