我想在symfony2中创建一个区域,其中包含用户可以下载的文件列表。
我希望能够跟踪每个文件被下载的次数。
有谁能解释一下我是如何做到这一点的?
发布于 2012-01-18 23:49:03
以简单的方式在你的文件列表中,你必须设置你的控制器的URL,它将计算下载的次数,然后打印直接链接到文件。
{# list of files in template %}
{% for file in files %}
<a href="{% path('counting', {'id': file.id}) %}">{{ file.name }}</a>
{% endfor %}
// Controller
/**
* Counting the number of file downloads
*
* @Route("/counting/{id}", name="counting", requirements={"id" = "\d+"})
* @Template()
*/
public function countingAction($id)
{
$em = $this->getDoctrine()->getEntityManager();
$file = $em->getRepository('MyBundle:File')->find($id);
$count = $file->getCount();
$file->setCount($count + 1);
$em->persist($entity);
$em->flush();
return array('file' => $file);
}
{# final file page in template #}
<a href="{{ file.url }}">{{ file.name }}</a>发布于 2012-01-18 17:05:53
使用Doctrine实体-本文"How to handle file upload“中提供了一些信息
https://stackoverflow.com/questions/8906895
复制相似问题