Symfony 4 - 路线:"The requested URL was not found on this server"

Symfony 4 - route : "The requested URL was not found on this server"

这是我的第一个 post 所以我会尽量让大家理解。

我从 symfony 开始,有一个我无法单独解决的问题。

这是我的控制器,我正在使用 WAMP。 当我的 Url 是“mysite.local”时,它正常工作,并告诉我它应该做什么(感谢 home() 函数)。但是当我的 Url 是“mysite.local/hello”时,我有以下错误。

Not Found

The requested URL was not found on this server.

Apache/2.4.41 (Win64) PHP/7.4.0 Server at mysite.local Port 80

我想这是正常的,因为我没有创建任何名为“hello”的文件,但它在我正在关注的编队中工作。

你能帮帮我吗?谢谢

<?php


namespace App\Controller;

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

use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;

class HomeController extends AbstractController
{
/**
 * @Route("/hello", name="hello")
 */

public function hello()
{
    return new Response("Bonjour ...");
}
/**
 *@Route("/", name="homepage");
 */

public function home(){
    $prenoms = ["Lior" => 17 , "Joseph" => 12, "Anne" => 55];
    return $this->render("home.html.twig",
        [
            'title' => "Bonjour a tous :)",
            'age' => "31 ",
            'tableau' => $prenoms,
        ]); 
}

}

?>

请尝试

mysite.local/index.php/hello

如果可行,则意味着 .htaccess 文件丢失或 mod_rewrite 未在 apache 服务器上启用。

首先你可以运行

composer require symfony/apache-pack

自动添加 .htaccess 并测试,如果路由有效则一切正常。

否则您必须编辑 httpd.confapache2.conf 文件并启用 mod_rewrite

您的服务器上缺少 apache,无法添加 .htaccess 文件

composer require symfony/apache-pack