Skip to content

Commit

Permalink
Merge pull request #11 from morcen/9-passage-command-to-create-passag…
Browse files Browse the repository at this point in the history
…e-controller

9 passage command to create passage controller
  • Loading branch information
morcen committed Aug 6, 2023
2 parents 7650395 + b8d74cb commit cb66475
Show file tree
Hide file tree
Showing 8 changed files with 1,248 additions and 683 deletions.
88 changes: 84 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,8 @@ This is a powerful API Gateway package for Laravel that allows you to efficientl
## Features

- Request routing to multiple microservices
- Request validation and transformation (coming soon)
- Request payload validation and transformation
- Response payload transformation
- Authentication and authorization handling (coming soon)
- Rate limiting and throttling (coming soon)
- Caching and response caching (coming soon)
Expand All @@ -31,11 +32,23 @@ You can install the package via composer:
composer require morcen/passage
```

You can publish the config file with:
Then install the package using the following command:
```bash
php artisan passage:install
```

This will publish the package's config file at `config/passage.php`.

If you wish to create a controller for handling requests, publish the controller stub using the following command:
```bash
php artisan vendor:publish --tag=passage-stubs
```

and then generate Passage controllers by running:
```bash
php artisan vendor:publish --tag="passage-config"
php artisan passage:controller {name}
```
where `{name}` is the name of the controller you want to generate.


## Usage
Expand Down Expand Up @@ -72,7 +85,74 @@ return [
> - Make sure that the `base_uri` ends with a trailing slash `/`, otherwise the request might not be forwarded properly.
> - All headers, query parameters, as well as the type of request (`GET`, `POST`, etc.) will be forwarded to the service.

#### Disabling `Passage`
##### Transforming/validating requests and response through a Passage controller
If you have a service called `github` and you want to handle the incoming and outgoing payloads, you can create a controller for it by running:
```bash
php artisan passage:controller GithubPassageController
```
This will create a controller at `app/Http/Controllers/Passage/GithubPassageController.php`.

In your controller, you can define the following methods:
```php
// app/Http/Controllers/Passage/GithubPassageController.php
use App\Http\Controllers\Controller;
use Illuminate\Http\Client\Response;
use Illuminate\Http\Request;
use Morcen\Passage\PassageControllerInterface;

class GithubPassageController extends Controller implements PassageControllerInterface
{
/**
* Transform and/or validate the request before it is sent to the service.
*
* @param Request $request
* @return Request
*/
public function getRequest(Request $request): Request
{
// Transform the request here
return $request;
}

/**
* Transform or validate the response before it is sent back to the client.
*
* @param Request $request
* @param Response $response
* @return Response
*/
public function getResponse(Request $request, Response $response): Response
{
// Transform the response here
return $response;
}

/**
* Set the route options when the service is instantiated.
*
* @return array
*/
public function getOptions(): array
{
return [
'base_uri' => 'https://api.github.com/',
];
}
}
```

In your `config/passage.php`, add the controller to the `services` array:
```php
// config/passage.php
return [
'services' => [
'github' => \App\Http\Controllers\Passage\GithubPassageController::class, // <-- Add this line,
]
]
```


### Disabling `Passage`
To disable `Passage` on a server/application level, set `PASSAGE_ENABLED` to `false` in your `.env` file:
```env
PASSAGE_ENABLED=false
Expand Down
6 changes: 3 additions & 3 deletions composer.json
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
{
"name": "morcen/passage",
"version": "v1.0.0",
"version": "v1.2.3",
"description": "API gateway for Laravel",
"keywords": [
"morcen",
"laravel",
"passage",
"laravel api gateway",
"laravel api proxy"
"api-gateway",
"api-proxy"
],
"homepage": "https://github.com/morcen/passage",
"license": "MIT",
Expand Down

0 comments on commit cb66475

Please sign in to comment.