0

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

每当我访问一个页面时,我需要指定前端控制器以触发路由:

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

为了进一步清理 URL,我想要做的是省略前端控制器:

http://localhost/symfony/test

这可能是一件简单的事情,但我无法找到一个单一的 SO 问题或文档页面来描述我如何做到这一点(如果可能的话)。我曾尝试将前端控制器重命名为 index.php,但不幸的是这也不起作用。

4

1 回答 1

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>

你可以使用这个 web/.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 已启用;关联

因此,在您的产品环境中,您将 app_dev.php 替换为 app.php

您可能还希望为每个环境设置不同的参数,最好的做法是将其放入 .gitignore 并创建两个文件:

parameters.prod.yml
parameters.dev.yml

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

ln -s parameters.dev.yml parameters.yml 
于 2014-06-26T13:49:03.917 回答