Symfony4中的多条路由会导致404错误

时间:2018-12-28 18:51:36

标签: php symfony symfony4

我开始了一个新的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 /控制台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>

1 个答案:

答案 0 :(得分:2)

也许您忘记安装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