Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to Set Base URL in Yii Framework

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?

like image 358
David Xia Avatar asked Sep 30 '11 16:09

David Xia


2 Answers

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',
        ),
    ),
);
like image 109
ecco Avatar answered Sep 29 '22 03:09

ecco


As the post in that forum says, it might be different for different platforms, or if the web app isnt located in the default folder.
All these things work for me:

echo Yii::app()->request->baseUrl."<br/>" ;
print_r(Yii::app()->request->baseUrl);
echo "<br/>";
var_dump(Yii::app()->getBaseUrl(true));
echo "<br/>";
echo Yii::app()->request->getBaseUrl(true);

I used yiic to create the web app, with default settings using the following command in a terminal, yiic webapp /path/to/webapp
So that generates the necessary directory structure for the web app, and also the default skeleton files. Try it and then see how it works. I'm new to yii myself.

Edit:

This solution might have worked for the op, but the correct way baseUrl can be set is shown by ecco's answer to this question.

like image 30
bool.dev Avatar answered Sep 29 '22 02:09

bool.dev