Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit 2c88375

Browse files
chuangbotimgws
authored andcommitted
Add service provider to support vendor:publish config (#60)
* Add service provider to support publish config * Add facade to elasticsearch client
1 parent 2194dfe commit 2c88375

File tree

5 files changed

+92
-26
lines changed

5 files changed

+92
-26
lines changed

‎readme.md‎

Lines changed: 24 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -76,7 +76,25 @@ To get started, add Elasticquent to you composer.json file:
7676

7777
"elasticquent/elasticquent": "dev-master"
7878

79-
Once you've run a `composer update`, add the Elasticquent trait to any Eloquent model that you want to be able to index in Elasticsearch:
79+
Once you've run a `composer update`, you need to register Laravel service provider, in your `config/app.php`:
80+
81+
```php
82+
'providers' => [
83+
...
84+
Elasticquent\ElasticquentServiceProvider::class,
85+
],
86+
```
87+
88+
We also provide a facade for elasticsearch-php client (which has connected using our settings), add following to your `config/app.php` if you need so.
89+
90+
```php
91+
'aliases' => [
92+
...
93+
'Es' => Elasticquent\ElasticquentElasticsearchFacade::class,
94+
],
95+
```
96+
97+
Then add the Elasticquent trait to any Eloquent model that you want to be able to index in Elasticsearch:
8098

8199
```php
82100
use Elasticquent\ElasticquentTrait;
@@ -92,7 +110,11 @@ Now your Eloquent model has some extra methods that make it easier to index your
92110

93111
### Elasticsearch Configuration
94112

95-
If you need to pass a special configuration array Elasticsearch, you can add that in an `elasticquent.php` config file at `/app/config/elasticquent.php` for Laravel 4, or `/config/elasticquent.php` for Laravel 5:
113+
By default, Elasticquent will connect to `localhost:9200` and use `default` as index name, you can change this and the other settings in the configuration file. You can add the `elasticquent.php` config file at `/app/config/elasticquent.php` for Laravel 4, or use the following Artisan command to publish the configuration file into your config directory for Laravel 5:
114+
115+
```shell
116+
$ php artisan vendor:publish --provider="Elasticquent\ElasticquentServiceProvider"
117+
```
96118

97119
```php
98120
<?php
@@ -223,29 +245,6 @@ You can also get the type mapping and check if it exists.
223245
Book::getMapping();
224246
```
225247

226-
### Setting a Custom Index Name
227-
228-
Elastiquent will use `default` as your index name, but you can set a custom index name by creating an `elasticquent.php` config file in `/app/config/`:
229-
230-
```php
231-
<?php
232-
233-
return array(
234-
235-
/*
236-
|--------------------------------------------------------------------------
237-
| Default Index Name
238-
|--------------------------------------------------------------------------
239-
|
240-
| This is the index name that Elastiquent will use for all
241-
| Elastiquent models.
242-
*/
243-
244-
'default_index' => 'my_custom_index_name',
245-
246-
);
247-
```
248-
249248
### Setting a Custom Type Name
250249

251250
By default, Elasticquent will use the table name of your models as the type name for indexing. If you'd like to override it, you can with the `getTypeName` function.
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
<?php
2+
3+
namespace Elasticquent;
4+
5+
use Illuminate\Support\Facades\Facade;
6+
7+
/**
8+
* @see \Elasticquent\ElasticquentServiceProvider
9+
*/
10+
class ElasticquentElasticsearchFacade extends Facade
11+
{
12+
/**
13+
* Get the registered name of the component.
14+
*
15+
* @return string
16+
*/
17+
protected static function getFacadeAccessor()
18+
{
19+
return 'elasticquent.elasticsearch';
20+
}
21+
}

‎src/ElasticquentServiceProvider.php‎

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
<?php
2+
3+
namespace Elasticquent;
4+
5+
use Illuminate\Support\ServiceProvider;
6+
7+
class ElasticquentServiceProvider extends ServiceProvider
8+
{
9+
/**
10+
* Bootstrap services.
11+
*
12+
* @return void
13+
*/
14+
public function boot()
15+
{
16+
$this->publishes([
17+
__DIR__.'/config/elasticquent.php' => config_path('elasticquent.php'),
18+
]);
19+
}
20+
21+
/**
22+
* Register services.
23+
*
24+
* @return void
25+
*/
26+
public function register()
27+
{
28+
// Support class
29+
$this->app->singleton('elasticquent.support', function () {
30+
return new ElasticquentSupport;
31+
});
32+
33+
// Elasticsearch client instance
34+
$this->app->singleton('elasticquent.elasticsearch', function ($app) {
35+
return $app->make('elasticquent.support')->getElasticSearchClient();
36+
});
37+
}
38+
}

‎src/ElasticquentSupport.php‎

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
<?php
2+
3+
namespace Elasticquent;
4+
5+
class ElasticquentSupport
6+
{
7+
use ElasticquentClientTrait;
8+
}

‎src/config/elasticquent.php‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,6 @@
2727
| Elasticquent models.
2828
*/
2929

30-
'default_index' => 'my_custom_index_name',
30+
'default_index' => 'default',
3131

3232
);

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /