Symfony省略路由的文件名

时间:2014-06-26 12:01:31

标签: php symfony routing symfony-2.5

我对Symfony很新,只是学习它。尽管Symfony的文档帮助了我解决了所有问题并回答了我迄今为止遇到的所有问题,但我无法找到关于路由的问题的解决方案。

每当我访问页面时,我都需要指定前端控制器才能触发路由:

http://localhost/symfony/app_dev.php/test

我想要进一步清理URL的目的是省略前端控制器:

http://localhost/symfony/test

这可能是一件简单的事情,但我还没有能够找到一个SO问题或文档页面来描述我该怎么做(如果它可能的话)。我已经尝试将前端控制器重命名为index.php,遗憾的是,它也无法正常工作。

1 个答案:

答案 0 :(得分:1)

如果您的httpd.conf说明允许覆盖

NameVirtualHost *:80
<VirtualHost *:80>
  ServerName foo.localhost.com
  DocumentRoot "/Projects/YourProject/web"
  <Directory "/Projects/YourProject/web">
    Options Indexes FollowSymLinks
    AllowOverride All
    Allow from All
  </Directory>
</VirtualHost>

您可以使用此网站/ .htacces

# Use the front controller as index file. It serves as a fallback solution when
# every other rewrite/redirect fails (e.g. in an aliased environment without
# mod_rewrite). Additionally, this reduces the matching process for the
# start page (path "/") because otherwise Apache will apply the rewriting rules
# to each configured DirectoryIndex file (e.g. index.php, index.html, index.pl).
DirectoryIndex app.php

<IfModule mod_rewrite.c>
    RewriteEngine On
    RewriteCond %{REQUEST_URI}::$1 ^(/.+)/(.*)::\2$
    RewriteRule ^(.*) - [E=BASE:%1]
    RewriteCond %{ENV:REDIRECT_STATUS} ^$
    RewriteRule ^app.php(/(.*)|$) %{ENV:BASE}/$2 [R=301,L]    ##### this is the part that you     should tweak, have the .htaccess point the request to app_dev.php, since the routing.yml is empty initially
    RewriteCond %{REQUEST_FILENAME} -f
    RewriteRule .? - [L]
    RewriteRule .? %{ENV:BASE}/app_dev.php [L]        ##### this is the part that you should tweak, have the .htaccess point the request to app_dev.php, since the routing.yml is empty initially
</IfModule>

<IfModule !mod_rewrite.c>
    <IfModule mod_alias.c>
        # When mod_rewrite is not available, we instruct a temporary redirect of
        # the startpage to the front controller explicitly so that the website
        # and the generated links can still be used.
        RedirectMatch 302 ^/$ /app_dev.php/
        # RedirectTemp cannot be used instead
    </IfModule>
</IfModule>

确保ifmodule已启用; link

所以在你的prod环境中你用app.php替换app_dev.php

您可能还希望为每个环境提供不同的参数,最好这样做是将它放入.gitignore 并创建两个文件:

parameters.prod.yml
parameters.dev.yml

并根据阶段创建一个在同一目录中带有符号链接的parameters.yml文件,如:

ln -s parameters.dev.yml parameters.yml