我想为我的项目创建Article fixture,该项目使用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
服务来构建完整的web映像路径,而该服务位于request
范围内。另一方面,Doctrine fixtures
是从CLI运行的,因此它们无法访问该服务,这使得我在加载文章fixture时无法获得图像路径。
有人能给我提一个解决这个问题的方法吗?
好吧,多亏@cheesemacfly的评论,我挖掘了一下,意识到由于我的图像存储在公共文件夹中,我根本不必使用assetic!
我没有使用它来创建图像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
。