Moein Hosseini
Moein Hosseini

Reputation: 4373

use external function in Yii Controller (Helper function)

I develop some web sites by CodeIgniter,it has got helper file,which can be written by developer to use some function in class.

Now I'm developing web site by Yii,and I have php file which has function that should be used in Controller. How can I import them and use them in Controller method?

Upvotes: 1

Views: 1178

Answers (1)

Rajat Singhal
Rajat Singhal

Reputation: 11264

The way we are doing it is this:

We have a file in lib called SharedFunctions.php, and lib.* are imported in config/main, so that Yii, autoloads the SharedFunctions file..

In SharedFunctions.php file

Class SharedFunctions{
    public static function lib() {
        return new SharedFunctions();
    }

    public function myfunction($params) {
        ....
    }
}

Now anywhere in Yii app we can use any function defined in the SharedFunctions file as

SharedFunctions::lib()->myfunction($params);

Upvotes: 3

Related Questions