You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
####REST API using Couchbase Capella in PHP using Laravel
3
+
## REST API using Couchbase Capella in PHP using Laravel
4
4
5
5
Often, the first step developers take after creating their database is to create a REST API that can perform Create, Read, Update, and Delete (CRUD) operations for that database. This repo is designed to teach you and give you a starter project (in PHP using Laravel) to generate such a REST API. After you have installed the travel-sample bucket in your database, you can run this application which is a REST API with Swagger documentation so that you can learn:
6
6
7
-
1. How to create, read, update, and delete documents using Key-Value[operations](https://docs.couchbase.com/php-sdk/current/howtos/kv-operations.html) (KV operations). KV operations are unique to Couchbase and provide super fast (think microseconds) queries.
7
+
1. How to create, read, update, and delete documents using Key-Value[operations](https://docs.couchbase.com/php-sdk/current/howtos/kv-operations.html) (KV operations). KV operations are unique to Couchbase and provide super fast (think microseconds) queries.
8
8
2. How to write simple parametrized [N1QL queries](https://docs.couchbase.com/php-sdk/current/howtos/n1ql-queries-with-sdk.html) using the built-in travel-sample bucket.
9
9
10
10
Full documentation for the tutorial can be found on the [Couchbase Developer Portal](https://developer.couchbase.com/tutorial-quickstart-laravel-php/).
11
11
12
-
13
12
## Prerequisites
14
13
15
14
To run this prebuilt project, you will need:
16
15
17
16
-[Couchbase Capella](https://www.couchbase.com/products/capella/) cluster with [travel-sample](https://docs.couchbase.com/php-sdk/current/ref/travel-app-data-model.html) bucket loaded.
18
-
- To run this tutorial using a self-managed Couchbase cluster, please refer to the [appendix](#running-self-managed-couchbase-cluster).
17
+
- To run this tutorial using a self-managed Couchbase cluster, please refer to the [appendix](#running-self-managed-couchbase-cluster).
19
18
-[PHP](https://www.php.net/downloads.php) 8.2 or higher installed
20
-
- Ensure that the PHP version is [compatible](https://docs.couchbase.com/php-sdk/current/project-docs/compatibility.html#php-version-compat) with the Couchbase SDK.
19
+
- Ensure that the PHP version is [compatible](https://docs.couchbase.com/php-sdk/current/project-docs/compatibility.html#php-version-compat) with the Couchbase SDK.
21
20
- Loading Travel Sample Bucket
22
-
If travel-sample is not loaded in your Capella cluster, you can load it by following the instructions for your Capella Cluster:
23
-
-[Load travel-sample bucket in Couchbase Capella](https://docs.couchbase.com/cloud/clusters/data-service/import-data-documents.html#import-sample-data)
21
+
If travel-sample is not loaded in your Capella cluster, you can load it by following the instructions for your Capella Cluster:
22
+
-[Load travel-sample bucket in Couchbase Capella](https://docs.couchbase.com/cloud/clusters/data-service/import-data-documents.html#import-sample-data)
24
23
25
24
## App Setup
26
25
@@ -40,7 +39,36 @@ The dependencies for the application are specified in the `composer.json` file i
40
39
composer install
41
40
```
42
41
42
+
### Initial Configuration
43
+
44
+
After installing dependencies, you need to set up the basic Laravel configuration:
The application will run on port 8000 of your local machine (http://localhost:8000/api/documentation). You will find the Swagger documentation of the API if you go to the URL in your browser. Swagger documentation is used in this demo to showcase the different API endpoints and how they can be invoked. More details on the Swagger documentation can be found in the [appendix](#swagger-documentation).
127
+
The application will run on port 8000 of your local machine ([http://localhost:8000/api/documentation](http://localhost:8000/api/documentation)). You will find the Swagger documentation of the API if you go to the URL in your browser. Swagger documentation is used in this demo to showcase the different API endpoints and how they can be invoked. More details on the Swagger documentation can be found in the [appendix](#swagger-documentation).
@@ -123,152 +151,72 @@ If you would like to add another entity to the APIs, follow these steps:
123
151
-**Create the new entity (collection) in the Couchbase bucket:** You can create the collection using the [SDK](https://docs.couchbase.com/php-sdk/current/howtos/provisioning-cluster-resources.html#collection-management) or via the [Couchbase Server interface](https://docs.couchbase.com/cloud/n1ql/n1ql-language-reference/createcollection.html).
124
152
125
153
-**Define the model:** Create a new model in the `app/Models` directory, similar to the existing `Airline` model. For example, you can create a file `Hotel.php`:
126
-
```php
127
-
namespace App\Models;
128
-
129
-
use Illuminate\Database\Eloquent\Model;
130
154
131
-
class Hotel extends Model
132
-
{
133
-
protected $bucket;
134
-
135
-
protected $fillable = [
136
-
'name',
137
-
'address',
138
-
'city',
139
-
'country',
140
-
'stars'
141
-
];
142
-
143
-
public function __construct(array $attributes = [])
144
-
{
145
-
parent::__construct($attributes);
146
-
$this->bucket = app('couchbase.bucket');
147
-
}
148
-
149
-
// Add methods for querying, saving, and deleting Hotel data
150
-
}
151
-
```
155
+
```php
156
+
namespace App\Models;
157
+
158
+
use Illuminate\Database\Eloquent\Model;
159
+
160
+
class Hotel extends Model
161
+
{
162
+
protected $bucket;
163
+
164
+
protected $fillable = [
165
+
'name',
166
+
'address',
167
+
'city',
168
+
'country',
169
+
'stars'
170
+
];
171
+
172
+
public function __construct(array $attributes = [])
173
+
{
174
+
parent::__construct($attributes);
175
+
$this->bucket = app('couchbase.bucket');
176
+
}
177
+
178
+
// Add methods for querying, saving, and deleting Hotel data
179
+
}
180
+
```
152
181
153
182
-**Define the controller:** Create a new controller in the `app/Http/Controllers` directory, similar to the existing `AirlineController`. For example, you can create a file `HotelController.php`:
154
-
```php
155
-
namespace App\Http\Controllers;
156
-
157
-
use Illuminate\Http\Request;
158
-
use App\Models\Hotel;
159
-
160
-
class HotelController extends Controller
161
-
{
162
-
// Add methods for handling HTTP requests for the Hotel entity
163
-
}
164
-
```
165
-
166
-
-**Define the routes:** In the `routes/api.php` file, define the routes for the new entity similar to the existing routes for airlines:
167
-
```php
168
-
Route::prefix('v1/hotels')->group(function () {
169
-
Route::get('list', 'HotelController@index');
170
-
Route::get('{id}', 'HotelController@show');
171
-
Route::post('{id}', 'HotelController@store');
172
-
Route::put('{id}', 'HotelController@update');
173
-
Route::delete('{id}', 'HotelController@destroy');
174
-
});
175
-
```
176
-
177
-
-**Integration tests:** Create a new test class in the `tests/Feature` directory, similar to the existing tests. For example, you can create a file `HotelIntegrationTest.php`:
178
-
```php
179
-
namespace Tests\Feature;
180
-
181
-
use Tests\TestCase;
182
183
183
-
class HotelIntegrationTest extends TestCase
184
-
{
185
-
// Add test methods for the Hotel endpoints
186
-
}
187
-
```
188
-
189
-
### Running Self Managed Couchbase Cluster
190
-
191
-
If you are running this quickstart with a self-managed Couchbase cluster, you need to [load](https://docs.couchbase.com/server/current/manage/manage-settings/install-sample-buckets.html) the travel-sample data bucket in your cluster and generate the credentials for the bucket.
184
+
```php
185
+
namespace App\Http\Controllers;
192
186
193
-
You need to update the connection string and the credentials in the `config/couchbase.php` file:
187
+
use Illuminate\Http\Request;
188
+
use App\Models\Hotel;
194
189
195
-
```env
196
-
DB_CONN_STR_=couchbase://<your-couchbase-server>
197
-
DB_USERNAME=<your-username>
198
-
DB_PASSWORD=<your-password>
199
-
DB_BUCKET=travel-sample
190
+
class HotelController extends Controller
191
+
{
192
+
// Add methods for handling HTTP requests for the Hotel entity
193
+
}
200
194
```
201
195
202
-
Replace `<your-couchbase-server>`, `<your-username>`, and `<your-password>` with your actual Couchbase server details and credentials.
203
-
> **NOTE:** Couchbase must be installed and running prior to running the Spring Boot app.### Extending API by Adding New Entity
204
-
205
-
If you would like to add another entity to the APIs, follow these steps:
206
-
207
-
-**Create the new entity (collection) in the Couchbase bucket:** You can create the collection using the [SDK](https://docs.couchbase.com/php-sdk/current/howtos/provisioning-cluster-resources.html#collection-management) or via the [Couchbase Server interface](https://docs.couchbase.com/cloud/n1ql/n1ql-language-reference/createcollection.html).
208
-
209
-
-**Define the model:** Create a new model in the `app/Models` directory, similar to the existing `Airline` model. For example, you can create a file `Hotel.php`:
210
-
```php
211
-
namespace App\Models;
212
-
213
-
use Illuminate\Database\Eloquent\Model;
214
-
215
-
class Hotel extends Model
216
-
{
217
-
protected $bucket;
218
-
219
-
protected $fillable = [
220
-
'name',
221
-
'address',
222
-
'city',
223
-
'country',
224
-
'stars'
225
-
];
226
-
227
-
public function __construct(array $attributes = [])
228
-
{
229
-
parent::__construct($attributes);
230
-
$this->bucket = app('couchbase.bucket');
231
-
}
232
-
233
-
// Add methods for querying, saving, and deleting Hotel data
234
-
}
235
-
```
236
-
237
-
-**Define the controller:** Create a new controller in the `app/Http/Controllers` directory, similar to the existing `AirlineController`. For example, you can create a file `HotelController.php`:
238
-
```php
239
-
namespace App\Http\Controllers;
240
-
241
-
use Illuminate\Http\Request;
242
-
use App\Models\Hotel;
243
-
244
-
class HotelController extends Controller
245
-
{
246
-
// Add methods for handling HTTP requests for the Hotel entity
247
-
}
248
-
```
249
-
250
196
-**Define the routes:** In the `routes/api.php` file, define the routes for the new entity similar to the existing routes for airlines:
251
-
```php
252
-
Route::prefix('v1/hotels')->group(function () {
253
-
Route::get('list', 'HotelController@index');
254
-
Route::get('{id}', 'HotelController@show');
255
-
Route::post('{id}', 'HotelController@store');
256
-
Route::put('{id}', 'HotelController@update');
257
-
Route::delete('{id}', 'HotelController@destroy');
258
-
});
259
-
```
197
+
198
+
```php
199
+
Route::prefix('v1/hotels')->group(function () {
200
+
Route::get('list', 'HotelController@index');
201
+
Route::get('{id}', 'HotelController@show');
202
+
Route::post('{id}', 'HotelController@store');
203
+
Route::put('{id}', 'HotelController@update');
204
+
Route::delete('{id}', 'HotelController@destroy');
205
+
});
206
+
```
260
207
261
208
-**Integration tests:** Create a new test class in the `tests/Feature` directory, similar to the existing tests. For example, you can create a file `HotelIntegrationTest.php`:
262
-
```php
263
-
namespace Tests\Feature;
264
209
265
-
use Tests\TestCase;
210
+
```php
211
+
namespace Tests\Feature;
212
+
213
+
use Tests\TestCase;
266
214
267
-
class HotelIntegrationTest extends TestCase
268
-
{
269
-
// Add test methods for the Hotel endpoints
270
-
}
271
-
```
215
+
class HotelIntegrationTest extends TestCase
216
+
{
217
+
// Add test methods for the Hotel endpoints
218
+
}
219
+
```
272
220
273
221
### Running Self Managed Couchbase Cluster
274
222
@@ -277,7 +225,6 @@ If you are running this quickstart with a self-managed Couchbase cluster, you ne
277
225
You need to update the connection string and the credentials in the `config/couchbase.php` file:
Replace `<your-couchbase-server>`, `<your-username>`, and `<your-password>` with your actual Couchbase server details and credentials.
291
-
> **NOTE:** Couchbase must be installed and running prior to running the Spring Boot app.
238
+
239
+
> **NOTE:** Couchbase must be installed and running prior to running the Laravel application.
292
240
293
241
### Swagger Documentation
294
242
@@ -307,4 +255,3 @@ You can try out an API by clicking on the "Try it out" button next to the endpoi
307
255
#### Models
308
256
309
257
Swagger documents the structure of request and response bodies using models. These models define the expected data structure using JSON schema and are extremely helpful in understanding what data to send and expect.
0 commit comments