


CORS middleware focused on handling PHP cross-domain version 1.4.0 update
专注于处理 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!

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

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

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics



Alipay PHP...

JWT is an open standard based on JSON, used to securely transmit information between parties, mainly for identity authentication and information exchange. 1. JWT consists of three parts: Header, Payload and Signature. 2. The working principle of JWT includes three steps: generating JWT, verifying JWT and parsing Payload. 3. When using JWT for authentication in PHP, JWT can be generated and verified, and user role and permission information can be included in advanced usage. 4. Common errors include signature verification failure, token expiration, and payload oversized. Debugging skills include using debugging tools and logging. 5. Performance optimization and best practices include using appropriate signature algorithms, setting validity periods reasonably,

Article discusses late static binding (LSB) in PHP, introduced in PHP 5.3, allowing runtime resolution of static method calls for more flexible inheritance.Main issue: LSB vs. traditional polymorphism; LSB's practical applications and potential perfo

Article discusses essential security features in frameworks to protect against vulnerabilities, including input validation, authentication, and regular updates.

Sending JSON data using PHP's cURL library In PHP development, it is often necessary to interact with external APIs. One of the common ways is to use cURL library to send POST�...

The article discusses adding custom functionality to frameworks, focusing on understanding architecture, identifying extension points, and best practices for integration and debugging.

The application of SOLID principle in PHP development includes: 1. Single responsibility principle (SRP): Each class is responsible for only one function. 2. Open and close principle (OCP): Changes are achieved through extension rather than modification. 3. Lisch's Substitution Principle (LSP): Subclasses can replace base classes without affecting program accuracy. 4. Interface isolation principle (ISP): Use fine-grained interfaces to avoid dependencies and unused methods. 5. Dependency inversion principle (DIP): High and low-level modules rely on abstraction and are implemented through dependency injection.

Session hijacking can be achieved through the following steps: 1. Obtain the session ID, 2. Use the session ID, 3. Keep the session active. The methods to prevent session hijacking in PHP include: 1. Use the session_regenerate_id() function to regenerate the session ID, 2. Store session data through the database, 3. Ensure that all session data is transmitted through HTTPS.
