【问题标题】:Symfony Route not working Apache returning 404Symfony Route 不工作 Apache 返回 404
【发布时间】:2020-07-04 06:02:22
【问题描述】:

对于所有不是 / 的请求,我得到 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。这是控制器类:

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

?>

【问题讨论】:

    标签: php apache symfony


    【解决方案1】:

    您的主页有效吗?如果它没有尝试从您的服务器更改您网站的默认托管路径目录。

    【讨论】:

    • 它适用于根页面 simfony.test,我得到了网页。但任何其他 URL 都会返回 404。
    • 尝试 index.php/hello 如果可行,请尝试安装 symfony apache 包
    • 如果我这样做,那么它会起作用。我能够看到你好消息。知道如何解决这个问题吗?
    • 我已经尝试安装 apache 包,如果您查看解决方案 2 链接。运行 composer require symfony/apache-pack 不会解决问题。
    【解决方案2】:

    这种情况下,需要允许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
    

    【讨论】:

      猜你喜欢
      • 2015-12-16
      • 2020-02-03
      • 1970-01-01
      • 1970-01-01
      • 2021-01-23
      • 2018-09-14
      • 2023-03-05
      • 1970-01-01
      • 2019-07-07
      相关资源
      最近更新 更多