Symfony4 中的多个路由导致 404 错误
Multiple routes in Symfony4 result in 404 error
我开始了一个全新的 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 站点-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
这将在 public 文件夹中配置您的 .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
我开始了一个全新的 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 站点-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
这将在 public 文件夹中配置您的 .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