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
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
First, we extract the Zend framework release file to a directory
protected is the
application base directory.
Verify that the file
Second, at the beginning of a controller class file, insert the following lines:
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
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));
In order to use namespaced library that follows PSR-0 (such as Zend Framework 2 or Symfony2) you need to register its root as path alias.
As an example we'll use Imagine.
If we put the
Imagine directory under
protected/vendors we'll be able to use
it like the following:
Yii::setPathOfAlias('Imagine',Yii::getPathOfAlias('application.vendors.Imagine')); // Then standard code from Imagine guide: // $imagine = new Imagine\Gd\Imagine(); // etc.
In the code above the name of the alias we've defined should match the first namespace part used in the library.
Yii can also be used as a self-contained library to support developing and enhancing existing 3rd-party systems, such as WordPress, Joomla, etc. To do so, include the following code in the bootstrap code of the 3rd-party system:
The above code is very similar to the bootstrap code used by a typical Yii application
except one thing: it does not call the
run() method after creating the Web application
Now we can use most features offered by Yii when developing 3rd-party enhancements. For example,
we can use
Yii::app() to access the application instance; we can use the database features
such as DAO and ActiveRecord; we can use the model and validation feature; and so on.