pinkcrab / ajax
Perique框架的Ajax创建库。
2.0.0
2023-04-03 22:49 UTC
Requires
- php: >=7.4.0
- pinkcrab/enqueue: 1.*
- pinkcrab/http: 1.0.*
- pinkcrab/perique-framework-core: 2.0.*
- pinkcrab/wp-nonce: *
Requires (Dev)
- dealerdirect/phpcodesniffer-composer-installer: <=1.0.0
- gin0115/wpunit-helpers: 1.1.*
- php-stubs/wordpress-stubs: 6.* || 5.9.*
- phpstan/phpstan: 1.*
- phpunit/phpunit: ^7.0 || ^8.0
- roots/wordpress: 6.1.*
- symfony/var-dumper: <=6.2.7
- szepeviktor/phpstan-wordpress: <=1.1.7
- vlucas/phpdotenv: <=5.5.0
- wp-coding-standards/wpcs: <=2.3.0
- wp-phpunit/wp-phpunit: 6.1.*
- yoast/phpunit-polyfills: ^0.2.0 || ^1.0.0
- dev-master
- 2.0.0
- 1.1.0
- 1.0.4
- 1.0.3
- 1.0.2
- 1.0.1
- 1.0.0
- dev-dependabot/composer/yoast/phpunit-polyfills-tw-0.2.0or-tw-1.0.0or-tw-2.0.0
- dev-dependabot/composer/szepeviktor/phpstan-wordpress-lte-1.3.1
- dev-dependabot/composer/phpunit/phpunit-tw-7.0or-tw-8.0or-tw-9.0
- dev-develop
- dev-perique-v2-dev
- dev-feature/v2/gh35-perique-v2-prep
- dev-feature/update-readme-for-default-config
- dev-feature/gh31-update-for-perique-1-4-0
- dev-feature/gh14-create-static-bootstrap
- dev-feature/fix_7-typos
This package is auto-updated.
Last update: 2024-09-07 09:30:27 UTC
README
Ajax
为PinkCrab Perique框架提供的一个简单但强大的Ajax库。允许创建基于对象的Ajax调用,处理所有基本的Nonce验证、WP操作,并使用HTTP PSR接口。
为什么选择它?
编写WordPress的Ajax脚本可能会非常混乱,需要定义多达2个共享回调的操作。Perique Ajax模块利用框架的注册和依赖注入方面。这允许将服务注入到回调中,从而实现干净和可测试的代码。
Perique Ajax文档
设置
需要PinkCrab Perique框架v2和Composer
使用Composer安装模块
$ composer require pinkcrab/ajax
包含自定义Ajax模块
// file:plugin.php // Boot the app as normal, including the module. $app = ( new App_Factory ) ->default_setup() ->module( \PinkCrab\Ajax\Module\Ajax::class ) ->boot();
用法
创建您的Ajax模型
use PinkCrab\Ajax\Ajax; use PinkCrab\Ajax\Ajax_Helper; use Psr\Http\Message\ResponseInterface; use Psr\Http\Message\ServerRequestInterface; use PinkCrab\Ajax\Dispatcher\Response_Factory; class My_Ajax extends Ajax { /** * Define the action to call. * @var string */ protected $action = 'my_ajax_action'; /** * The ajax calls nonce handle. * @var string */ protected $nonce_handle = 'my_ajax_nonce'; /** * Some service which handles the logic of the call. * @var Some_Service */ protected $my_service; /** * Constructs the object * My_Service will be injected when this is created by the DI Container */ public function __construct( Some_Service $my_service ) { $this->my_service = $my_service; } /** * The callback * * @param \Psr\Http\Message\ServerRequestInterface $request * @param \PinkCrab\Ajax\Dispatcher\Response_Factory $response_factory * @return \Psr\Http\Message\ResponseInterface */ public function callback( ServerRequestInterface $request, Response_Factory $response_factory ): ResponseInterface { // Extract the args from the request, you can also do this manually $args = Ajax_Helper::extract_server_request_args( $request ); // Do something with the request args, ideally in a service class $data_to_return = array_key_exists('foo', $args) ? $this->my_service->do_something($args['foo']) : 'Foo not found!'; // Return with a valid PSR Response. return $response_factory->success( $data_to_return ); } }
这将具有分配了
my_ajax_action
动作的Ajax调用。
**将所有Ajax模型添加到registration.php
**
// file:registration.php return [ .... My_Ajax_Call::class, .... ];
许可协议
MIT许可协议
https://open-source.org.cn/licenses/mit-license.html
预发布版本
- 对于Perique 1.4.*,使用版本1.1.0
- 对于Perique 1.3.*,使用版本1.0.4
- 对于Perique 1.0.* - 1.2.*,使用版本1.0.3
变更日志
- 2.0.0 - 升级对Perique 2.0.0的支持,删除Ajax::bootstrap()并用Ajax模块替换。
- 1.1.0 - 升级对Perique 1.4.0的支持
- 1.0.4 - 更新开发依赖到wp6.1和PinkCrab/HTTP 1.*,停止支持PHP 7.1
- 1.0.3 - 更新开发依赖,更新GH Pipeline并改进在检查是否进行Ajax时的条件。
- 1.0.2 - 添加Ajax_Bootstrap类和::use()方法,简化与Perique的集成。作为Perique.info网站的一部分改进了文档。
- 1.0.1 - 更新yoast/phpunit-polyfills要求从^0.2.0到^0.2.0 || ^1.0.0,由@dependabot在#13中更新
- 1.0.0 - 支持Perique 1.0.0,并包含检查以确保仅在wp_ajax调用时添加
- 0.1.0 从Registerables模块提取出来。现在使用自定义Registration_Middleware服务来调度所有Ajax调用。