I want to register user script globally, to be available all over the site. Now I insert in every action in my controllers:
Yii::app()->clientScript->registerScriptFile(Yii::app()->baseUrl . '/js/script.js');
But really I understand that it's not good way...
The best way to register global js and css files I think is registering them in beforeRender() method (not in beforeAction() - because if you render json or xml this may destroy your structure) of some BaseController.
You can do this in this way : initiate init function in base controller class having path protected/components/controller.php
U can do like this: 1. create private attribute $_assetsUrl; 2. then in the module or controller
Hope this was useful, see also this link http://www.yiiframework.com/wiki/148/understanding-assets/
If you are looking forward to use themes in your project, i would put some css and scripts in layout file (views/layouts/my-layout-file.php). Because if you changing theme you will be using another css and maybe sometimes another scripts, so you would not want to mix it together.
But some main css and scipts, that didn't change accross themes, i would put in main Controller (protected/components/Controller.php) And all other controllers (/protected/controllers/) would extend this class Controller
And so if all your controllers using on parent class, you can edit just parent class and add something like this
And all your actions will be now using same script.
EDIT: @realtebo (in comments) pointed out to use 'beforeRender' not 'beforeAction'.
See more: Understanding the view rendering flow