定制 richardhj/contao-simple-ajax 二次开发

按需修改功能、优化性能、对接业务系统,提供一站式技术支持

邮箱:yvsm@zunyunkeji.com | QQ:316430983 | 微信:yvsm316

richardhj/contao-simple-ajax

最新稳定版本:v1.3.2

Composer 安装命令:

composer require richardhj/contao-simple-ajax

包简介

More control over your AJAX requests.

README 文档

README

SimpleAjax provides an endpoint for Ajax endpoints.

While it has been very convenient for Contao 3, this extension is not required for Contao 4 anymore. You should only use this extension in case your extension needs to be compatible with Contao 3 and Contao 4. Find the upgrade instructions below.

Changelog

v1.1.0 introduces the SimpleAjax\Event\SimpleAjax. You are able to register an event listener. If you're not using composer, you might want to use v1.0 instead and use the legacy hooks.

v1.2.0 lets you set an Response instance (from the symfony/http package). This was introduced to prepare a smooth upgrade process to Contao 4.

v1.3.0 is the Contao 4 release with equal features. The extension will not include any features as Contao 3 is approaching EOL.

Usage

Per Event

At first: Listen on the event SimpleAjax\Event\SimpleAjax using one method described here.

Either: Set a Response (recommended)

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);
   }
}

Or: Handwritten response with termination (legacy)

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;
   }
}

Per Hook (legacy)

// 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;
       }
   }
}

Upgrade to Contao 4

This extension has been very convenient for Contao 3. While upgrading to Contao 4, you are advised to use the routing features coming with Contao 4.

In case you want to build an AppBundle (that's most probably true if you are no extension developer): Create an AppBundle

Implement the RoutingPluginInterface in your Contao Manager Plugin and load the routing.yml with the getRouteCollection().

-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');
+    }
}

Create a routing.yml, e.g. in the directory src/AppBundle/Resources/config/routing.yml

Put the following content:

app.ajax_tags:
   path: /ajax_tags/{param1}
   defaults:
       _scope: frontend
       _token_check: true
       _controller: 'AppBundle\Controller\AjaxTagsController'

Create the AppBundle\Controller\AjaxTagsController.php.

You can use https://github.com/richardhj/isotope-klarna-checkout/blob/v1.0/src/Controller/Push.php as a boilerplate.

The __invoke() method contains all the parameters that are mentioned in the path of the routing definition (routing.yml).

统计信息

  • 总下载量: 3.42k
  • 月度下载量: 0
  • 日度下载量: 0
  • 收藏数: 5
  • 点击次数: 0
  • 依赖项目数: 0
  • 推荐数: 0

GitHub 信息

  • Stars: 5
  • Watchers: 4
  • Forks: 2
  • 开发语言: PHP

其他信息

  • 授权协议: LGPL-3.0
  • 更新时间: 2016-10-07

承接程序开发

PHP开发

VUE

Vue开发

前端开发

小程序开发

公众号开发

系统定制

数据库设计

云部署

网站建设

安全加固