Laravel API Gateway is a powerful package designed to facilitate the management and routing of API requests in your Laravel application. It provides features such as header manipulation, authentication, and request forwarding.
- Route API requests to different endpoints.
- Manipulate request and response headers.
- Support for multiple authentication methods.
- Easy integration with existing Laravel applications.
You can install the package via Composer:
git clone https://github.com/oixan/laravel_api_gateway.git
In your config/apigateway.php
, you can define the routes for your API Gateway:
return [
'routes' => [
[
'prefix' => '/service1',
'method' => 'GET',
'service_url' => 'https://api.restful-api.dev',
'timeout' => 5000,
'auth' => 'none',
],
// Add more routes as needed
],
];
Filters allow you to process requests before they are forwarded to the target service. You can define filters in your route configuration.
Example route configuration with a filter:
return [
'routes' => [
[
'prefix' => '/service1',
'method' => 'GET',
'service_url' => 'https://api.restful-api.dev',
'timeout' => 5000,
'auth' => 'none',
'filters' => [
\App\Filters\DefaultFilter::class
]
],
// Add more routes as needed
],
];
Here is an example of a filter class that logs a message when processing a request:
namespace App\Filters;
use Closure;
use Illuminate\Log\Logger;
use Illuminate\Http\Request;
class DefaultFilter extends Filter
{
/**
* Processes the current filter without altering the request.
*
* @param Request $request The request to process.
* @param Closure $next The next callback in the chain.
* @return mixed
*/
protected function handler(Request $request, Closure $next)
{
app(Logger::class)->info('DefaultFilter is processing the request');
return $next($request);
}
}
To start the Laravel development server, run the following command:
php artisan serve
You can now access your API Gateway at http://localhost:8000/api
.
You can test your API Gateway using Postman. Make a request to the appropriate endpoint, e.g., http://localhost:8000/api/service1
, and it will be routed through the gateway.
Contributions are welcome! Please follow these steps to contribute:
- Fork the repository.
- Create a new branch (
git checkout -b feature-branch
). - Make your changes.
- Commit your changes (
git commit -m 'Add some feature'
). - Push to the branch (
git push origin feature-branch
). - Create a new Pull Request.
This package is licensed under the MIT License. See the LICENSE file for details.