richardhj / contao-simple-ajax
Requires
- php: ^5.6 || ^7.0
- contao/core-bundle: ^4.4
- symfony/http-foundation: ^3.3 || ^4.1
Requires (Dev)
- phpcq/all-tasks: ~1.0
Replaces
- contao-legacy/simple_ajax: *
- leounglaub/contao-simple-ajax: *
This package is auto-updated.
Last update: 2019-09-10 02:11:51 UTC
README
SimpleAjax 提供了一个 Ajax 端点的接口。
虽然它对 Contao 3 非常方便,但这个扩展在 Contao 4 中不再需要。只有在你的扩展需要与 Contao 3 和 Contao 4 兼容的情况下,才应使用此扩展。以下为升级说明。
变更日志
v1.1.0 引入了 SimpleAjax\Event\SimpleAjax
。你可以注册事件监听器。如果你不使用 composer,你可能想使用 v1.0 并使用传统的钩子。
v1.2.0 允许你设置一个 Response
实例(来自 symfony/http 包)。这是为了平滑升级到 Contao 4 而引入的。
v1.3.0 是 Contao 4 的版本,具有相同的功能。由于 Contao 3 即将达到 EOL,该扩展将不会包含任何新功能。
使用方法
按事件
首先:使用以下方法之一监听事件 SimpleAjax\Event\SimpleAjax
。
或者:设置一个 Response
(推荐)
class MyAjaxListener { public function myMethod(\SimpleAjax\Event\SimpleAjax $event) { if ('myrequest' !== \Input::get('acid')) { return; } $return = ['foo', 'bar', 'foobar']; $response = new \Symfony\Component\HttpFoundation\JsonResponse($return); $event->setResponse($response); } }
或者:手写响应并终止(传统)
class MyAjaxListener { public function myMethod(\SimpleAjax\Event\SimpleAjax $event) { if ('myrequest' !== \Input::get('acid')) { return; } // Check whether the SimpleAjaxFrontend.php was requested if (false === $event->isIncludeFrontendExclusive()) { return; } $return = ['foo', 'bar', 'foobar']; header('Content-Type: application/json'); echo json_encode($return); exit; } }
按钩子(传统)
// config.php $GLOBALS['TL_HOOKS']['simpleAjax'][] = array('MyClass', 'myMethod'); $GLOBALS['TL_HOOKS']['simpleAjaxFrontend'][] = array('MyClass', 'myMethod'); // Use this hook for front end exclusive hooks // MyClass.php class MyClass { public function myMethod() { if ('myrequest' === \Input::get('acid')) { $return = ['foo', 'bar', 'foobar']; header('Content-Type: application/json'); echo json_encode($return); exit; } } }
升级到 Contao 4
此扩展对 Contao 3 非常方便。在升级到 Contao 4 时,建议使用 Contao 4 带来的路由功能。
如果你想要构建一个 AppBundle(如果你不是扩展开发者,这很可能是正确的):创建一个 AppBundle
在你的 Contao 管理插件中实现 RoutingPluginInterface
并使用 getRouteCollection()
加载 routing.yml。
-class Plugin implements BundlePluginInterface +class Plugin implements BundlePluginInterface, RoutingPluginInterface { public function getBundles(ParserInterface $parser): array { // … } + + /** + * Returns a collection of routes for this bundle. + * + * @param LoaderResolverInterface $resolver + * @param KernelInterface $kernel + * + * @return RouteCollection|null + * + * @throws \Exception + */ + public function getRouteCollection(LoaderResolverInterface $resolver, KernelInterface $kernel) + { + return $resolver + ->resolve(__DIR__.'/../Resources/config/routing.yml') + ->load(__DIR__.'/../Resources/config/routing.yml'); + } }
创建一个 routing.yml
,例如在目录 src/AppBundle/Resources/config/routing.yml
中。
放入以下内容
app.ajax_tags: path: /ajax_tags/{param1} defaults: _scope: frontend _token_check: true _controller: 'AppBundle\Controller\AjaxTagsController'
创建 AppBundle\Controller\AjaxTagsController.php
。
你可以使用 https://github.com/richardhj/isotope-klarna-checkout/blob/v1.0/src/Controller/Push.php 作为模板。
在 __invoke()
方法中包含了在路由定义(routing.yml
)路径中提到的所有参数。