Call A Function From Other Controllers In Component

I have a controller file: protected/components/global.php

This file contains a function name: ipAddress

What must I do in order to run this ipAddress function in another controller file: protected/modules/user/controllers/defaultController.php ?

It’s not a good idea to implement reusable functions/methods in a controller, if it’s not the basecontroller for the other controllers.

You should implement the method (maybe static) in a helper component or another object which is accessible inside a controller method / view … (application component, module).

You can require_once this file somewhere (probably in index.php even), and use it as usual.


$globals = dirname(__FILE__).'/protected/components/globals.php';


echo ipAddress();

Can you publish example code - controller-file ‘global.php’.

Is this really a controller??

If globals.php is not a (helper)component, this is not OOP / Yii style and should not be placed in protected/components.

I’m pretty sure it’s a strange file with

function app()


	return Yii::app();


function l($text, $url = '#', $htmlOptions = array())


	return CHtml::link($text, $url, $htmlOptions);


in it.

There was a time when this file was quite popular.

first of all,

Global.php is located in components and in Yii config, you can see that all the components are loaded by default.

You can call any of function of the any of the component anywhere.

If you want to call a function of a controller, you can do this by as


or you can do it as



Or as


I’ve tried implementing PeRoChAk’s suggestion and finally it works with just a single line:


Thank you so much.