I’m very new to Yii and programming in general, so go easy on me
I want to specify where the image folder is in my controller so that I don’t have to put <?php echo Yii::app()->request->baseUrl; ?> in front of every img src="". Is there an easy way to do that?
You could set up a constant, such as
define('B', Yii::app()->request->baseUrl);
Then in your code you could use it like
echo CHtml::image(B . '/path/to/image.webp');
georgebuckingham:
You could set up a constant, such as
define('B', Yii::app()->request->baseUrl);
Then in your code you could use it like
echo CHtml::image(B . '/path/to/image.webp');
Hmm, I was hoping for something a little more DRY.
I thought you wanted an easy way
In which case, you could subclass CHtml, and override the function image (http://code.google.com/p/yii/source/browse/tags/1.1.7/framework/web/helpers/CHtml.php#383 ), modifying it so instead of just setting $htmlOptions[‘src’] = $src it sets it as:
$htmlOptions['src'] = Yii::app()->request->baseUrl . '/' . $src;
jacmoe
(Jacob Moen)
June 8, 2011, 1:23am
6
Even easier would be do have this in your base Controller class:
public $baseurl;
public function beforeRender() {
$this->baseurl = Yii::app()->request->baseUrl;
return true;
}
Then it will be available as $this->baseurl in your controller and views.
But, George’s solution is less typing.