My typical symfony frontend controller
You probably dislike URL's like http://yourdomain.com/backend.php/foo as much as I do.
For that reason I prefer to set up multiple aliases in my vhost for my symfony project, all pointing to the same webroot. For example:
1 2 3 4 5 6 7 8 9 10 | <VirtualHost *:80> ServerName www.mydomain.dev ServerAlias admin.mydomain.dev DocumentRoot "/my/symfony/project/web" <Directory "/my/symfony/project/web"> AllowOverride All Order allow,deny Allow from all </Directory> </VirtualHost> |
Once you set up your different vhosts, you can set up some rewriting rules in your .htaccess to point to the right PHP file depending on the requested host, but I prefer to do that bit of magic in my frontend controller (index.php).
This is what my typical frontend controller looks like:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 | <?php require_once(dirname(__FILE__) . '/../config/ProjectConfiguration.class.php'); if (strpos($_SERVER['SERVER_NAME'], 'admin.') === 0) { $app = 'backend'; } else { $app = 'frontend'; } if (substr($_SERVER['SERVER_NAME'], -4) == '.dev') { $env = 'dev'; $debug = true; } else { $env = 'prod'; $debug = false; } $configuration = ProjectConfiguration::getApplicationConfiguration($app, $env, $debug); sfContext::createInstance($configuration)->dispatch(); |
This allows me to remove any additional PHP scripts in the /web directory (typically frontend_dev.php, backend.php and backend_dev.php) and even has the added benefit of saving you some worries about accidently deploying the development environments to production.
March 10th, 2010 - 16:55
Simple, yet effective! cewl.
March 10th, 2010 - 19:09
About the dev environment, what happen if I define myself the .dev extension in my hosts file to point to your server ? I’m not sure it does secure the dev mode
My two cents
March 10th, 2010 - 19:29
As long as you don’t configure a .dev domain in your production server’s vhosts, it shouldn’t be a problem.
March 11th, 2010 - 14:00
True
June 16th, 2010 - 16:51
Sup Gerry, BigTrucK from #symfony. I implemented this strategy but still found that link helpers like url_for were including the index.php within the url, for example: /index.php/vehicle/search. So when I post a form it takes me out of the dev controller b/c the link didn’t propagate fe_dev. Maybe I’m missing something?
June 16th, 2010 - 16:56
Hi BigTruck. Yes, you also need to enable no_script_name in every settings.yml for every app/environment you want to use this way.
June 16th, 2010 - 17:12
Ahhh, I think it works for you b/c you are using .dev in the host section of your url. I’m trying to keep the hosts similar with something like mydomain.com/fe_dev vs mydomain.com/index.php.
July 1st, 2010 - 13:58
Nice tip! Will implement it this week in one of my projects. Thanks!