I have file with functions which I want to use everywhere in my application. In raw PHP the way to do it is to call them in each file by include or require. Is there any simple way in Yii to call this file once and use it everywhere? Second question is to call the same file and use it in a particular controller.

有帮助吗?

解决方案

You can require this file in main config of your appllication for example, or in beforeAcition method of application base controller if you have it.

 require_once  dirname(dirname(__FILE__)) . DIRECTORY_SEPARATOR . 'components/some_funcs.php';

其他提示

maybe this could help you THE DEFINITIVE GUIDE TO YII.

Using 3rd-Party Libraries

Yii is carefully designed so that third-party libraries can be easily integrated to further extend Yii's functionalities. When using third-party libraries in a project, developers often encounter issues about class naming and file inclusion. Because all Yii classes are prefixed with letter C, it is less likely class naming issue would occur; and because Yii relies on SPL autoload to perform class file inclusion, it can play nicely with other libraries if they use the same autoloading feature or PHP include path to include class files.

Below we use an example to illustrate how to use the Zend_Search_Lucene component from the Zend framework in a Yii application.

First, we extract the Zend framework release file to a directory under protected/vendors, assuming protected is the application base directory. Verify that the file protected/vendors/Zend/Search/Lucene.php exists.

Second, at the beginning of a controller class file, insert the following lines:

Yii::import('application.vendors.*');
require_once('Zend/Search/Lucene.php');
许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top