PHP code example of richardhj / contao-simple-ajax

1. Go to this page and download the library: Download richardhj/contao-simple-ajax library. Choose the download type require.

2. Extract the ZIP file and open the index.php.

3. Add this code to the index.php.
    
        
<?php
require_once('vendor/autoload.php');

/* Start to develop here. Best regards https://php-download.com/ */

    

richardhj / contao-simple-ajax example snippets


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