Home > Article > Backend Development > Yii Framework Official Guide Series 36 - Extending Yii: Using Third-Party Libraries
Yii is carefully designed so that third-party libraries can be easily integrated to further expand the functionality of Yii. When using third-party libraries in a project, programmers often encounter problems regarding class naming and file inclusion. Because all Yii classes begin with the letters C
, this reduces class naming problems that may arise; and because Yii relies on SPL autoload to perform class file inclusion, if they use the same autoload function or PHP include path to include the class files, it combines well.
Below we use an example to illustrate how to use the Zend_Search_Lucene component from Zend framework in a Yii application.
First, assuming protected
is the application base directory, we extract the Zend Framework release files to the protected/vendors
directory. Confirm that the protected/vendors/Zend/Search/Lucene.php
file exists.
Second, at the beginning of a controller class file, add the following lines:
Yii::import('application.vendors.*'); require_once('Zend/Search/Lucene.php');
The above code contains the class file Lucene.php
. Because we are using relative paths, we need to change the PHP include path so that the files can be located correctly. This is done by calling Yii::import
before require_once
.
Once the above setup is ready, we can use the Lucene
class in the controller action, similar to the following:
$lucene=new Zend_Search_Lucene($pathOfIndex); $hits=$lucene->find(strtolower($keyword));
The above is the Yii Framework Official Guide Series 36 - Extensions Yii: Content using third-party libraries, please pay attention to the PHP Chinese website (www.php.cn) for more related content!
##