繁体   English   中英

Symfony4中的多个路由导致404错误

[英]Multiple routes in Symfony4 result in 404 error

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

我创建了两个呈现两个视图的简单控制器。

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

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


route.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>

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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM