【发布时间】:2014-11-17 16:13:29
【问题描述】:
我使用 Symfony 2.5(最新版本)来创建一个 Web 应用程序。我使用 Windows 7 上的 WAMP 本地服务器。
当我启动应用程序时,这是我得到http://localhost/Symfony/web/的url。事实上,我想添加 /homepage。
我的第一个包名为 WelcomeBundle 和他的控制器:它涉及我的应用程序主页:Gir/WelcomeBundle/Controller/HomePageController.php
<?php
namespace Gir\WelcomeBundle\Controller;
use Symfony\Bundle\FrameworkBundle\Controller\Controller;
class HomePageController extends Controller
{
public function indexAction()
{
return $this->render('GirWelcomeBundle:HomePage:index.html.twig');
}
}
这是路由文件:Gir/WelcomeBundle/Ressource/config/routing.yml
GirWelcomeBundle_HomePage:
pattern: /
defaults: { _controller: GirWelcomeBundle:HomePage:index }
requirements:
methods: GET
schemes: https
这是通用路由文件:app/config/routing.yml
GirWelcomeBundle:
resource: "@GirWelcomeBundle/Resources/config/routing.yml"
prefix: /
gir_administration:
resource: "@GirAdministrationBundle/Resources/config/routing.yml"
prefix: /
fos_user:
resource: "@FOSUserBundle/Resources/config/routing/all.xml"
gir_user:
resource: "@GirUserBundle/Resources/config/routing.yml"
prefix: /
所以当我首先进入应用程序时,我就有了我的主页,所以它运行良好。这是我启动应用程序时的 url:http://localhost/Symfony/web/
但如果我想像这样添加路径(我在路径行添加/homepage):Gir/WelcomeBundle/Ressource/config/routing.yml
GirWelcomeBundle_HomePage:
pattern: /homepage
defaults: { _controller: GirWelcomeBundle:HomePage:index }
requirements:
methods: GET
schemes: https
应用程序没有找到路径,我有这个错误:
No route found for "GET /" (from "http://localhost/Symfony/")
404 Not Found - NotFoundHttpException
1 linked Exception: ResourceNotFoundException »
有人知道为什么吗?
然后,当我尝试启动管理页面时,浏览器显示该页面是一个无法访问的网页,例如this。
我真的不明白。
该包名为 AdministrationBundle,controller 代码:Gir/AdministrationBundle/Controller/AdministrationController.php
<?php
namespace Gir\AdministrationBundle\Controller;
use Symfony\Bundle\FrameworkBundle\Controller\Controller;
class AdministrationController extends Controller
{
public function indexAction()
{
return $this->render('GirAdministrationBundle:Admin:index.html.twig');
}
}
这是路由文件:Gir/AdministrationBundle/Ressource/config/routing.yml
gir_administration_homepage:
pattern: /administration
defaults: { _controller: GirAdministrationBundle:Administration:index }
requirements:
methods: GET
schemes: https
有人可以帮我解释一下为什么吗?
更新帖子: 这是 web 文件夹中的 .htacess
DirectoryIndex app.php
<IfModule mod_rewrite.c>
RewriteEngine On
# Determine the RewriteBase automatically and set it as environment variable.
# If you are using Apache aliases to do mass virtual hosting or installed the
# project in a subdirectory, the base path will be prepended to allow proper
# resolution of the app.php file and to redirect to the correct URI. It will
# work in environments without path prefix as well, providing a safe, one-size
# fits all solution. But as you do not need it in this case, you can comment
# the following 2 lines to eliminate the overhead.
RewriteCond %{REQUEST_URI}::$1 ^(/.+)/(.*)::\2$
RewriteRule ^(.*) - [E=BASE:%1]
# Sets the HTTP_AUTHORIZATION header removed by apache
RewriteCond %{HTTP:Authorization} .
RewriteRule .* - [E=HTTP_AUTHORIZATION:%{HTTP:Authorization}]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^(.*)$ app_dev.php [QSA,L]
# Redirect to URI without front controller to prevent duplicate content
# (with and without `/app.php`). Only do this redirect on the initial
# rewrite by Apache and not on subsequent cycles. Otherwise we would get an
# endless redirect loop (request -> rewrite to front controller ->
# redirect -> request -> ...).
# So in case you get a "too many redirects" error or you always get redirected
# to the start page because your Apache does not expose the REDIRECT_STATUS
# environment variable, you have 2 choices:
# - disable this feature by commenting the following 2 lines or
# - use Apache >= 2.3.9 and replace all L flags by END flags and remove the
# following RewriteCond (best solution)
RewriteCond %{ENV:REDIRECT_STATUS} ^$
RewriteRule ^app\.php(/(.*)|$) %{ENV:BASE}/$2 [R=301,L]
# If the requested filename exists, simply serve it.
# We only want to let Apache serve files and not directories.
RewriteCond %{REQUEST_FILENAME} -f
RewriteRule .? - [L]
# Rewrite all other queries to the front controller.
RewriteRule .? %{ENV:BASE}/app.php [L]
</IfModule>
<IfModule !mod_rewrite.c>
<IfModule mod_alias.c>
# When mod_rewrite is not available, we instruct a temporary redirect of
# the start page to the front controller explicitly so that the website
# and the generated links can still be used.
RedirectMatch 302 ^/$ /app.php/
# RedirectTemp cannot be used instead
</IfModule>
</IfModule>
【问题讨论】:
-
您是否正确设置了虚拟主机?例如 DocumentRoot /var/www/gir.dev/web 是否使用正确的 .htaccess 启用了 mod_rewrite?
-
我已经更新了我的帖子,所以你可以看到我的 htaccess 代码。
-
你的虚拟主机设置怎么样?
-
@Rooneyl,我还没有创建虚拟主机,它是如何工作的?
标签: php .htaccess symfony url routing