Laravel 5.1 - 命令未按预期运行

时间:2016-11-01 10:28:29

标签: php laravel

我有一些使用调度程序运行的命令,其中一些命令没有按预期工作,如果放在外部文件中,代码可以正常工作。但是当作为调度程序的命令运行它没有做任何事情时,没有文件被删除&该文件没有输出。

命令:

<?php

namespace App\Console\Commands;

use Illuminate\Console\Command;
use DirectoryIterator;

class CleanExports extends Command {

    protected $name = 'clean:exports';

    public function fire() {
$folderName = 'exports';

if (file_exists($folderName)) {
    $i = 0;
    foreach (new DirectoryIterator($folderName) as $fileInfo) {
        if ($fileInfo->isDot()) {
        continue;
        }
        if (time() - $fileInfo->getCTime() >= 1*24*60*60) {
            unlink($fileInfo->getRealPath());
        }
        $i++;
    }

    echo $i.' files cleaned';
}        
}
}

Kernel.php

<?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::class,
        '\App\Console\Commands\CleanExports',
    ];

    /**
     * Define the application's command schedule.
     *
     * @param  \Illuminate\Console\Scheduling\Schedule  $schedule
     * @return void
     */
    protected function schedule(Schedule $schedule) {
        $schedule->command('inspire')
                ->hourly();    
             $schedule->command('clean:exports')->everyMinute()->sendOutputTo('temp/errors.txt');
}

1 个答案:

答案 0 :(得分:0)

你不会从中得到任何输出的原因是因为它file_exists('exports')将返回false。

尝试添加绝对路径:

$folderName = public_path('exports');

另外,只是为了更好的衡量并检查这是正确的,如果目录不存在,请添加else输出内容。

希望这有帮助!