PHP 5.3 namespaces & Symfony2

If you’re not used to namespaces, they can be easy to forget about. Essentially, if you’re going to use any class in your code, you need to ensure that the package or namespace it belongs to is imported at the top of your file, like so:

[php]
use Symfony\Component\HttpFoundation\Response;
class DefaultController extends Controller
{
public function indexAction()
{
return new Response("Hello");
}
}[/php]

If you don’t import the class Response, you’ll get a class not found error when you try to instantiate it.

Read more:

Apache virtual hosts config

This approach uses the vhost_alias module combined with a local DNS to let you use any subfolder in your web directory as a virtual host without having to do any additional Apache configuration. This description is for Apache on Windows; the same Apache config will work on any system, but you’ll need to find and configure a local DNS service for your OS to get the full benefit.

1. Apache configuration

First, make sure mod_vhost_alias.so is enabled in httpd.conf.

LoadModule vhost_alias_module modules/mod_vhost_alias.so

Next, place the following code in your httpd-vhosts.conf file:

 ServerName normal.dev
 ServerAlias *.dev
 DirectoryIndex index.php index.html
 UseCanonicalName Off
 VirtualDocumentRoot D:\Jeremy\htdocs\%1\

If you now create a directory like so:

--- htdocs
  |
  --- testsite
    |
    --- index.html

This configuration will allow you to visit http://testsite.dev/index.html without having to add a new vhost in your apache config.

2. DNS configuration

However, before you can test this in your browser, you first need to map testsite.dev to localhost so that your browser can get to your Apache instance and pick up on that nice autoconfigured virtual host. There are two ways you can do this on Windows. One method is to edit the hosts file (type notepad c:\windows\system32\drivers\etc\hosts into the Run dialog) and add a line for each virtual host you want to use:

127.0.0.1        testsite.dev
127.0.0.1        testsite2.dev

Unfortunately, the windows hosts file doesn’t support the use of wildcards, so you can’t just define *.dev and be done with it. So, the preferred method is to install a DNS service like Acrylic. (Download page is here.) Once you’ve done that, you can add wildcards to the Acrylic custom hosts file, which looks just like the windows hosts file:

127.0.0.1        *.dev

3. Configuration for Symfony2

But what if your project, like Symfony2, requires a webroot within the project directory itself? Consider the following configuration:

--- htdocs
  |
  --- testsite
    |
    --- app
    |
    --- bin
    |
    --- src
    |
    --- vendor
    |
    --- web

You don’t want to have all your code in the web root, but you don’t want to have to store all your project files in htdocs either. So you need to define the VirtualDocumentRoot to point to a subdirectory of your project directory.

 ServerName symfony.sym
 ServerAlias *.sym
 DirectoryIndex index.php index.html
 UseCanonicalName Off
 VirtualDocumentRoot D:\Jeremy\htdocs\%1\web

Now, if you visit testsite.dev, Apache will set the document root to /testsite, according to the first set of rules we created; if you visit testsite.sym, Apache will look for a Symfony2-appropriate document root in /testsite/web. This lets you develop projects with various different directory structures without having to change your Apache config.

Don’t forget to create the appropriate rule in your Acrylic configuration too:

127.0.0.1        *.sym