简体   繁体   English

Symfony 5 带有参数的自定义路由导致 404 错误

[英]Symfony 5 custom route with param result in 404 error

I'm new to Symfony 5 (I started learning it today) and I'm following a course on symfonycasts.com about it, I've done everything as the teacher showed but I still got a 404 error on my controller redirection, here's the code:我是Symfony 5(我今天开始学习),并且我在Symfonycasts.z4d236d236d236d236d236d1d2c5fe6ad1c501c501c50da4bec50z上都遵循了一门404.04的信息。编码:

<?php

namespace App\Controller;

use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\Routing\Annotation\Route;

class QuestionController
{
    /**
     * @Route("/")
     */
    public function homepage()
    {
        return new Response('Homepage');
    }

    /**
     * @return Response
     * @Route("/q/{$param}")
     */
    public function showQuestions($param)
    {
        return new Response(sprintf("Hello %s!", $param));
    }
}

As you see it's a real simple code, but when I go to http://localhost I have the 'Homepage', if I change the如您所见,这是一个真正简单的代码,但是当我从 go 到http://localhost我有“主页”,如果我更改

    /**
     * @return Response
     * @Route("/q/{$param}")
     */
    public function showQuestions($param)
    {
        return new Response(sprintf("Hello %s!", $param));
    }

to

    /**
     * @return Response
     * @Route("/q")
     */
    public function showQuestions()
    {
        return new Response("Hello World");
    }

I have "Hello World" on http://localhost/q But when I add the /{param} to my annotation like http://localhost/q/Jean I have the Error page telling me that http://localhost/q/Jean does not exist.我在http://localhost/q上有“Hello World”但是当我将 /{param} 添加到我的注释中时,例如http://localhost/q/Jean我有错误页面告诉我Z80791B3AE7002CBFA88C246876D q/Jean不存在。 I have a .htaccess file in my public folder generated by我的公共文件夹中有一个 .htaccess 文件,由

composer req symfony:apache-pack

and I also tried to clean the php cache and change the development mod with我还尝试清理 php 缓存并更改开发模式

php bin/console cache:clear --env=prod

but I still have the error.但我仍然有错误。 I'm running my symfony app on a XAMPP server Version: XAMPP for Linux 7.4.1-1 On ubuntu 18.04 and Apache 2.4.29 I'm running my symfony app on a XAMPP server Version: XAMPP for Linux 7.4.1-1 On ubuntu 18.04 and Apache 2.4.29

I believe the problem is that you are using {$param} instead of {param} at your @Route.我相信问题在于您在 @Route 使用 {$param} 而不是 {param}。 See this .看到 这个

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM