【问题标题】:Multiple routes in Symfony4 result in 404 errorSymfony4 中的多个路由导致 404 错误
【发布时间】:2019-05-26 13:08:21
【问题描述】:

我开始了一个全新的 symfony 4.2 网站项目。

我创建了两个简单的控制器来渲染两个视图。

第一个 localhost/ 工作正常,但是第二个 localhost/home 导致 404 错误。

我不明白发生了什么,一切都很简单,并且基于官方文档。


routes.yaml 文件:

index:
    path: /
    controller: App\Controller\IntroController::introView

home:
    path: /home
    controller: App\Controller\HomeController::homeView

IntroController.php 文件:

<?php

namespace App\Controller;

use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
use Symfony\Component\HttpFoundation\Response;

class IntroController extends AbstractController
{
    public function introView()
    {
        return $this->render('intro.html.twig');
    }
}

HomeController.php 文件:

<?php

namespace App\Controller;

use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
use Symfony\Component\HttpFoundation\Response;

class HomeController extends AbstractController
{
    public function homeView()
    {
        return $this->render('home.html.twig');
    }
}

php bin/console debug:router命令:

 -------------------------- -------- -------- ------ ----------------------------------- 
  Name                       Method   Scheme   Host   Path                               
 -------------------------- -------- -------- ------ ----------------------------------- 
  _twig_error_test           ANY      ANY      ANY    /_error/{code}.{_format}           
  _wdt                       ANY      ANY      ANY    /_wdt/{token}                      
  _profiler_home             ANY      ANY      ANY    /_profiler/                        
  _profiler_search           ANY      ANY      ANY    /_profiler/search                  
  _profiler_search_bar       ANY      ANY      ANY    /_profiler/search_bar              
  _profiler_phpinfo          ANY      ANY      ANY    /_profiler/phpinfo                 
  _profiler_search_results   ANY      ANY      ANY    /_profiler/{token}/search/results  
  _profiler_open_file        ANY      ANY      ANY    /_profiler/open                    
  _profiler                  ANY      ANY      ANY    /_profiler/{token}                 
  _profiler_router           ANY      ANY      ANY    /_profiler/{token}/router          
  _profiler_exception        ANY      ANY      ANY    /_profiler/{token}/exception       
  _profiler_exception_css    ANY      ANY      ANY    /_profiler/{token}/exception.css   
  index                      ANY      ANY      ANY    /                                  
  home                       ANY      ANY      ANY    /home                              
 -------------------------- -------- -------- ------ ----------------------------------- 

apache sites-enabled.conf 文件:

...
    <VirtualHost *:80>
        ServerName localhostczy
        ServerAdmin webmaster@localhost
        DocumentRoot /var/www/czyjestemladna/public

        <Directory /var/www/czyjestemladna/public>
           Options Indexes FollowSymLinks
           AllowOverride All
           Require all granted
        </Directory>

        ErrorLog ${APACHE_LOG_DIR}/error.log
        CustomLog ${APACHE_LOG_DIR}/access.log combined
    </VirtualHost>

【问题讨论】:

  • 您的 .htaccess 文件怎么样?它应该配置为将所有 url 指向 index.php
  • @Smankusors 我只是想找到有关如何执行此操作的任何解释。不知道从哪里开始。希望它不会破坏我基于这台笔记本电脑上其他框架的其他项目
  • home.html.twig 文件位于模板目录中?也尝试删除缓存文件:rm -r var/cache/*
  • @M.Galardi 是的,两个树枝文件都在模板目录中。我应该删除项目文件或系统中的 /var/cache 吗?我已经用过php bin/console cache:clear
  • 刚刚在项目中。 php bin/console cache:clear is Ok

标签: php symfony symfony4


【解决方案1】:

也许您忘记安装apache pack

在您的项目文件夹中尝试此命令

composer require symfony/apache-pack

这将在公共文件夹中配置您的 .htaccess 文件。基本上,当您请求 localhost\home 时,您的 Apache 将查看您的计算机上不存在的 /var/www/czyjestemladna/public/home。所以 .htaccess 文件中的这一行

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

将重写 Apache 访问文件的方式,它现在将调用 index.php 而不是返回错误 404

【讨论】:

  • 哦,好吧,老实说,我已经跳过了 apache-pack 安装。我很久以前就安装了 apache2 并运行了一些我从未在 Laravel 中完成的项目。害怕覆盖一些配置并丢失我到目前为止所做的事情。在这种情况下运行此命令是否安全?
  • 应该是的,如果你还是担心,那就在运行前备份项目吧:)
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2013-09-12
  • 2021-12-23
  • 1970-01-01
  • 2020-04-10
  • 2015-04-07
  • 2014-07-12
  • 1970-01-01
相关资源
最近更新 更多