Code snippets for symfony 1.x


Refine Tags

Snippets tagged "configuration"

Moving the front controllers outside the web directory in Symfony 1.4

On occasions we want to move the front controllers of our application to another location and remove them from the web directory, without losing the relative directions to the directories css, js, images, etc..

For this we need to do two things.

  1. Add a little code in the class sfWebRequest to achieve this behavior in the development environment.
  2. Create a filter with the same code for the production environment.

For development environment: Replace the following line in the class sfWebRequest

568 $this->relativeUrlRoot = preg_replace('#/[^/]+\.php5?$#', '', $this->getScriptName());

by the following code

568 $relative = preg_replace('#/[^/]+\.php5?$#', '', $this->getScriptName());
569 $webDir = str_replace('\\', '/', sfConfig::get('sf_web_dir'));
570 $intercept = '/'.implode('/', array_intersect(explode('/',$relative), explode('/',$webDir)));       
571 $position = strpos($webDir, $intercept);
572 $this->relativeUrlRoot = substr($webDir, $position);                 

For production environment:: Create a filter relativeUrl.

class relativeUrlFilter extends sfFilter 
    public function execute($filterChain){
        $context  = $this->getContext();
        $request  = $context->getRequest();
        $relative = preg_replace('#/[^/]+\.php5?$#', '', $request->getScriptName());
        $webDir = str_replace('\\', '/', sfConfig::get('sf_web_dir'));
        $intercept = '/'.implode('/', array_intersect(explode('/',$relative), explode('/',$webDir)));       
        $position = strpos($webDir, $intercept);
        $request->setRelativeUrlRoot(substr($webDir, $position));                

Active a filter in apps/yourapp/config/filters.yml

rendering: ~
security:  ~
  class: relativeUrlFilter
cache:     ~
common:    ~
execution: ~

and that's it. Now you can put the front controllers outside the web directory, for example, in the root directory of the application myproject/index.php and myproject/myapp_dev.php, or in another location within the web directory, for example, myproject/web/frontend/index.php and myproject/web/backend/index.php.

Note: Remember that if you move these files of the web directory, you must update the following line of code according to the new location.

by Ivannis Suárez Jérez on 2010-02-23, tagged configuration 

Use the model in a batch or a test

To use database connections defined in the databases.yml configuration file, you can use the following snippet:

// initialize database manager
$databaseManager = new sfDatabaseManager();
by Fabien Potencier on 2006-05-21, tagged batch  configuration  propel  test