Martelo
Martelo

Reputation: 1

Symfony paths issues

I'm currently working on a basic Symfony project to discover this Framework, my website identifies some French rap albums and gives infos about it, then I created 2 Controllers : "DefaultController" and "AlbumsController". In the fist one, I implements some functions to displays some music lyrics and I use path names for the links and it's well work but with the second Controller I do the exact same things and it's not working. (Sorry for bad English). Attached -> The problematic code DefaultController :

    <?php

namespace App\Controller;

use App\Entity\Musique;
use App\Form\AlbumsType;
use Doctrine\Common\Persistence\ObjectManager;
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
use App\Controller\AlbumsController;
use App\Repository\AlbumsRepository;
use App\Repository\MusiqueRepository;
use Symfony\Component\Routing\Annotation\Route;
use Symfony\Component\HttpFoundation\Request;
use App\Entity\Albums;


class DefaultController extends AbstractController
{
    /**
     * @Route("/", name="index")
     */
    public function index()
    {
        return $this->render('index.html.twig', [
            'title' => 'Accueil',
        ]);
    }

    /**
     * @Route("/albums", name="albums")
     */
    public function albums()
    {
        $repo = $this->getDoctrine()->getRepository(Albums::class);

        $albums = $repo->findAll();

        return $this->render('albums/index.html.twig', [
            'albums' => $albums,
        ]);
    }

    /**
     * @Route("/musiques", name="musiques")
     */
    public function musiques()
    {
        $repo = $this->getDoctrine()->getRepository(Musique::class);

        $musiques = $repo->findAll();

        return $this->render('musiques.html.twig', [
            'title' => 'Liste des Musiques',
            '$musiques' => $musiques,
        ]);
    }


    /**
     * @Route("/musiques/{id}", requirements={"id": "[1-9]\d*"}, name="randMusique")
     * @throws \Exception
     */
    public function randomMusique()
    {
        $random = random_int(1, 100);
        $repo = $this->getDoctrine()->getRepository(Musique::class);

        $musique = $repo->find($random);

        return $this->render('randomMusique.html.twig', [
            'musique' => $musique,
        ]);
    }

}

AlbumsController :

<?php

namespace App\Controller;

use App\Entity\Albums;
use App\Form\AlbumsType;
use App\Repository\AlbumsRepository;
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\Routing\Annotation\Route;

/**
 * @Route("/albums")
 */
class AlbumsController extends AbstractController
{
    /**
     * @Route("/", name="albums_index", methods={"GET"})
     */
    public function index(AlbumsRepository $albumsRepository): Response
    {
        return $this->render('albums/index.html.twig', [
            'albums' => $albumsRepository->findAll(),
        ]);
    }

    /**
     * @Route("/new", name="album_new", methods={"GET","POST"})
     */
    public function new(Request $request): Response
    {
        $album = new Albums();
        $form = $this->createForm(AlbumsType::class, $album);
        $form->handleRequest($request);

        if ($form->isSubmitted() && $form->isValid())
        {
            $entityManager = $this->getDoctrine()->getManager();
            $entityManager->persist($album);
            $entityManager->flush();

            return $this->redirectToRoute('albums_index');
        }

        return $this->render('albums/new.html.twig', [
            'album' => $album,
            'formAlbum' => $form->createView(),
        ]);
    }

    /**
     * @Route("/{id}", name="albums_show", methods={"GET"})
     */
    public function show(Albums $album): Response
    {
        return $this->render('albums/show.html.twig', [
            'album' => $album,
        ]);
    }

    /**
     * @Route("/{id}/edit", name="albums_edit", methods={"GET","POST"})
     */
    public function edit(Request $request, Albums $album): Response
    {
        $form = $this->createForm(AlbumsType::class, $album);
        $form->handleRequest($request);

        if ($form->isSubmitted() && $form->isValid()) {
            $this->getDoctrine()->getManager()->flush();

            return $this->redirectToRoute('albums_index');
        }

        return $this->render('albums/edit.html.twig', [
            'album' => $album,
            'form' => $form->createView(),
        ]);
    }

    /**
     * @Route("/{id}", name="albums_delete", methods={"DELETE"})
     */
    public function delete(Request $request, Albums $album): Response
    {
        if ($this->isCsrfTokenValid('delete'.$album->getId(), $request->request->get('_token'))) {
            $entityManager = $this->getDoctrine()->getManager();
            $entityManager->remove($album);
            $entityManager->flush();
        }

        return $this->redirectToRoute('albums_index');
    }
}

base.html.twig :

<!DOCTYPE html>
<html>
<head>
    <meta charset="UTF-8">
    <title>{% block title %}Projet &mdash; PHP{% endblock %}</title>
    <link rel="stylesheet" href="https://bootswatch.com/4/darkly/bootstrap.min.css">
    <link rel="stylesheet" type="text/css" href="{{ asset('css/style.css')}}">
    {% block stylesheets %}{% endblock %}
</head>
<body>
<nav class="navbar navbar-expand-lg navbar-light">
    <a class="navbar-brand" href="/">France-Rap</a>
    <button class="navbar-toggler" type="button" data-toggle="collapse" data-target="#navbarColor03"
            aria-controls="navbarColor03" aria-expanded="false" aria-label="Toggle navigation">
        <span class="navbar-toggler-icon"></span>
    </button>

    <div class="collapse navbar-collapse" id="navbarColor03">
        <ul class="navbar-nav mr-auto">
            <li class="nav-item active">
                <a class="nav-link" href="{{ path('index') }}">Accueil</a>
            </li>
            <li class="nav-item">
                <a class="nav-link" href="{{ path('albums') }}">Liste des Albums</a>
            </li>
            {#
            <li class="nav-item">
                <a class="nav-link" href="{{ path('musiques') }}">Liste des Musiques</a> // The path isn't working
            </li>
            <li class="nav-item">
                <a class="nav-link" href="{{ path('randMusique') }}">Musique Aléatoire</a>
            </li>
            <li class="nav-item">
                <a class="nav-link" href="{{ path('album_new') }}">Créer un Album</a>
            </li>
            #}
            <li class="nav-item">
                <a class="nav-link" href="#">Contact</a>
            </li>
        </ul>
        {% block search %}{% endblock %}
    </div>
</nav>
    {% block body %}{% endblock %}
{% block javascripts %}{% endblock %}
</body>
</html>

This is the error : Render error Resultat of the command : php bin/console debug:router musiques

Upvotes: 0

Views: 141

Answers (1)

Qasim Nadeem
Qasim Nadeem

Reputation: 607

In the secreen shot of the error i can see that your view is picked up from cache try runing follwing comand:

php bin/console cache:clear

Upvotes: 1

Related Questions