url 有效但未加载视图

url valid but view not loading

我一直在为一个学校项目开发一个在线学习网站。单击课程中属于某个部分的课程时,我无法通过变量。这个想法是在课程页面 (formation.html.twig) 上有课程摘要,您可以单击侧边栏菜单中的课程。然后它应该显示一个具有相同菜单的页面,并显示课程内容代替课程摘要 (lesson.html.twig)。

我已经更新了控制器,因此 url 变为: formations/consulter-formationid-sectionid-lessonid

在视图中,我写了{{ path('app_formations_lesson', {'formation':formation.id, 'section': section.id, 'id':lesson.id}) }}

它在 url 上工作正常,当我单击时显示正确的值,但页面刷新并且不会加载正确的树枝视图 (lesson.html.twig),它加载相同的视图我目前在 (formation.html.twig).

编队控制器:

#[Route('/formations/consulter-{id}', name: 'app_formations_see')]
    public function see($id): Response
    {
        $formation = $this->doctrine->getRepository(Formation::class)->findOneById($id);
        $section  = $this->doctrine->getRepository(Section::class)->findAll();
        $lesson = $this->doctrine->getRepository(Lesson::class)->findAll();
        return $this->render('formations/formation.html.twig', [
            'formation' => $formation,
            'sections' => $section,
            'lessons' => $lesson
        ]);
    }

    #[Route('/formations/consulter-{formation}-{section}-{id}', name: 'app_formations_lesson')]
    public function seeLesson($id): Response
    {
        $lesson = $this->doctrine->getRepository(Lesson::class)->findOneById($id);
        return $this->render('formations/lesson.html.twig', [
            'lesson' => $lesson
        ]);
    }

formation.html.twig

{% extends 'base.html.twig' %}
{% block title %}{{ formation.title }}{% endblock %}
{% block content %}
    <div class="formationcontainer text-center">
    {% block sidebar %}
        {% include "./formations/sidebar.html.twig" %}
    {% endblock %}
        <h1>{{ formation.title }} par {{ formation.user.firstname }} {{ formation.user.lastname }}</h1>
        {{ formation.description }}
        <hr>
        <h2>Sommaire</h2>
        <div class="tableau">
            <table class="table">
                {% for section in formation.sections %}
                    <thead class="table-success">
                    <tr>
                        <th scope="col">{{ section.name }}</th>
                    </tr>
                    </thead>
                    <tbody>
                    {% for lesson in section.lessons %}
                        <tr>
                            <td>{{ lesson.title }}</td>
                        </tr>
                    {% endfor %}
                    </tbody>
                {% endfor %}
            </table>
        </div>
    </div>
{% endblock %}

lesson.html.twig

{% extends 'base.html.twig' %}
{% block title %}titre de la leçon{% endblock %}
{% block content %}
    <div class="formationcontainer text-center">
        {% block sidebar %}
            {% include "./formations/sidebar.html.twig" %}
        {% endblock %}

        <h1>Nom de la leçon</h1>
        <hr>
        <h2>Vidéo</h2>
        <h2>Contenu</h2>
    </div>
{% endblock %}

侧边栏块

<!-- Sidear for lesson pages -->

<nav class="flex-shrink-0flex-shrink-0 p-3 bg-white sidenav">
    <button class="btn btn-success" id="sidenav-btn" type="button" data-bs-toggle="collapse" data-bs-target="#sidebarCollapse" aria-expanded="false" aria-controls="collapseOne">
        Sommaire
    </button>
    <div class="list-unstyled ps-0 ul-custom navbar-collapse collapse show" id="sidebarCollapse" aria-expanded="true">
        <li class="mb-1">
            {% for section in formation.sections %}
                <ul class="list-unstyled align-items-center rounded fw-normal">
                    <li>{{ section.name }}</li>
                </ul>
                <div>
                    {% for lesson in lessons %}
                        <ul class="list-unstyled fw-normal pb-1 small">
                            <li><a href="{{ path('app_formations_lesson',  {'formation':formation.id, 'section': section.id, 'id':lesson.id}) }}" class="link-dark rounded">{{ lesson.title }}</a></li>
                        </ul>
                    {% endfor %}
                </div>
            {% endfor %}
        </li>
        <li class="border-top my-3"></li>
        <li class="mb-1">
            <ul class="list-unstyled fw-normal pb-1 small">
                <li><a href="{{path('app_formations')}}" class="link-dark rounded">retour à la liste des formations</a></li>
            </ul>
        </li>
    </div>
</nav>

这个问题从何而来?如何显示正确的视图? 非常感谢!

编辑:我交换了控制器中的代码以首先匹配 seeLesson 函数。

#[Route('/formations/consulter-{formation}-{section}-{id}', name: 'app_formations_lesson')]
    public function seeLesson($id): Response
    {
        $formation = $this->doctrine->getRepository(Formation::class)->findOneById($id);
        $section  = $this->doctrine->getRepository(Section::class)->findAll();
        $lesson = $this->doctrine->getRepository(Lesson::class)->findOneById($id);
        return $this->render('formations/lesson.html.twig', [
            'lesson' => $lesson,
            'formation' => $formation,
            'sections' => $section
        ]);
    }

    #[Route('/formations/consulter-{id}', name: 'app_formations_see')]
    public function see($id): Response
    {
        $formation = $this->doctrine->getRepository(Formation::class)->findOneById($id);
        $section  = $this->doctrine->getRepository(Section::class)->findAll();
        $lesson = $this->doctrine->getRepository(Lesson::class)->findAll();
        return $this->render('formations/formation.html.twig', [
            'formation' => $formation,
            'sections' => $section,
            'lessons' => $lesson
        ]);
    }

它呈现错误的页面,因为您的 URL 太相似了。

当试图确定要执行的方法时,Symfony 将请求的 URL 与控制器方法上方的路由定义从上到下进行匹配。

例如,如果您请求 /formations/consulter-1-2-3, Symfony 首先尝试将其与 /formations/consulter-{id} 进行匹配。 如果您将 1-2-3 替换为 {id},这将匹配,因此它会执行 see 方法。

要修复你有两个选择:

切换控制器中 2 个方法的顺序,使方法 seeLesson 定义在 see 方法之上。然后 Symfony 将首先尝试匹配 seeLesson 方法的路由。

或者保持方法顺序不变,但是对see方法的路由中的{id}参数添加约束,指定匹配的{id}只能由位数:

#[Route('/formations/consulter-{id}', name: 'app_formations_see', requirements: ['id' => '\d+'])]