clusterpoint/laravel-clusterpoint

4.0.6 2016-10-21 10:10 UTC

This package is not auto-updated.

Last update: 2024-09-20 22:03:31 UTC


README

Latest Stable Version Total Downloads Latest Unstable Version License

官方文档

API 文档可以在 Clusterpoint 网站 上找到。

安装

  1. 安装 包。
    composer require clusterpoint/laravel-clusterpoint
  2. 你的 config/app.php 文件中 注册 服务提供者。
    Clusterpoint\ClusterpointServiceProvider::class
  3. 发布 配置文件。
    php artisan vendor:publish --provider="Clusterpoint\ClusterpointServiceProvider"
  4. 编辑配置 – 主要配置文件路径是
    config/clusterpoint.php
    但我们在 Laravel 项目根目录的 .env 文件中添加你的凭据。
CP_HOST=https://api-eu.clusterpoint.com/v4  
CP_ID=42  
CP_USERNAME=myusername@clusterpoint.com  
CP_PASSWORD=mypassword  

使用示例

##客户端使用示例 这里可以看到使用我们的服务的标准 Laravel 控制器。

<?php

namespace App\Http\Controllers;

use Clusterpoint\Client; // use our package.

class ExampleController extends Controller
{
    public function getIndex() {
		
		$cp = new Client(); // by defualt uses 'default' connection name from ./config/clusterpoint.php
		
		// Set the collection to work with to initalize the query builder for it.
		$collection = $cp->database("database.collection");

		// Build your query
		$results = $collection->where('color', 'red')
			->where('availability', true)
			->limit(5)
			->groupBy('category')
			->orderBy('price')
			->select(['name', 'color', 'price', 'category'])
			->get();
			
		// Access your results
		return $results[0]->price;
	}
}

##模型使用示例 首先,在你的 app 文件夹中创建你的模型

<?php

namespace App;

use Clusterpoint\Model;

class Example extends Model
{
	protected $db = "database.collection"; // set your databse and collection names
	//protected $primaryKey = "custom_id"; // If you want to define specific specific primary key, default = _id
}

现在你可以在控制器中使用模型。

<?php

namespace App\Http\Controllers;

use App\Example; // use your model.

class ExampleController extends Controller
{	
    public function getIndex() {
		$example = Example::where('price', '>', 200)->first();
		
		$example->price = 300;
		$example->save();
		
		return view('example', compact('example'));
	}
}

##路由模型绑定示例 我们将使用上面创建的模型来展示这个示例,首先在 app/Http/routes.php 文件中绑定你的模型,如下所示

<?php

Route::model('example', 'App\Example');
Route::get('/examples/{example}', 'ExampleController@getIndex');

现在,如果你在 URL 中传递主键值,例如 myweb.dev/examples/42,你可以在控制器中这样访问文档。

<?php

namespace App\Http\Controllers;

use App\Example;

class ExampleController extends Controller
{
	public function getIndex($example) {
		$id = $example->_id; // value is 42
		$name = $example->name;
		$price = $example->price;
		return view('example', compact('name','price'));
	}
}

##多个连接示例 你可以在模型中设置多个连接并使用所需的内容。

首先在主配置文件 config/clusterpoint.php 中添加一个连接设置数组。例如,一个名为 "test" 的连接

<?php

return array(
  "default" => array(
    'host' => env('CP_HOST', 'https://api-eu.clusterpoint.com/v4'),
    'account_id' => env('CP_ID', ''),
    'username' => env('CP_USERNAME', ''),
    'password' => env('CP_PASSWORD', ''),
  ),
  "test" => array(
    'host' => env('CP1_HOST', 'https://api-eu.clusterpoint.com/v4'),
    'account_id' => env('CP1_ID', ''),
    'username' => env('CP1_USERNAME', ''),
    'password' => env('CP1_PASSWORD', ''),
  )
);

但我们建议你在 Laravel 项目的根目录的 .env 文件中添加你的凭据。

CP1_HOST=https://api-eu.clusterpoint.com/v4  
CP1_ID=42  
CP1_USERNAME=myusername@clusterpoint.com  
CP1_PASSWORD=mypassword  

现在你可以在你的模型中使用这个连接

<?php

namespace App;

use Clusterpoint\Model;

class Example extends Model
{
	protected $connection = "test";
}

支持、功能请求与错误报告

许可

Clusterpoint 4.0 PHP 客户端 API - Laravel 包是开源软件,许可协议为 MIT 许可