我想为我的项目创建使用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运行,因此无法访问该服务,使我无法在加载文章夹具时获取图像路径。
有人能建议我解决这个问题的方法最少 hackish 吗?
答案 0 :(得分:1)
好的,感谢@ cheesemacfly的评论,我挖了一点,意识到由于我的图像存储在公共文件夹中,我根本不需要使用资产!
我没有使用它来创建图片网址,而是将router
服务注入我的ArticleManager
并以这种方式生成了网址:
$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
。