【发布时间】:2020-07-04 06:02:22
【问题描述】:
对于所有不是 / 的请求,我得到 404 Not Found。在浏览器中点击 smfony.test 会毫无问题地打开网页。但是除了 symfony.test 之外的任何东西都会导致 404 Not Found。我查找了几个堆栈溢出解决方案(Issue 1、Issue 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。这是控制器类:
<?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));
}
}
?>
【问题讨论】: