5

I have one controller like as below .

<?php
namespace Drupal\users\Controller;

use Drupal\Core\Controller\ControllerBase;


class UsersController extends ControllerBase {


  public function getList() {
    $time = time();
    $data = array();
    $response = $data = $result = null;

      // add all the data in one multiple dim. array
      $data['title'] = 'Example to consume REST API of User List';
      $data['users'] = $result->list;
      $data['form_data'] = $result->form_data;

      // display the content in the middle section of the page
      $build = array(
        '#theme' => 'users_list', // assign the theme [users-list.html.twig]
        '#title' => 'Example to consume REST API', 
        '#pagehtml' => 'API URL : api/users/list ',
        '#data' => $data
      ); 
    }


    return $build;
  }
}

this above function is called and render the HTML in the middle of the page in HTML format where page URL is call as per defined in routing file (http://siteurl.com/user-list). I want to use this HTML output in ajax request which will call the same function through ajax instead of page url.

<?php
namespace Drupal\ajax\Controller;

use Drupal\Core\Controller\ControllerBase;
use Symfony\Component\HttpFoundation\JsonResponse;

class AjaxController extends ControllerBase {

  public function call_ajax(){

      //  my target is like as below
       $op = Drupal\Core\Controller\UsersController()->getList();
       return $op;
  }
}

Q) Is there any way to get the output of getList() in HTML format in ajax callback?

  • Did you try render() function? While returning variable $op, pass it through render() function. eg: return render($op); – ARUN Aug 23 '16 at 10:08
7

After lots of searching finally, I got the solution. I would like to share this.

AjaxController.php

namespace Drupal\ajax\Controller;

use Drupal\Core\Controller\ControllerBase;
use Symfony\Component\HttpFoundation\JsonResponse;

class AjaxController extends ControllerBase {

  public function call_ajax($controller,$function){
    $args = func_get_args();
    $r = array();
    $r['html'] = null;

    $class = "\\Drupal\\".$controller."\\Controller\\".ucfirst($controller)."Controller";

    $obj = new $class();

    if(method_exists($obj,$function)){
      $r['html'] = render($obj->$function());
    }
    return new JsonResponse($r);
  }

}

ajax.routing.yml

ajax.call:
  path: '/call/ajax/{controller}/{function}'
  defaults:
    _controller: '\Drupal\ajax\Controller\AjaxController::call_ajax'
    _title: 'Get List'
  methods:  [GET]
  requirements:
    _permission: 'access content'

ajax.js

jQuery(document).ready(function($a){
    $a.ajax({
        url: "call/ajax/users/getList", 
        method :'GET',
        dataType: "json", 
        success: function(result){
          $a(".region-content").html(result.html);
        }
    });
})

Now I have to just pass the controller and function name to call the function using ajax.

The main purpose to use this way is reusability of controller functions.

| improve this answer | |
  • 3
    This is a neat solution, but without checking the list of allowed controllers and functions you open every function from every controller up to the public. – Geat Dec 7 '18 at 0:36

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.