Symfony2 Starter Tweaks

When you start with symfony you probably use the Symfony Standard Edition.
This is a quite good start but there are somethings that helped me and might help you aswell.
Since every beginning is “schwer” :)
So here there are.

1. replace web/app*.dev with the following index.php

<?php
/**
* Tweaked Symfony2 bootstrap file
*/

// if you don't want to setup permissions the proper way, just uncomment the following PHP line
// read http://symfony.com/doc/current/book/installation.html#configuration-and-setup for more information
umask(0000);

if ( file_exists( dirname(__FILE__).'/../.env.php' ) ){
    $env = require_once( dirname(__FILE__).'/../.env.php' );
}
$env = isset( $env )? $env : 'prod';
$debug = true;
if($env == 'prod'){
	$debug = false;
}

require_once __DIR__.'/../app/bootstrap.php.cache';
require_once __DIR__.'/../app/AppKernel.php';

use Symfony\Component\HttpFoundation\Request;

$kernel = new AppKernel($env, $debug);
$kernel->loadClassCache();
if( $env != 'dev' ){
//require_once __DIR__.'/../app/AppCache.php';
//$kernel = new AppCache($kernel);
}
$request = Request::createFromGlobals();
$response = $kernel->handle($request);
$response->send();
$kernel->terminate($request, $response);

its much nicer to use the DirectoryIndex in your URL and not having the web_app.php, thats the aesthetics.
But it also avoids having some hassle with ajax urls which are defined absolute in some scripts like
url: /myajax/call.json

The Environment can be changed in the .env.php file which just returns the Environment value:

<?php
return $env = 'dev';

Personally i dont mind changing the Env in the code rather than in the url, while in development.
It might be a tick more far away but its rather uncommon anyway and it would be more effort to change the vhost DirectoryIndex on web_dev.php and prod everytime to get a clean url.
(thanks max & joshi)

2. resolve console and webserver user conflict for cache
Different to Symfony 1.x Symfony2 assumes a more sophisticated User Managment on your machine for Security reasons, which absolutly makes sense. Read more about it here:http://symfony.com/doc/current/book/installation.html#configuration-and-setup
This means you have to adjust the rights for the webserver- and for the console user that both can create and delete the cache.
This requires some admin skills which might be a bit too much for starters.
And some people start wondering why the

php app/console cache:clear

command wont work and start using

rm -rf app/cache/*

in desperation.

To fix this Symfony2 recommends as a ‘hotfix’ to uncomment the

umask(0000);

part on top of your bootstrap files.
So simply uncomment the line in app_dev.php ( if you use the one from above its already done :) ) and in app/console.
Thats alright for local setup, for production go and talk to your admin of trust or live the life of adventure.

3. make a local config that can be placed in gitignore
Here you can store config parameters and overwrite common ones to fit your local setup. Put it in the gitignore so your fellow devs can do the same.
Therefor we need to hack the AppKernel.php and redefine the registerContainerConfiguration method like so.

public function registerContainerConfiguration(LoaderInterface $loader) {

        if ($this->getEnvironment() == 'dev') {
            $extrafiles = array(
                __DIR__ . '/config/config_local.yml',
            );
            foreach ($extrafiles as $filename) {
                if (file_exists($filename) && is_readable($filename)) {
                    $loader->load($filename);
                }
            }
        } else {
            $loader->load(__DIR__ . '/config/config_' . $this->getEnvironment() . '.yml');
        }
}

Read more about it:
http://stackoverflow.com/questions/7338767/can-i-include-an-optional-config-file-in-symfony2/10336451#10336451

Now have fun with bundles & services.

2 Replies to “Symfony2 Starter Tweaks”

  1. Thanks! War bis jetzt gar nicht über dieses umask()-Ding im Bilde.

    Ich benutze zur Entwicklung mittlerweile ausschließlich ein lighty-Setup in Kombination mit einem Startup-Script (spawn-fcgi), mittels mod_env hinderlege ich das gewünschte Environment als Umgebungsvariable direkt in die vhost-Konfiguration.

    Ist vielleicht ein wenig mehr Aufwand in der Einrichtung (eine Alternative wäre Apache mit mod_fcgid + suxec, ist aber noch schmerzhafter, dafür startet der fcgi-Daemon immerhin automagisch), aber nie mehr Probleme mit Datei-Berechtigungen zu haben ist die Mühe definitiv Wert…

Comments are closed.