gpt4 book ai didi

php - Symfony 路由不工作 Apache 返回 404

转载 作者:行者123 更新时间:2023-12-02 02:57:23 25 4
gpt4 key购买 nike

对于所有不是/的请求,我都得到 404 Not Found。在浏览器中点击 smfony.test 可以毫无问题地打开网页。但是 symfony.test 以外的任何东西都会导致 404 Not Found。我查找了几个堆栈溢出解决方案( Issue 1Issue 2 ),但无法找到确切的问题所在。从我的角度来看,这似乎是配置错误,因为当我运行 debug:router 时,我得到了所有 URL:

jernej@blackbook:/var/www/html/symfart$ php bin/console debug:router
------------------- -------- -------- ------ --------------------------
Name Method Scheme Host Path
------------------- -------- -------- ------ --------------------------
_preview_error ANY ANY ANY /_error/{code}.{_format}
app_article_index GET ANY ANY /
app_article_save GET ANY ANY /article/save
app_article_hello GET ANY ANY /hello
------------------- -------- -------- ------ --------------------------

在我的项目中(/dir 路径/var/www/project/public)我还有 .htaccess 文件:

<IfModule mod_rewrite.c>
RewriteEngine On

# Determine the RewriteBase automatically and set it as environment variable.
RewriteCond %{REQUEST_URI}::$1 ^(/.+)/(.*)::\2$
RewriteRule ^(.*) - [E=BASE:%1]

# If the requested filename exists, simply serve it.
# We only want to let Apache serve files and not directories.
RewriteCond %{REQUEST_FILENAME} -f
RewriteRule .? - [L]

# Rewrite all other queries to the front controller.
RewriteRule .? %{ENV:BASE}/index.php [L]
</IfModule>

由于我没有太多使用 Apache 服务器的经验,我宁愿指出我还在 Apache 配置 (/etc/apache2/sites-enabled/000-default.conf) 中做了以下更改以将项目添加到网址:

<VirtualHost *:80>
ServerName symfony.test
DocumentRoot /var/www/html/project/public
</VirtualHost>

我还在 Linux 中添加了 hosts 文件的 URL。这是 Controller 类:

<?php

namespace App\Controller;

use App\Entity\Article;

use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\Routing\Annotation\Route;
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;

class ArticleController extends AbstractController {

/**
* @Route("/", methods={"GET"})
*/
public function index() {
$articles = array();

return $this->render('articles/index.html.twig', array('articles' => $articles));
}

/**
* @Route("/article/save", methods={"GET"})
*/
public function save() {
$entityManager = $this->getDoctrine()->getManager();

$article = new Article();
$article->setTitle('Article One');
$article->setBody('This is the body for article one');

$entityManager->persist($article);
$entityManager->flush();

return new Response('Saved an article with the id of ' + $article->getId());
}

/**
* @Route("/hello", methods={"GET"})
*/
public function hello() {
$articles = array();

return $this->render('articles/index.html.twig', array('articles' => $articles));
}
}

?>

最佳答案

在这种情况下,您需要允许 apache2 从 web 根目录执行 .htaccess 文件...

<VirtualHost *:80>
ServerName symfony.test
DocumentRoot /var/www/html/project/public

<Directory /var/www/html/project/public>
AllowOverride All
Order Allow,Deny
Allow from All
</Directory>

</VirtualHost>

然后,您需要启用 mod_rewrite。你可以在你的 .htaccess 中找到这个 mod,它负责 URL 修改......

sudo a2enmod rewrite
sudo systemctl restart apache2

关于php - Symfony 路由不工作 Apache 返回 404,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60819242/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com