


专注于处理 PHP 跨域的 CORS 中间件 1.4.0 版本更新,添加了 Swoft 框架支持
在很早之前个人发布了一个 PHP CORS 中间件!最初的初衷是开发一个替代 barryvdh/laravel-cors 包的工具,这个包的缺点在于维护不够,然后就是代码量庞大,支持性也不多。当然也有优势,首先专注于 Laravel 框架,然后 Satr 较多使用者较多。当然大多数用来放到 Laravel 中够用。
优势
PHP CORS Middleware 代码量更少(核心代码仅四个文件,每个文件拆分很精短),在 Laravel 中支持模式更多,首先就是常规的全站都附加跨域信息,然后是可选仅预检,路由模式和匹配模式。有了这几个模式支持,你可以指定特定路由跨域,也可以分配给组路透跨域等。
推荐:《PHP教程》
除了上述对 Laravel 支持外,还很好了支持以下方式:
● PHP 原生数组,可以使用数组来进行跨域响应头信息的接收,开发者可以自由处理。
● 支持 PSR-7 的请求和响应
● 支持 PSR-15 中间件
● 支持 Laravel/Lumen 框架
● 支持 Swoft 框架
● 支持 Slim 框架
● 支持 ThinkPHP 5 框架
缺点
支持了太多的框架,如果你仅在 Laravel 使用,代码包中还包含了其他框架的支持代码,但是总量非常低,代码强迫症者可能受不了,多余的支持代码只有在对应框架中才会生效和被加载。
本次版本更新内容
随着 PSR-15 的稳定,针对 PSR-15 中间件接口进行了重构。可以更加方便的配置 CORS 信息
增加 Swoft 框架支持见?
https://github.com/medz/cors/issues/6
本次更新内容的具体使用
PSR-15 中间件
先创建一个实例:
use Medz\Cors\PSR\CorsMiddleware; // Settings. $settings = [ 'allow-credentials' => false, 'allow-headers' => ['*'], 'expose-headers' => [], 'origins' => ['*'], 'methods' => ['*'], 'max-age' => 0, ]; // $cors = new Medz\Cors\Cors($settings); // Create CORS instance. // Create CORS middleware instance $middleware = new CorsMiddleware($settings /* $cors */ /* , true */ /* 是否仅处理预检 */); // TODO.
可以看出,新版本可以直接从中间件构造参数进行传递设置了,之前版本必须传递一个 Medz\Cors\Cors 实例,当然,新版本也可以直接传递实例。第二个参数可以进行配置是否仅处理预检请求,默认是处理全部请求。
Swoft 中间件
在配置文件 config/properties/app.php 中进行如下配置:
'components' => [ 'custom' => [ 'Medz\\Cors\\Swoft\\', ], ], 'cors' => [ 'onlyPreflight' => false, // 是否仅 OPTIONS 预检请求才进行跨域信息附加 'settings' => [ /// ... 参考 README 中的 PSR-7 ], ],
全局使用
打开 app/config/beans/base.php 配置如下:
'serverDispatcher' => [ 'middlewares' => [ \Medz\Cors\Swoft\CorsMiddleware::class, ], ],
通过注解使用
通过 @Middleware 和 @Middlewares, 可以很方便的配置中间件到当前的 Controller 和 Action 内。
● 当将此注解应用于 Controller 上,则作用域为整个 Controller
● 将此注解应用于 Action 上,则作用域仅为当前的 Action
use Swoft\Http\Server\Bean\Annotation\Controller; use Swoft\Http\Message\Bean\Annotation\Middleware; use Swoft\Http\Server\Bean\Annotation\RequestMapping; use Medz\Cors\Swoft\CorsMiddleware; /** * Setting Controller middleware. * * @Controller("middleware") * @Middleware(CorsMiddleware::class) */ class CorsOneController { // } /** * Setting Action middleware. */ class CorsTwoController { /** * @RequestMapping() * @Middleware(CorsMiddleware::class) */ public function corsAction(): array { return [ 'message' => 'The action using CORS.' ]; } }
其他
CORS 项目地址 https://github.com/medz/cors ,如果喜欢,欢迎 Star 欢迎 Issues 欢迎 PR。
Seven 的代码太渣,欢迎关注我的新拓展包 medz/cors 解决 PHP 项目程序设置跨域需求。
The above is the detailed content of CORS middleware focused on handling PHP cross-domain version 1.4.0 update. For more information, please follow other related articles on the PHP Chinese website!

Long URLs, often cluttered with keywords and tracking parameters, can deter visitors. A URL shortening script offers a solution, creating concise links ideal for social media and other platforms. These scripts are valuable for individual websites a

Laravel simplifies handling temporary session data using its intuitive flash methods. This is perfect for displaying brief messages, alerts, or notifications within your application. Data persists only for the subsequent request by default: $request-

This is the second and final part of the series on building a React application with a Laravel back-end. In the first part of the series, we created a RESTful API using Laravel for a basic product-listing application. In this tutorial, we will be dev

Laravel provides concise HTTP response simulation syntax, simplifying HTTP interaction testing. This approach significantly reduces code redundancy while making your test simulation more intuitive. The basic implementation provides a variety of response type shortcuts: use Illuminate\Support\Facades\Http; Http::fake([ 'google.com' => 'Hello World', 'github.com' => ['foo' => 'bar'], 'forge.laravel.com' =>

The PHP Client URL (cURL) extension is a powerful tool for developers, enabling seamless interaction with remote servers and REST APIs. By leveraging libcurl, a well-respected multi-protocol file transfer library, PHP cURL facilitates efficient execution of various network protocols, including HTTP, HTTPS, and FTP. This extension offers granular control over HTTP requests, supports multiple concurrent operations, and provides built-in security features.

Do you want to provide real-time, instant solutions to your customers' most pressing problems? Live chat lets you have real-time conversations with customers and resolve their problems instantly. It allows you to provide faster service to your custom

The 2025 PHP Landscape Survey investigates current PHP development trends. It explores framework usage, deployment methods, and challenges, aiming to provide insights for developers and businesses. The survey anticipates growth in modern PHP versio

In this article, we're going to explore the notification system in the Laravel web framework. The notification system in Laravel allows you to send notifications to users over different channels. Today, we'll discuss how you can send notifications ov


Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

AI Hentai Generator
Generate AI Hentai for free.

Hot Article

Hot Tools

mPDF
mPDF is a PHP library that can generate PDF files from UTF-8 encoded HTML. The original author, Ian Back, wrote mPDF to output PDF files "on the fly" from his website and handle different languages. It is slower than original scripts like HTML2FPDF and produces larger files when using Unicode fonts, but supports CSS styles etc. and has a lot of enhancements. Supports almost all languages, including RTL (Arabic and Hebrew) and CJK (Chinese, Japanese and Korean). Supports nested block-level elements (such as P, DIV),

SublimeText3 Linux new version
SublimeText3 Linux latest version

Notepad++7.3.1
Easy-to-use and free code editor

PhpStorm Mac version
The latest (2018.2.1) professional PHP integrated development tool

Dreamweaver CS6
Visual web development tools
