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

Ste*_*che 5 symfony

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

COi*_*Oil 3

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

parameters:
    # /app/config.yml 
    # @see MyExportCommand.php
    my_export_command:
        base_path:       '/data/ftp/export'
        other_command_related_param: true
Run Code Online (Sandbox Code Playgroud)

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

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

最后在你的执行函数中,你可以使用这样的东西:(是Symfony2 文件系统组件$this->fs的一个实例)

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