Get baseUrl in Javascript

My application is deployed in a URL prefixed with "http://hostname/baseurl/". The "/baseurl" part is environment-dependent, and varies from server to server. Now I need to make ajax call in separate js file. I need to pass the prefix to the js file so that the ajax URL is portable. How to pass the PHP variable to the Javascript code? Is there a solution in Yii?

Thank you!

use Yii::app()->baseUrl

This is a possibility:


Yii::app()->clientScript->registerScript('helpers', '

	someUrl = '.CJSON::encode(Yii::app()->createUrl('blabla')).';

	baseUrl = '.CJSON::encode(Yii::app()->baseUrl).';

');?>



You can use the variables someUrl and baseUrl in your Javascript files or snippets now.

hth, yodel

Thank you zaccaria, that’s the easy part. Hard part is the interoperability of Javascript.

Thank you yodel. It is working! Pretty elegant.

Also need to note that if this variable is going to be used in a $(document).ready() function, it must be loaded before it, say CClientScript::POS_HEAD or CClientScript::POS_BEGIN.

ok, javascript files are not support the related path in MVC, if you want to use any php code, like the baseUrl , in your javascript file, do the following, in your main layout file, in the head section, add-


<script>

var baseurl="<?php print Yii::app()->request->baseUrl;?>";

</script>

and now you can use this baeurl in your javascript file, enjoy :slight_smile:

Thanks hofrob, your suggestion worked great. I wanted to share my change to the solution since I wanted to namespace my url vars:


<?php                                                                                                                                                                                                                                                                                                                                                                

      Yii::app()->clientScript->registerScript('helpers', '                                                           

          yii = {                                                                                                     

              urls: {                                                                                                 

                  saveEdits: '.CJSON::encode(Yii::app()->createUrl('edit/save')).',                                   

                  base: '.CJSON::encode(Yii::app()->baseUrl).'                                                        

              }                                                                                                       

          };                                                                                                          

      ');                                                                                                             

?> 

now the urls can be access in javascript like this:


yii.urls.base;

maybe this is better ???


<?php                                                                                                                                                                                                                                                                                                                                                                

      Yii::app()->clientScript->registerScript('helpers', '                                                           

          yii = {                                                                                                     

              urls: {                                                                                                 

                  saveEdits: '.CJSON::encode(Yii::app()->createUrl('edit/save')).',                                   

                  base: '.CJSON::encode(Yii::app()->baseUrl).'                                                        

              }                                                                                                       

          };                                                                                                          

      ',CClientScript::POS_HEAD);                                                                                                             

?> 

add CClientScript::POS_HEAD

Thank you all for these solutions. The only thing I had to change to make it work for me was this:


base: '.CJSON::encode(Yii::app()->getBaseUrl(true)).',