Bonjour,
j'ai mis en place l'authentification sur mon site développé en Symfony 4 en suivant le tuto de Lior
mais j'ai un bugg sur une de mes pages localhost:8000/album/{codeAlbum}/{codeMorceau}security.yamlNo route found for "GET /achat//undefined" (from "http://localhost:8000/album/2")
Code yaml : Sélectionner tout - Visualiser dans une fenêtre à part
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25 security: encoders: App\Entity\Abonne: algorithm: bcrypt # https://symfony.com/doc/current/security.html#where-do-users-come-from-user-providers providers: in_memory: { memory: ~ } in_database: entity: class: App\Entity\Abonne property: login firewalls: dev: pattern: ^/(_(profiler|wdt)|css|images|js)/ security: false main: anonymous: true provider: in_database form_login: login_path: security_login check_path: security_login logout: path: security_logout target: home
show.html.twig
Code twig : Sélectionner tout - Visualiser dans une fenêtre à part
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77 {% extends 'base.html.twig' %} {% block title %}Album {% endblock %} {% block body %} <div class="jumbotron text-center"> <h1>{{ album.titreAlbum }}</h1> </div> <div class="container"> <table class="table"> <tbody> <p style='text-align : center'> <img src="{{album.codeAlbum}}/pochette" style='width:300px; height:300px'> </p> <tr> <th>Année</th> <td>{{ album.anneeAlbum }}</td> </tr> <tr> <th>Listes des enregitrements de l'album</th> <td> {% for piste in pistes %} <p style='font-weight: bold' >{{piste.titre}} </p> <figure> <figcaption>Ecoutez l'extrait :</figcaption> <audio controls src="{{piste.codeMorceau}}/audio"> Your browser does not support the <code>audio</code> element. </audio> <!--on créé un identifiant sur chaque lien avec le codeMorceau--> <!--on passe en data le prix du morceau--> <a href="{{ path('achat', {'codeMorceau': piste.codeMorceau, 'prix': piste.prix} ) }}" id=='{{piste.codeMorceau}}' data-prix-produit={{piste.prix}} class='btn btn-success' style='margin-bottom: 20px'>Ajouter au panier</a> </figure> {% endfor %} </td> </tr> </tbody> </table> <p id="demo"></p> <a href="{{ path('album_index') }}">Retour à la liste des albums</a> </div> {% endblock %} {% block javascripts %} <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.4.0/jquery.min.js"></script> <script src="https://unpkg.com/axios/dist/axios.min.js"></script> <script> $(document).ready(function(){ $('a').click(function(event){ /*récupération de l'id du lien clické*/ var id = this.id; //alert(id); /*passage de données via data*/ var prix = this.dataset.prixProduit; //alert('Prix du produit : '+produit); event.preventDefault();//on bloque la rédirection = comportement par défaut d'un lien //const url = this.href;//on récupère l'url du lien clické var url = '/achat/'+id+'/'+prix; axios.get(url.toString()); /*axios.get(url.toString()).then(function(response){//appel de la fonction achat du homeController console.log(response); })*/ }); }) </script> {% endblock %}
HomeController
AlbumController
Code : Sélectionner tout - Visualiser dans une fenêtre à part
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67 <?php namespace App\Controller; use Twig\Environment; use App\Entity\Abonne; use Symfony\Component\HttpFoundation\Response; use Symfony\Component\Routing\Annotation\Route; use Symfony\Component\HttpFoundation\Session\Session; use Symfony\Bundle\FrameworkBundle\Controller\AbstractController; class HomeController extends AbstractController { /** * @Route("/",name="home") * @return Response */ public function index(): Response { $session = new Session(); //réinitiale la session $session->migrate(); //démarre la session $session->start(); //on récupère les données utilisateurs connecté $user = $this->getUser(); $achats = array(); //si un utilisateur est connecté on enregistre les données en variables de session if($user!=null){ $session->set('idUser', $user->getCodeAbonne()); $session->set('achats', $achats); } return $this->render('pages/home.html.twig'); } /** * @Route("/achat/{codeMorceau}/{prix}", name="achat", methods="GET") */ public function achats($codeMorceau, $prix): Response { $session = new session(); //réinitiale la session $session->migrate(); //démarre la session $session->start(); $achats = $session->get('achats') ; /*on ajoute l'enregistrement*/ //array_push($achats, array($codeMorceau,1)); array_push($achats, array($codeMorceau,$prix)); /*$codeMorceau passé en paramètre correspond à l'id du morceau à ajouter à la variable de session */ /*$session->set('maVariable', $codeMorceau); $var = $session->get('maVariable');*/ $session->set('achats', $achats); $var = $session->get('achats'); /*affichage pour le controle*/ //return new Response('<body><p>'.$var.'</p></body>'); return new Response('<body><p>'.dd($var).'</p></body>'); } }
Code : Sélectionner tout - Visualiser dans une fenêtre à part
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96 <?php namespace App\Controller; use App\Entity\Album; use App\Entity\Disque; use App\Repository\AlbumRepository; use Knp\Component\Pager\PaginatorInterface; use Symfony\Component\HttpFoundation\Request; use Symfony\Component\HttpFoundation\Response; use Symfony\Component\Routing\Annotation\Route; use Symfony\Bundle\FrameworkBundle\Controller\AbstractController; use App\Entity\CompositionDisque; use App\Entity\Enregistrement; use Symfony\Component\HttpFoundation\Session\Session; /** * @Route("/album") */ class AlbumController extends AbstractController { /** * @Route("/", name="album_index", methods={"GET"}) */ public function index(PaginatorInterface $paginator, AlbumRepository $repository, Request $request) { $albums = $paginator->paginate( $repository->findAll(), $request->query->getInt('page', 1), 5 ); // $albums = $this->getDoctrine() // ->getRepository(Album::class) // ->findAll(); return $this->render('album/index.html.twig', [ 'albums' => $albums, ]); } /** * @Route("/{codeAlbum}", name="album_show", methods={"GET"}) */ public function show(Album $album): Response { //On récupère un objet disque correspondant au code album. $disques = $this->getDoctrine()->getRepository(Disque::class)->findBy(['codeAlbum' => $album->getCodeAlbum()], []); $disques = $disques[0]; //dump($disques); $compoDisque = $this->getDoctrine()->getRepository(CompositionDisque::class)->findBy(['codeDisque' => $disques->getCodeDisque()], []); $pistes = array(); foreach ($compoDisque as $compo) { $enregistrement = $this->getDoctrine()->getRepository(Enregistrement::class)->findBy(['codeMorceau' => $compo->getCodeMorceau()->getCodeMorceau()], []); array_push($pistes, $enregistrement[0]); } dump($pistes); return $this->render('album/show.html.twig', [ 'album' => $album, 'pistes' => $pistes ]); } /** * @Route("/{codeAlbum}/pochette", name="pochetteAlbum", methods="GET") */ public function pochette(Album $album): Response { return new Response( stream_get_contents($album->getPochette()), $album->getPochette() ? Response::HTTP_OK : Response::HTTP_NOT_FOUND, [ 'Content-type' => 'image/jpeg', ] ); } /** * @Route("/{codeMorceau}/audio", name="extraitMusique", methods="GET") */ public function Extrait(Enregistrement $enregistrement): Response { return new Response( stream_get_contents($enregistrement->getExtrait()), $enregistrement->getExtrait() ? Response::HTTP_OK : Response::HTTP_NOT_FOUND, [ 'Content-type' => 'audio/mp3' ] ); } }
Partager