【发布时间】:2016-04-10 16:54:56
【问题描述】:
在我的自定义 Artisan 命令中,我从我的数据库中获取项目列表,我想遍历它们并通过我的 DownloaderController 中的方法运行它们。我怎样才能做到这一点?最好的做法是什么?
下载命令.php
public function handle()
{
$files = File::all();
foreach($files as $file)
{
// downloadFile method belongs to DownloadController
downloadFile($file);
}
}
下载控制器.php
public function downloadFile($file)
{
// some example logic to download file
if(wget($file))
{
$file->status = 'Downloaded';
}
else
{
$file->status = 'Failed';
}
$file->save();
}
【问题讨论】:
-
正确的方法是将该逻辑移动到另一个类,以便它能够从 Controller 和 Comand 调用它(控制器和命令显示与应用程序交互的不同方式,但在同一级别,从另一个调用一个是错误的)
-
如果 downloadFile 也更新了
$file记录怎么办? (检查更新的帖子) -
然后将该 $file 作为参数传递给类方法。像 Filesaver::saveFile($file).
-
但是数据库更改应该存储在该类还是控制器中?
标签: php laravel laravel-artisan