【发布时间】: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