我目前正在做一个基本的Symfony项目来发现这个框架,我的网站识别了一些法国说唱专辑并提供了有关它的信息,然后我创建了2个控制器:"DefaultController"和"AlbumsController"。在第一个中,我实现了一些功能来显示一些音乐歌词,并且我使用路径名作为链接,效果很好,但是使用第二个控制器,我做了完全相同的事情,但它不起作用。(对不起,英语不好(。 附加 -> 有问题的代码 默认控制器 :
<?php
namespace AppController;
use AppEntityMusique;
use AppFormAlbumsType;
use DoctrineCommonPersistenceObjectManager;
use SymfonyBundleFrameworkBundleControllerAbstractController;
use AppControllerAlbumsController;
use AppRepositoryAlbumsRepository;
use AppRepositoryMusiqueRepository;
use SymfonyComponentRoutingAnnotationRoute;
use SymfonyComponentHttpFoundationRequest;
use AppEntityAlbums;
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,
]);
}
}
专辑控制器 :
<?php
namespace AppController;
use AppEntityAlbums;
use AppFormAlbumsType;
use AppRepositoryAlbumsRepository;
use SymfonyBundleFrameworkBundleControllerAbstractController;
use SymfonyComponentHttpFoundationRequest;
use SymfonyComponentHttpFoundationResponse;
use SymfonyComponentRoutingAnnotationRoute;
/**
* @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');
}
}
基.html.树枝 :
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>{% block title %}Projet — 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>
这是错误: 渲染错误 命令的结果: PHP bin/console 调试:路由器音乐
在错误的秒镜头中,我可以看到您的视图是从缓存中选取的,请尝试运行以下链接:
PHP bin/console cache:clear