Installation
composer require priyankpatel/proxify-laravel
Publish the config file:
php artisan vendor:publish --provider="Proxify\ProxifyServiceProvider"
Configuration
Edit config/proxify.php to define your OAuth2 API endpoints, client credentials, and proxy routes:
'apis' => [
'stripe' => [
'client_id' => env('STRIPE_CLIENT_ID'),
'client_secret' => env('STRIPE_CLIENT_SECRET'),
'token_url' => 'https://api.stripe.com/v1/oauth/token',
'api_url' => 'https://api.stripe.com/v1/',
],
],
First Use Case
Create a proxy route in routes/api.php:
Route::get('/stripe/{endpoint}', 'ProxifyController@proxy')->name('stripe.proxy');
Then call /api/stripe/customers to proxy requests to Stripe's API.
Route Definition
Use the ProxifyController to handle proxy requests:
Route::match(['get', 'post', 'put', 'delete'], '/{api}/{endpoint}', 'ProxifyController@proxy');
Dynamic API Routing
Leverage the api and endpoint parameters to dynamically route requests:
// Proxifies GET /api/stripe/customers to Stripe's API
Route::get('/{api}/{endpoint}', 'ProxifyController@proxy');
Authentication Handling Automatically attach OAuth2 tokens to requests via middleware:
// In ProxifyServiceProvider.php
$router->aliasMiddleware('proxify.auth', \Proxify\Middleware\AuthenticateProxy::class);
Custom Headers and Payloads
Extend the ProxifyController to modify requests before forwarding:
public function proxy(Request $request, $api, $endpoint)
{
$request->merge(['custom_param' => 'value']);
return parent::proxy($request, $api, $endpoint);
}
Response Transformation
Override the transformResponse method in a custom controller to modify API responses:
protected function transformResponse($response, $api, $endpoint)
{
return $response->json()->all();
}
Token Expiry Handling
public function handle($request, Closure $next)
{
if ($request->hasHeader('authorization') && $this->isTokenExpired()) {
$newToken = $this->refreshToken();
$request->headers->set('authorization', 'Bearer ' . $newToken);
}
return $next($request);
}
CORS Issues
Route::middleware('cors')->group(function () {
Route::get('/{api}/{endpoint}', 'ProxifyController@proxy');
});
Rate Limiting
throttle middleware:
Route::middleware('throttle:60,1')->group(function () {
Route::get('/{api}/{endpoint}', 'ProxifyController@proxy');
});
Logging Proxy Requests Add logging to debug proxy requests/responses:
protected function proxy(Request $request, $api, $endpoint)
{
\Log::info('Proxy Request', [
'api' => $api,
'endpoint' => $endpoint,
'method' => $request->method(),
'data' => $request->all(),
]);
return parent::proxy($request, $api, $endpoint);
}
Environment-Specific Config
Use Laravel's .env to manage API credentials per environment:
STRIPE_CLIENT_ID=test_123
STRIPE_CLIENT_SECRET=secret_456
Testing Proxies Mock the HTTP client in tests to avoid real API calls:
$this->app->instance(\Illuminate\Http\Client\PendingRequest::class, function () {
return \Mockery::mock(\Illuminate\Http\Client\PendingRequest::class)
->shouldReceive('asForm')
->andReturnSelf()
->shouldReceive('post')
->andReturn(new \Illuminate\Http\Client\Response($this->mockResponse(), 200));
});
Extending Proxy Logic
Create a custom proxy handler by extending ProxifyController:
class CustomProxifyController extends \Proxify\ProxifyController
{
protected function getApiConfig($api)
{
// Custom logic to fetch API config
return config("proxify.custom_apis.{$api}");
}
}
Error Handling
Customize error responses by overriding the handleError method:
protected function handleError($response, $api, $endpoint)
{
if ($response->status() === 401) {
return response()->json(['error' => 'Unauthorized'], 401);
}
return parent::handleError($response, $api, $endpoint);
}
How can I help you explore Laravel packages today?