简体   繁体   English

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

[英]Multiple routes in Symfony4 result in 404 error

I started a fresh symfony 4.2 website project. 我开始了一个新的symfony 4.2网站项目。

I have created two simple controllers rendering two views. 我创建了两个呈现两个视图的简单控制器。

The first one localhost/ works fine, however the second one localhost/home results in 404 error. 第一个localhost/正常工作,但是第二个localhost/home导致404错误。

I don't understand what happens, everything is very simple and based on official documentation. 我不明白会发生什么,一切都非常简单,并且基于官方文档。


routes.yaml file: route.yaml文件:

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

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

IntroController.php file: 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 file: 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 command: 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 file: 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>

Maybe you forget to install apache pack ? 也许您忘记安装apache pack

Try this command inside your project folder 在项目文件夹中尝试此命令

composer require symfony/apache-pack

This will configure your .htaccess file inside public folder. 这将在公用文件夹中配置您的.htaccess文件。 Basically, when you request localhost\\home , your Apache will looks at /var/www/czyjestemladna/public/home , which don't exist on your computer. 基本上,当您请求localhost\\home ,您的Apache将查看/var/www/czyjestemladna/public/home ,它在您的计算机上不存在。 So this line from the .htaccess file 因此,.htaccess文件中的这一行

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

Will rewrite how Apache access the file, it will now call the index.php instead returning error 404 将重写Apache如何访问文件,它现在将调用index.php而不是返回错误404

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

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