Ken*_*lar 5 php routes http-status-code-404 symfony symfony4
这是我的第一篇文章,所以我会尽量理解。
我从 symfony 开始,有一个我无法单独解决的问题。
这是我的控制器,我正在使用 WAMP。当我的 Url 是“mysite.local”时,它可以正常工作,并向我展示它应该做什么(感谢 home() 函数)。但是当我的 Url 是“mysite.local/hello”时,我有以下错误。
未找到
在此服务器上找不到请求的 URL。
Apache/2.4.41 (Win64) PHP/7.4.0 服务器在 mysite.local 端口 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,
]);
}
Run Code Online (Sandbox Code Playgroud)
}
?>
Fai*_*zan 12
请尝试
mysite.local/index.php/hello
如果可行,则意味着 .htaccess 文件丢失或 apache 服务器上未启用 mod_rewrite。
首先你可以运行
composer require symfony/apache-pack
Run Code Online (Sandbox Code Playgroud)
自动添加 .htaccess 并测试,如果路由有效,那么一切都很好。
否则你必须编辑httpd.conf或apache2.conf归档并启用mod_rewrite