Symfony Route 不工作 Apache 返回 404

Jer*_*j K 3 php apache symfony

对于所有非 / 的请求,我收到 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                    
 ------------------- -------- -------- ------ -------------------------- 
Run Code Online (Sandbox Code Playgroud)

在我的项目(/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>
Run Code Online (Sandbox Code Playgroud)

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

<VirtualHost *:80>
    ServerName symfony.test
    DocumentRoot /var/www/html/project/public
</VirtualHost>
Run Code Online (Sandbox Code Playgroud)

我还将 URL 添加到 Linux 中的主机文件中。这是控制器类:

<?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));
        }
    }

?>
Run Code Online (Sandbox Code Playgroud)

xay*_*yer 7

在这种情况下,您需要允许 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>
Run Code Online (Sandbox Code Playgroud)

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

sudo a2enmod rewrite
sudo systemctl restart apache2
Run Code Online (Sandbox Code Playgroud)