Jump24——Laravel 开发人员,精准到位。绝不外包。绝不离岸外包。始终卓越。

Passage: A Lightweight API Proxy Gateway for Laravel

最后更新于 经过

Passage: A Lightweight API Proxy Gateway for Laravel image

Passage is a Laravel package by Morcen Chavez that lets your app sit between a client and an external API, forwarding requests and responses while keeping full control over authentication, headers, and payload transformation — all through familiar Laravel routing and middleware.

The typical use case is when you need to call a third-party API from your frontend but don't want to expose API keys, need to normalize payloads, or want to enforce validation and auth logic in one place. Instead of building a custom proxy from scratch, Passage gives you a structured way to do it with minimal boilerplate.

Defining Routes

Routes are registered using the Passage facade, right alongside your regular routes. The {path?} wildcard captures any sub-path and forwards it upstream:

使用 Morcen\Passage\Facades\Passage ;
Passage :: 得到 'github/{path?}' , GithubPassageController ::班级 (英文):
Passage :: 邮政 'stripe/{path?}' , StripePassageController ::班级 (英文):
Passage :: 任何 'payments/{path?}' , PaymentsPassageController ::班级 (英文):

These support the standard HTTP methods — get , post , put , patch , delete , 和 any

Creating a Handler

Each route points to a handler class that controls how requests are forwarded and responses are returned. Generate one with:

php 工匠 passage:controller GithubPassageController

Handlers extend PassageHandler and can implement three methods:

  • getOptions() — sets the upstream base URI and Guzzle options (timeouts, headers, etc.)
  • getRequest() — transforms or injects credentials into the outgoing request
  • getResponse() — transforms the upstream response before it reaches the client

A minimal handler might look like this:

班级 GithubPassageController 延伸 PassageHandler
{
民众 功能 获取选项 () : 大批
{
返回 [
基地 => 'https://api.github.com/' ,
];
}
民众 功能 getRequest 要求 $请求) : 要求
{
返回 $this -> withBearerToken (请求, 配置 'services.github.token' ));
}
}

Note the trailing slash on base_uri — it's required for path forwarding to work correctly.

Built-in Authentication Helpers

Passage ships with three authentication traits you can use inside getRequest() :

  • Bearer token$this->withBearerToken($request, $token)
  • API密钥 (as a header or query param) — $this->withApiKey($request, $key) 或者 $this->withApiKeyQuery($request, $key, 'api_key')
  • HMAC signing$this->withHmacSignature($request, $secret)

You can also scaffold a handler with auth pre-wired:

php 工匠 passage:controller StripePassageController --with-auth=apikey
php 工匠 passage:controller PaymentsPassageController --with-auth=hmac

安全

Passage automatically strips sensitive client headers — cookies, authorization, proxy-authorization — before forwarding requests upstream. If you need to selectively pass certain headers through (like forwarding the client's Authorization header), have your handler implement the AcceptsClientHeaders interface and define the allowedClientHeaders() method to return an allowlist:

班级 GithubPassageController 延伸 PassageHandler 实现 AcceptsClientHeaders
{
民众 功能 allowedClientHeaders () : 大批
{
返回 [ 'authorization' ];
}
}

You can also restrict which hosts can be proxied by setting PASSAGE_ENFORCE_ALLOWED_HOSTS=true in your environment.

Inbound Validation and Resilience

Handlers can validate incoming requests before they ever reach the upstream service by implementing ValidatesInboundRequest . Define Laravel validation rules in a rules() method, and any failures return a 422 — no upstream call is made.

使用 Morcen\Passage\Contracts\ValidatesInboundRequest ;
班级 StripePassageController 延伸 PassageHandler 实现 ValidatesInboundRequest
{
民众 功能 获取选项 () : 大批
{
返回 [ 基地 => 'https://api.stripe.com/' ];
}
民众 功能 规则 () : 大批
{
返回 [
'数量' => [ '必需的' , ‘整数’ , '最小值:1' ],
'货币' => [ '必需的' , '细绳' , 'size:3' ],
];
}
}

For resilience, withRetry() adds automatic retry with exponential backoff:

班级 PaymentsPassageController 延伸 PassageHandler
{
民众 功能 获取选项 () : 大批
{
返回 数组合并
[ 基地 => 'https://payments.example.com/' ],
$this -> withRetry 3 , 200 ), // 3 retries, 200ms initial delay
(英文):
}
}

