Destroy every zig! The official Gerry Vandermaesen blog

10Mar/108

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.

Comments (8) Trackbacks (0)
  1. Simple, yet effective! cewl.

  2. 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

  3. As long as you don’t configure a .dev domain in your production server’s vhosts, it shouldn’t be a problem.

  4. 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?

  5. 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.

  6. 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.

  7. Nice tip! Will implement it this week in one of my projects. Thanks!


Leave a comment

(required)

No trackbacks yet.