问题描述
这是我的第一篇文章,所以我会尽量理解。
this is my first post so i'll try to be understandable.
我从symfony开始,但有一个我无法单独解决的问题。
I am starting with symfony, and there is a problem i can't resolve alone.
这是我的控制器,我正在使用WAMP。
当我的Url是 mysite.local时,它可以正常工作,并且向我显示了它应该做什么(由于home()函数)。但是当我的网址为 mysite.local / hello时,出现以下错误。
This is my controller, and I am working with WAMP.When my Url is "mysite.local", it work normally, and it show me what it should (thanks to the home() function). But when my Url is "mysite.local/hello", i have the following error.
在此服务器上找不到请求的URL。
The requested URL was not found on this server.
Apache / 2.4.41(Win64)PHP / 7.4.0服务器,位于mysite。本地端口80
Apache/2.4.41 (Win64) PHP/7.4.0 Server at mysite.local Port 80
我想这很正常,因为我没有创建任何名为 hello的文件,但它在编队中正常工作我正在关注。
I guess this is normal as i didn't created any file named "hello", but it's working in the formation i am following.
能帮我吗?谢谢
<?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
If that works, that means either .htaccess file is missing or mod_rewrite is not enabled on apache server.
首先您可以运行
composer require symfony/apache-pack
自动添加.htaccess并进行测试,如果路由有效,则一切正常。
to add .htaccess automatically and test, if route works then everything is fine.
否则,您必须编辑 httpd.conf
或 apache2.conf
文件并启用 mod_rewrite
otherwise you've to edit httpd.conf
or apache2.conf
file and enable mod_rewrite
这篇关于Symfony 4-路由:“在此服务器上找不到所请求的URL”的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!