Cleaner way to have a dynamically generated param

Hi, I need to have a param that holds the path to the images.

It needs to be relative to Yii::getPathOfAlias(‘webroot’) since the app is tested in and deployed to different environments.

But can’t use Yii::getPathOfAlias(‘webroot’) inside config/main.php, so this is the way I found to make it work, although it’s a little dirty as I see it.

I added this to the end of the entry script index.php


$theYii = Yii::createWebApplication( $config );

$theYii->params['imagePath']  = Yii::getPathOfAlias('webroot').'/images';

$theYii->run();

Got a cleaner solution?

I think that what you have done is best if you don’t or can’t touch the config/main.php file

Maybe a parameter is not the best place to store a dynamic path. I usually only save user defined config options there. Since you now kind of hard code the path in your index.php it’s not really configurable in the end. How about using a custom alias instead?





// config/main.php:

'params'=>array(

  'imageAlias' => 'webroot.images',

  ...

// index.php

$app=Yii::createWebApplication($config);

Yii::setPathOfAlias('images',  Yii::getPathOfAlias($config['params']['imageAlias']));

$app->run();


// Somewhere:

$imageFolder=Yii::getPathOfAlias('images'); // not much longer than accessing a parameter



Nice one Mike,

I will mark this as a great hint