How to Set Base URL in Yii Framework

David Xia picture David Xia · Sep 30, 2011 · Viewed 75.6k times · Source

When I do

print_r(Yii::app()->request->baseUrl)

I get an empty string. A post on the Yii forum says this is blank by default. How can I change its default value so that I can use absolute URLs?

Answer

ecco picture ecco · Jun 26, 2012

You can edit /path/to/application/protected/config/main.php to change the default value. Add a request component, and configure baseUrl porperty.

return array(
    ...
    'components' => array(
        ...
        'request' => array(
            'baseUrl' => 'http://www.example.com',
        ),
    ),
);