0 follower

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 an 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');

The above code includes the class file Lucene.php. Because we are using a relative path, we need to change the PHP include path so that the file can be located correctly. This is done by calling Yii::import before require_once.

Once the above set up is ready, we can use the Lucene class in a controller action, like the following:

$lucene=new Zend_Search_Lucene($pathOfIndex);
$hits=$lucene->find(strtolower($keyword));