简体   繁体   中英

Symfony omit filename for routing

I am pretty new to Symfony and just learning it. Even though Symfony's documentation has helped me with all of my concerns and answered all the questions I've had so far, I wasn't able to find the solution to this question regarding routing.

Whenever I access a page, I need to specify the front-controller in order to trigger the routing:

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

What I want to do to clean up the URL even further is omitting the front-controller:

http://localhost/symfony/test

This might be a simple thing, but I haven't been able to find a single SO question or documentation page which describes me how to do it (if it's at all possible). I have tried renaming the front-controller to index.php, which unfortunately doesn't work either.

if your httpd.conf says allows overriding

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>

you can use this 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>

be sure ifmodule is enabled; link

so in your prod environment you replace app_dev.php with app.php

you may also want to have different parameters for each environment, the best to do this is to put it into .gitignore and create two files :

parameters.prod.yml
parameters.dev.yml

and depending on stage you create a parameters.yml file with a symbolic link in the same dir like :

ln -s parameters.dev.yml parameters.yml 

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM