0

我想为我使用 Doctrine2 和 Symfony 2.2 的项目创建文章固定装置。

以下是文章的创建方式:

它们不直接链接到图像,而是包含图像名称。例如,在保存文章之前,我ArticleManager会解析文章文本、查找图像名称、在数据库中搜索这些图像并用真实图像路径替换图像标记部分。

This is article content
typed in form
and it contains an
![Image description](Here is My Awesome Image Name)

那么当表单提交并被ArticleManager->save($article)调用时,文章管理器通过真实的文件WEB路径改变图片标记:

This is article content
typed in form
![Image description](/path/to/my_awesome_image.png)

问题ArticleManager依赖Assetic assets helper服务来构建完整的网络图像路径,并且该服务驻留在request范围内。另一方面,Doctrine fixtures它们是从 CLI 运行的,因此他们无法访问该服务,这使我在加载文章夹具时无法获取图像路径。

任何人都可以建议我解决这个问题的最简单的方法吗

4

1 回答 1

1

好的,感谢@cheesemacfly 的评论,我挖掘了一下,意识到由于我的图像存储在公共文件夹中,我根本不需要使用资产!

我没有使用它来创建图像 URL,而是以这种方式将router服务注入到我的ArticleManager和生成的 URL 中:

$baseRoute = $this->router->getContext()->getBaseUrl();

$appFiles = array('/app.php', '/app_dev.php');
$baseRoute = str_replace($appFiles, '', $baseRoute);

$imageDocumentRoute = $baseRoute . '/' . $imageDocument->getWebPath();

imageDocument->getWebPath()返回附加到保存图像的子文件夹的图像名称。例如subweb/path/imagename.png

于 2013-05-19T11:45:55.653 回答