Skip to content

Commit efe3f4b

Browse files
committed
updated tutorial based on feedback
1 parent e0f88bd commit efe3f4b

File tree

1 file changed

+95
-153
lines changed

1 file changed

+95
-153
lines changed

tutorial/markdown/php/quickstart-php-laravel/php-laravel.md

Lines changed: 95 additions & 153 deletions
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,8 @@ To run this prebuilt project, you will need:
3535

3636
- [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.
3737
- To run this tutorial using a self managed Couchbase cluster, please refer to the [appendix](#running-self-managed-couchbase-cluster).
38-
- PHP installed
38+
- [PHP](https://www.php.net/downloads.php) 8.2 or higher installed
39+
- 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.
3940
- Code Editor installed (Visual Studio Code, PhpStorm, or Sublime Text)
4041
- Composer command line
4142

@@ -45,6 +46,12 @@ To run this prebuilt project, you will need:
4546
git clone https://github.com/couchbase-examples/php-laravel-quickstart.git
4647
```
4748

49+
### Change Directory
50+
51+
```shell
52+
cd php-laravel-quickstart
53+
```
54+
4855
### Install Dependencies
4956

5057
```shell
@@ -54,27 +61,39 @@ composer install
5461

5562
> Note: Composer automatically installs the required dependencies when building the project.
5663
57-
### Database Server Configuration
64+
### Initial Configuration
5865

59-
The `CouchbaseServiceProvider` class is a Laravel service provider class responsible for setting up the connection to a Couchbase database in a Laravel application. It defines the following configurations:
66+
After installing dependencies, you need to set up the basic Laravel configuration:
6067

61-
- `couchbase.cluster`: This configuration specifies the Couchbase cluster connection using the provided hostname, username, and password.
62-
- `couchbase.bucket`: This configuration specifies the Couchbase bucket connection using the provided bucket name.
63-
- `couchbase.airlineCollection`: This configuration specifies the airline collection in the Couchbase bucket.
64-
- `couchbase.airportCollection`: This configuration specifies the airport collection in the Couchbase bucket.
65-
- `couchbase.routeCollection`: This configuration specifies the route collection in the Couchbase bucket.
66-
- `couchbase.hotelCollection`: This configuration specifies the hotel collection in the Couchbase bucket.
68+
1. Create the environment file:
69+
70+
```sh
71+
cp .env.example .env
72+
```
73+
74+
2. Generate application key:
75+
76+
```sh
77+
php artisan key:generate
78+
```
79+
80+
### Couchbase Service Provider
81+
82+
The `CouchbaseServiceProvider` class is a Laravel service provider class responsible for setting up the connection to a Couchbase database in a Laravel application.
6783

6884

6985
### Application Environment
70-
You need to configure the connection details to your Couchbase Server in the `config/couchbase.php` file located in the root directory of the project.
7186

72-
In the `config/couchbase.php` file, update the following variables:
73-
- `DB_CONN_STR`: Replace with the connection string of your Couchbase cluster.
74-
- `DB_USERNAME`: Replace with the username of a Couchbase user with access to the bucket.
75-
- `DB_PASSWORD`: Replace with the password of the Couchbase user.
87+
All configuration for communication with the database is read from environment variables. As a security best practice, you should set these values in your `.env` file:
7688

77-
Make sure to save the changes after updating the variables in the `config/couchbase.php` file.
89+
```env
90+
DB_CONN_STR=<connection_string>
91+
DB_USERNAME=<username>
92+
DB_PASSWORD=<password>
93+
DB_BUCKET=travel-sample
94+
```
95+
96+
Replace the placeholder values with your actual Couchbase cluster details.
7897

7998
The connection string should be in the following format:
8099

@@ -126,8 +145,7 @@ docker run -d --name laravel-container -p 8000:8000 \
126145
php-laravel-quickstart
127146
```
128147

129-
Note: The `config/couchbase.php` file has the connection information to connect to your Capella cluster. You can also pass the connection information as environment variables to the Docker container.
130-
If you choose not to pass the environment variables, you can update the `config/couchbase.php` file in the root directory.
148+
Note: Environment variables passed to the Docker container will override the default values in the configuration.
131149

132150
Once the application is running, you can access it in your browser at [http://localhost:8000](http://localhost:8000). Swagger UI is available at [http://localhost:8000/api/documentation](http://localhost:8000/api/documentation).
133151

@@ -162,136 +180,83 @@ Our airline document will have a structure similar to the following example:
162180
}
163181
```
164182

165-
## Let's Review the Code
166-
167-
To begin, open the repository in an IDE of your choice to learn about how to create, read, update, and delete documents in your Couchbase Server.
168-
169-
### Code Organization
170-
171-
- `tests/Feature`: Contains integration tests.
172-
- `app/Http/Controllers`: Contains the controller classes.
173-
- `app/Models`: Contains the model classes.
174-
175-
## API Endpoints Documentation
176-
177-
### 1. Get List of Airlines
178-
179-
**Endpoint:**
180-
181-
`GET /api/v1/airlines/list`
182-
183-
**Description:**
184-
185-
This endpoint retrieves a list of airlines from the database. Clients can optionally filter the results by country and paginate through the results using the `offset` and `limit` query parameters. This helps in managing large datasets by breaking them into smaller, manageable chunks.
186-
187-
**Steps:**
188-
189-
1. The client sends a GET request to the endpoint `/api/v1/airlines/list`.
190-
2. Optionally, the client includes query parameters:
191-
- `offset` specifies the number of items to skip before starting to collect the result set.
192-
- `limit` specifies the number of items to return.
193-
- `country` filters the results to only include airlines from a specific country.
194-
3. The server processes the request and queries the database for airlines that match the given criteria.
195-
4. The server returns a list of airlines. If no airlines match the criteria, an appropriate error message is returned.
196-
197-
---
198-
199-
### 2. Get Airline by ID
200-
201-
**Endpoint:**
183+
## Code Structure
202184

203-
`GET /api/v1/airlines/{id}`
185+
### Project Structure
204186

205-
**Description:**
206-
207-
This endpoint retrieves detailed information about a specific airline by its ID. This is useful for getting the full profile of an airline, including its callsign, country, IATA code, ICAO code, and name.
208-
209-
**Steps:**
210-
211-
1. The client sends a GET request to the endpoint `/api/v1/airlines/{id}`.
212-
2. The client includes the airline ID in the path of the request.
213-
3. The server searches the database for an airline with the specified ID.
214-
4. If the airline is found, the server returns the airline's details. If the airline is not found, an error message is returned.
215-
216-
---
217-
218-
### 3. Create a New Airline
219-
220-
**Endpoint:**
221-
222-
`POST /api/v1/airlines/{id}`
223-
224-
**Description:**
225-
226-
This endpoint allows the creation of a new airline with the specified ID. The client must provide all required airline details in the request body, such as callsign, country, IATA code, ICAO code, and name.
227-
228-
**Steps:**
229-
230-
1. The client sends a POST request to the endpoint `/api/v1/airlines/{id}`.
231-
2. The client includes the airline ID in the path and provides the airline details in the request body.
232-
3. The server validates the provided data to ensure all required fields are present and correctly formatted.
233-
4. If validation passes, the server saves the new airline in the database. If validation fails, an error message is returned.
234-
235-
---
236-
237-
### 4. Update an Existing Airline
238-
239-
**Endpoint:**
240-
241-
`PUT /api/v1/airlines/{id}`
242-
243-
**Description:**
187+
```
188+
.
189+
├── app/
190+
│ ├── Http/
191+
│ │ └── Controllers/
192+
│ │ ├── AirlineController.php
193+
│ │ ├── AirportController.php
194+
│ │ ├── RouteController.php
195+
│ │ └── HotelController.php
196+
│ ├── Models/
197+
│ │ ├── Airline.php
198+
│ │ ├── Airport.php
199+
│ │ ├── Route.php
200+
│ │ └── Hotel.php
201+
│ └── Providers/
202+
│ └── CouchbaseServiceProvider.php
203+
├── config/
204+
│ └── couchbase.php
205+
├── routes/
206+
│ └── api.php
207+
└── tests/
208+
└── Feature/
209+
├── AirlineTest.php
210+
├── AirportTest.php
211+
├── RouteTest.php
212+
└── HotelTest.php
213+
```
244214

245-
This endpoint updates an existing airline or creates a new one if it does not exist. The client must provide the necessary airline details in the request body. This allows updating airline information or adding new airlines to the database.
215+
### Key Components
246216

247-
**Steps:**
217+
- **Controllers**: Handle HTTP requests and responses for each entity
218+
- **Models**: Define data structures and business logic for Couchbase operations
219+
- **Service Provider**: Configures Couchbase connection and collection references
220+
- **Tests**: Integration tests for all API endpoints
248221

249-
1. The client sends a PUT request to the endpoint `/api/v1/airlines/{id}`.
250-
2. The client includes the airline ID in the path and provides the updated airline details in the request body.
251-
3. The server validates the provided data to ensure all required fields are present and correctly formatted.
252-
4. If the airline exists, the server updates the existing record with the new data. If the airline does not exist, the server creates a new airline with the provided details.
253-
5. The server returns a success message indicating whether the airline was updated or created. If validation fails, an error message is returned.
222+
### Airline Entity
254223

255-
---
224+
For this tutorial, we will focus on the airline entity to demonstrate CRUD operations and SQL++ queries.
256225

257-
### 5. Delete an Airline
226+
We will be setting up a REST API to manage airline documents:
258227

259-
**Endpoint:**
228+
- [POST Airline](#post-airline) – Create a new airline
229+
- [GET Airline](#get-airline) – Read specified airline
230+
- [PUT Airline](#put-airline) – Update specified airline
231+
- [DELETE Airline](#delete-airline) – Delete airline
232+
- [List Airlines](#list-airlines) – Get all airlines. Optionally filter the list by country
233+
- [Airlines to Destination](#airlines-to-destination) – Get airlines flying to a destination airport
260234

261-
`DELETE /api/v1/airlines/{id}`
235+
For CRUD operations, we will use the [Key-Value operations](https://docs.couchbase.com/php-sdk/current/howtos/kv-operations.html) that are built into the Couchbase SDK to create, read, update, and delete a document. Every document will need an ID (similar to a primary key in other databases) to save it to the database. This ID is passed in the URL. For other endpoints, we will use [SQL++](https://docs.couchbase.com/php-sdk/current/howtos/n1ql-queries-with-sdk.html) to query for documents.
262236

263-
**Description:**
237+
### POST Airline
264238

265-
This endpoint deletes a specific airline from the database by its ID. This is useful for removing outdated or incorrect airline records.
239+
Create a new airline document using the POST endpoint.
266240

267-
**Steps:**
241+
### GET Airline
268242

269-
1. The client sends a DELETE request to the endpoint `/api/v1/airlines/{id}`.
270-
2. The client includes the airline ID in the path of the request.
271-
3. The server searches the database for an airline with the specified ID.
272-
4. If the airline is found, the server deletes the airline from the database. If the airline is not found, an error message is returned.
243+
Retrieve a specific airline document by its ID using the GET endpoint.
273244

274-
---
245+
### PUT Airline
275246

276-
### 6. Get Airlines Flying to a Destination Airport
247+
Update an existing airline document or create a new one if it doesn't exist using the PUT endpoint.
277248

278-
**Endpoint:**
249+
### DELETE Airline
279250

280-
`GET /api/v1/airlines/to-airport/{destinationAirportCode}`
251+
Delete an airline document by its ID using the DELETE endpoint.
281252

282-
**Description:**
253+
### List Airlines
283254

284-
This endpoint retrieves a list of airlines that fly to a specific destination airport. This can be useful for travelers or systems that need to display available airlines for a particular route.
255+
Retrieve a list of airlines with optional filtering by country and pagination using query parameters.
285256

286-
**Steps:**
257+
### Airlines to Destination
287258

288-
1. The client sends a GET request to the endpoint `/api/v1/airlines/to-airport/{destinationAirportCode}`.
289-
2. The client includes the destination airport code in the path of the request.
290-
3. Optionally, the client includes query parameters:
291-
- `offset` specifies the number of items to skip before starting to collect the result set.
292-
- `limit` specifies the number of items to return.
293-
4. The server processes the request and queries the database for airlines that fly to the specified airport.
294-
5. The server returns a list of airlines. If no airlines are found, an appropriate error message is returned.
259+
Find airlines that fly to a specific destination airport using SQL++ queries to join collections.
295260

296261
## Running The Tests
297262

@@ -301,25 +266,6 @@ This command will execute all the test cases in your project.
301266
php artisan test
302267
```
303268

304-
### Run Individual Tests:
305-
306-
Additionally, you can run individual test classes or methods using the following commands:
307-
308-
To run the tests for the AirlineTest class:
309-
```sh
310-
php artisan test --filter AirlineTest
311-
```
312-
313-
To run the tests for the AirportTest class:
314-
315-
```sh
316-
php artisan test --filter AirportTest
317-
```
318-
319-
To run the tests for the RouteTest class:
320-
321-
```sh
322-
php artisan test --filter RouteTest
323269
```
324270
325271
## Project Setup Notes
@@ -341,22 +287,18 @@ If you would like to add another entity to the APIs, follow these steps:
341287
342288
- **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).
343289
344-
- **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`:
290+
- **Define the model:** Create a new model in the `app/Models` directory, similar to the existing `Airline` model. Note that this quickstart already includes a `Hotel` model as an example. For a new entity, you would create a similar structure:
345291
```php
346292
namespace App\Models;
347293
348294
use Illuminate\Database\Eloquent\Model;
349295
350-
class Hotel extends Model
296+
class YourEntity extends Model
351297
{
352298
protected $bucket;
353299
354300
protected $fillable = [
355-
'name',
356-
'address',
357-
'city',
358-
'country',
359-
'stars'
301+
// Define your entity fields here
360302
];
361303
362304
public function __construct(array $attributes = [])
@@ -365,7 +307,7 @@ If you would like to add another entity to the APIs, follow these steps:
365307
$this->bucket = app('couchbase.bucket');
366308
}
367309
368-
// Add methods for querying, saving, and deleting Hotel data
310+
// Add methods for querying, saving, and deleting data
369311
}
370312
```
371313

0 commit comments

Comments
 (0)