elastica php yii,Yii 1.1.*集成elasticsearch php 客户端Elastica
Yii??是一個基于組件、用于開發大型 Web 應用的高性能 PHP 框架。Yii提供了今日Web 2.0應用開發所需要的幾乎一切功能。Yii是最有效率的PHP框架之一。官方網站
ElasticSearch ?是一個基于Lucene構建的開源,分布式,RESTful搜索引擎。設計用于云計算中,能夠達到實時搜索,穩定,可靠,快速,安裝使用方便。支持通過HTTP使用JSON進行數據索引。
Elastica ?是用php寫的elasticsearch客戶端,通過Elastica可很方便的在php應用中訪問elasticsearch,如:創建索引,添加文檔等。官方網站
在Yii中集成elasticsearch其實非常簡單:
第一步:將下載的Elastica整個拷貝到protected/vendors/目錄下(注意:如果是從github上clone下來的,應該是Elastica下lib下面的Elastica)
第二步:編寫Elastica類自動導入文件,如?ElasticaAutoLoader.php
/**
* Description of ElasticaAutoLoader
*
* @author Owner
*/
class ElasticaAutoLoader {
/**
* @var array class prefixes
*/
static $prefixes = array(
'Elastica'
);
/**
* @var string path to where Zend classes root is located
*/
static $basePath = null;
/**
* Class autoload loader.
*
* @static
* @param string $className
* @return boolean
*/
static function loadClass($className) {
foreach (self::$prefixes as $prefix) {
if (strpos($className, $prefix . '_') !== false) {
if (!self::$basePath)
self::$basePath =
Yii::getPathOfAlias("application.vendors") . '/';
include self::$basePath . str_replace('_', '/', $className) . '.php';
return class_exists($className, false) ||
interface_exists($className, false);
}
}
return false;
}
}
?>
第三步:修改index.php文件
將
// change the following paths if necessary
$yii=dirname(__FILE__).'/../yii/framework/yii.php';
$config=dirname(__FILE__).'/protected/config/main.php';
// remove the following lines when in production mode
defined('YII_DEBUG') or define('YII_DEBUG',true);
// specify how many levels of call stack should be shown in each log message
defined('YII_TRACE_LEVEL') or define('YII_TRACE_LEVEL',3); require_once($yii);
Yii::createWebApplication($config)->run(); 改為:
// change the following paths if necessary
$yii = dirname(__FILE__) . '/../yii/framework/yii.php';
$config = dirname(__FILE__) . '/protected/config/main.php';
// remove the following lines when in production mode
defined('YII_DEBUG') or define('YII_DEBUG', true);
// specify how many levels of call stack should be shown in each log message
defined('YII_TRACE_LEVEL') or define('YII_TRACE_LEVEL', 3);
require_once(dirname(__FILE__) . '/protected/functions/yii.php');
require_once(dirname(__FILE__) . '/protected/functions/functions.php');
require_once($yii);
$app = Yii::createWebApplication($config);
// adding custom Zend Framework autoloader
Yii::import("application.vendors.*");
Yii::import("application.components.ElasticaAutoLoader", true);
Yii::registerAutoloader(array('ElasticaAutoLoader','loadClass'), true);
$app->run();
完成上面幾步就可以直接在程序中使用了!
總結
以上是生活随笔為你收集整理的elastica php yii,Yii 1.1.*集成elasticsearch php 客户端Elastica的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: UTF8各国语言分段表
- 下一篇: icloud 照片导出_如何将iClou