I have to create a site for downloading packages. There is an account system (local or CAS authentication).
I use VichUploaderBundle to distribute my files. And I distribute them from a controller so that I can modify my database accordingly.
I have a user entity and a package entity. The user contains an ArrayCollection () of packages (manytomany).
If it downloads a package, then the downloaded package is added to its arrayCollection ().
The problem is this, when my user clicks on a package to download, the database is modified correctly, but the download does not start, and it is redirected to a weird page:
I don't understand what is it ?
This is my code :
/**
* Lorsque l'on clique sur un package à télécharger ou sa notice : Téléchargement
*
* @Route("/{id}/{type}/file", name="user_paquet_fileDDL")
*/
public function paquetFileAction(Paquet $paquet, $type)
{
// $em = $this->getDoctrine()->getManager(); //Récupération du manager
//$this->getDoctrine()->getManager()->getRepository('PagesBundle:User')->setDDL("loughin.bastien"); //On décrémente le nombre de DDL pour l'utilisateur en question
// $user = $this->getUser();
$downloadHandler = $this->get('vich_uploader.download_handler'); // On prépare le téléchargement
if($type == "package") //Si l'utilisateur clique sur le lien du package, on lui donne le fichier package
{
$token = $this->get('security.token_storage')->getToken();
$user = $token->getAttribute('user');
// $user = $this->get('serializer')->deserialize($data, 'Site\PagesBundle\Entity\UserCas', 'json');
$this->packageDDL($paquet,$user);
return $downloadHandler->downloadObject($paquet, $fileField = 'paquetFile', Paquet::class, null);
}
else //Sinon c'est qu'il a cliqué sur le lien de la notice, alors on lui donne la notice associée au package
{
return $downloadHandler->downloadObject($paquet, $fileField = 'noticeFile', Paquet::class, null);
}
}
And when an User download the file, the called "packageDDL()" function :
public function packageDDL($paquet,$user)
{
$token = $this->get('security.token_storage')->getToken();
$typeAuth = $token->getAttribute('typeAuth');
dump($user);
$em = $this->getDoctrine()->getManager(); //Récupération du manager
$user->addPackage($paquet);
$em->persist($user);
$em->flush();
if($typeAuth == 'cas')
{
$data = $this->get('serializer')->serialize($user, 'json');
$token->setAttribute('user',$data) ;
}
}
Could anyone help me please ?