这是我的第一篇文章,所以我会尽量理解。
我从 symfony 开始,有一个我无法单独解决的问题。
这是我的 Controller ,我正在使用 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 文件丢失或 apache 服务器上未启用 mod_rewrite。
首先你可以运行
composer require symfony/apache-pack
自动添加 .htaccess 并测试,如果路由有效,那么一切都很好。
否则你必须编辑
httpd.conf
或 apache2.conf
文件并启用 mod_rewrite
关于php - Symfony 4 - 路由 : "The requested URL was not found on this server",我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60537798/