Passage also supports response caching for GET / HEAD routes and streaming responses for large payloads.

Useful Artisan Commands

  • php artisan passage:list — lists all registered proxy routes
  • php artisan passage:health — checks connectivity to upstream services

You can also disable all proxying without touching your routes by setting PASSAGE_ENABLED=false 在你的 .env


Passage is a clean solution for scenarios where you need a lightweight API proxy inside an existing Laravel app without reaching for a full enterprise gateway. Learn more and explore the source code on GitHub

Yannick Lyn Fatt 的照片

Laravel News 的特约撰稿人和全栈 Web 开发人员。

归档于:
立方体

Laravel 时事通讯

加入超过 4 万名开发者的行列,不错过任何新的技巧、教程等内容。

图像
Laravel 云

轻松创建和管理服务器,并在几秒钟内部署 Laravel 应用程序。

访问 Laravel Cloud
Tinkerwell 徽标

廷克威尔

Laravel 开发者必备的代码运行器。可在本地和生产环境中体验 AI、自动补全和即时反馈功能。

廷克威尔
几天内即可获得 Laravel 代码审查徽标的专家指导

几天内即可获得 Laravel 代码审查方面的专家指导

专家级代码审查!两位拥有 10 年以上 Laravel 开发经验的开发者将为您提供清晰、实用的反馈,帮助团队构建更优质的应用程序。

几天内即可获得 Laravel 代码审查方面的专家指导
PhpStorm 标志

PhpStorm

首选的 PHP IDE,对 Laravel 及其生态系统提供广泛的开箱即用支持。

PhpStorm
Laravel Cloud 标志

Laravel 云

轻松创建和管理服务器,并在几秒钟内部署 Laravel 应用程序。

Laravel 云
了解 Softtech 的标志

了解软科技

Acquaint Softtech 提供 AI 就绪的 Laravel 开发人员,48 小时内即可上手,每月费用为 3000 美元,没有冗长的销售流程,并提供 100% 退款保证。

了解软科技
Kirschbaum 标志

樱桃树

提供创新和稳定性,确保您的Web应用程序取得成功。

樱桃树
Shift 标志

转移

还在运行旧版本的 Laravel?立即实现 Laravel 自动升级和代码现代化,让您的应用程序保持最新状态。

转移
鱼叉:新一代时间跟踪和发票标志

Harpoon:新一代时间跟踪和发票系统

新一代时间跟踪和计费软件,帮助您的机构规划和预测盈利的未来。

Harpoon:新一代时间跟踪和发票系统
Lucky Media 标志

幸运传媒

Get Lucky Now——拥有十余年经验的 Laravel 开发理想之选!

幸运传媒
SaaSykit:Laravel SaaS 入门套件徽标

SaaSykit:Laravel SaaS 入门套件

SaaSykit 是一个多租户 Laravel SaaS 入门套件,包含运行现代 SaaS 所需的所有功能,例如支付、美观的结账界面、管理面板、用户仪表盘、身份验证、现成组件、统计数据、博客、文档等等。

SaaSykit:Laravel SaaS 入门套件
MongoDB 徽标

MongoDB

Enhance your PHP applications with the powerful integration of MongoDB and Laravel, empowering developers to build applications with ease and efficiency. Support transactional, search, analytics and mobile use cases while using the familiar Eloquent APIs. Discover how MongoDB's flexible, modern database can transform your Laravel applications.

MongoDB
Flaky Test Retries in Pest v4.5.0 image

Flaky Test Retries in Pest v4.5.0

阅读文章
Debugging production Laravel apps without losing your mind image

Debugging production Laravel apps without losing your mind

阅读文章
Passage: A Lightweight API Proxy Gateway for Laravel image

Passage: A Lightweight API Proxy Gateway for Laravel

阅读文章
PestPHP Intellisense in Laravel VS Code Extension v1.7.0 image

PestPHP Intellisense in Laravel VS Code Extension v1.7.0

阅读文章
Drop in comments for Filament with Commentions image

Drop in comments for Filament with Commentions

阅读文章
Laravel Starter Kits Now Include Toast Notifications image

Laravel Starter Kits Now Include Toast Notifications

阅读文章