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

标签 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                    
 ------------------- -------- -------- ------ -------------------------- 

在我的项目中(/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/

相关文章:

php - MySQL 和 PHP : Display matrix table dynamically

php - 如何在laravel中为每个用户生成唯一的自动增量值并将其添加到数据库

java - Ant 类互相寻找

java - JSOUP 和 Android 模拟器 - 无法解析主机

forms - 在 Symfony2 表单中插入 Google Recaptcha

php - 过滤掉不需要的元素后将一维数组转换为二维

php - '1' 数字出现在 wordpress 网站的侧边栏下

wordpress - 如何配置 apache 为自定义域提供目录服务

php - 有没有标准的方法来缓存学说对象?

html - 如何缩小 Symfony/Twig 生成的 HTML 代码以满足 Google PageSpeed Insight?