x-wp/ajax-router
Composer 安装命令:
composer require x-wp/ajax-router
包简介
Simplifies the use of WordPress AJAX by providing NestJS inspired Controller design.
README 文档
README
WordPress AJAX Router
PSR-7 / PSR-15 compatible AJAX router for WordPress.
Inspired by NestJS and Express - this library provides a simple way to handle AJAX requests in WordPress.
QuickStart
Install the library
composer require x-wp/ajax-server
Define a controller
<?php use Sunrise\Http\Router\Exception\PageNotFoundException; use XWP\Decorator\Core\Controller; use XWP\Decorator\Core\Use_Guards; use XWP\Decorator\HTTP\Argument\Body; use XWP\Decorator\HTTP\Get; use XWP\Decorator\HTTP\Param; use XWP\Decorator\HTTP\Post; use XWP\Guard\Capability_Guard; use XWP\Guard\Nonce_Guard; use XWP\Response\JSON; #[Use_Guards( new Nonce_Guard() )] #[Controller( 'my-endpoint' )] class My_Controller { #[Get( 'get-post/{id}', JSON::class )] public function get_post( #[Param( 'id' )] int $id, ): \WP_Post { return \get_post( $id ); } #[Use_Guards( new Capability_Guard( 'manage_options' ) )] #[Post( 'modify-post/{id}', JSON::class )] public function modify_post( #[Param( 'id' )] int $id, #[Body] array $data ): int { $post = \get_post( $id ); if ( ! $post ) { throw new PageNotFoundException( 'Invalid post', 404 ); } return wp_update_post( array_merge( array( 'ID' => $id ), $data, ), ); } }
Register a controller
xwp_register_routes( My_Controller::class );
This will expose the following endpoints:
GET /wp-ajax/my-endpoint/get-post/{id}POST /wp-ajax/my-endpoint/modify-post/{id}
Ajax base URL is /wp-ajax by default, and can be changed in permalink settings.
Getting Endpoint URLs
In PHP
xwp_ajax_url( 'some', 'endpoint' ); // https://example.com/wp-ajax/some/endpoint xwp_ajax_url( 'some/endpoint' ); // https://example.com/wp-ajax/some/endpoint
In JavaScript
Library adds a global object xwp attached to window object. Both in the admin and on the frontend.
You can disable this by using xwp_ajax_vars filter, or calling the xwp_disable_ajax_js_vars
declare type XWP = { /** * URL for the wp-ajax endpoint */ wpAjaxUrl: string; /** * URL for the admin-ajax endpoint */ adminAjaxUrl: string; /** * Get URL for the wp-ajax endpoint * * @param parts - parts of the URL */ forWpAjax: (...parts: string[]): string; /** * Get URL for the admin-ajax endpoint * * @param action - action name * @param nonce - nonce value (optional) * @param data - data to be sent */ forAdminAjax: (action:string , nonce?: string, data: Record<string, any>|FormData): string; }
const baseUrl = window.xwp.wpAjaxUrl; // https://example.com/wp-ajax const endUrl = window.xwp.adminAjaxUrl; // https://example.com/wp-admin/admin-ajax.php const ajaxEp = window.xwp.forWpAjax( 'some', 'endpoint' ); // https://example.com/wp-ajax/some/endpoint const adminEp = window.xwp.forAdminAjax( 'some_action' ); // https://example.com/wp-admin/admin-ajax.php?action=some_action
FAQ
1. Did you just reinvent the wheel?
Yes, we did. We basically took the wheels from NestJS and Express and put it on a WordPress car.
2. Why?
Read this.
3. Can I use this in my project?
Yes, you can. But remember, this is a work in progress and we are still figuring out the best way to do things. API is subject to change.
4. Can I contribute?
Sure 💪💪💪.
If you have an idea, a suggestion, or a bug report - feel free to open an issue.
5. How does it work?
Read this.
x-wp/ajax-router 适用场景与选型建议
x-wp/ajax-router 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 0 次下载、GitHub Stars 达 3, 最近一次更新时间为 2024 年 03 月 23 日, 在 PHP 生态内属于活跃度较高的组件。
我们在过去多个企业项目中使用过 x-wp/ajax-router 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。
基于 x-wp/ajax-router 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。
线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。
承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。
统计信息
- 总下载量: 0
- 月度下载量: 0
- 日度下载量: 0
- 收藏数: 3
- 点击次数: 37
- 依赖项目数: 0
- 推荐数: 0
其他信息
- 授权协议: GPL-2.0-only
- 更新时间: 2024-03-23