Symfony数据导出文件位置的最佳实践

时间:2014-04-18 15:18:02

标签: symfony

我正在编写一个控制台命令,用于生成供外部服务使用的数据文件(例如,Google Feed,广告资源Feed等)。生成的数据文件的位置是否应该在Symfony应用程序中?我知道他们实际上可以在任何地方,我只是想知道是否有标准的方法来做。

1 个答案:

答案 0 :(得分:2)

取决于您,但最好在参数中包含此路径。例如,您可以拥有与命令相关的参数组。这允许您根据当前环境具有不同的配置:

parameters:
    # /app/config.yml 
    # @see MyExportCommand.php
    my_export_command:
        base_path:       '/data/ftp/export'
        other_command_related_param: true

在您的命令中,获取并将这些参数存储在initialize函数中:

// MyExportCommand.php
protected function initialize(InputInterface $input, OutputInterface $output)
{
    $this->parameters = $this->getContainer()->getParameter('my_export_command');
}

最后在执行函数中,您可以使用以下内容:($this->fsSymfony2 Filesystem component的实例)

// execute()
// Write the file
$filePath = $this->parameters['base_path']. '/'. $this->fileName;
$this->fs->dumpFile($filePath, $myContent);