Views: 4454
Last Modified: 18.01.2022

You can use the following two approaches for processing AJAX queries in component:

class.php

Query handler in component class (class.php file) allows to:

  • Incapsulate complete code in a single class
  • Repeatedly use component methods, data and parameters
  • Use component phrases and templates
  • Re-define standard behaviour in descendant-components

For the component class to process queries, it's necessary to:

  • Implement the interface \Bitrix\Main\Engine\Contract\Controllerable
  • Define action method with suffix Action
  • Implement the method configureActions (usually returns an empty array === default configuration)
  • When you need to add, process errors, you can implement \Bitrix\Main\Errorable

Note: Executing component in Ajax mode sequentially executes CBitrixComponent::onIncludeComponentLang, CBitrixComponent::onPrepareComponentParams and launching an action with filters.

Attention! Executing component in ajax-mode doesn't launch method CBitrixComponent::executeComponent().

Example

<?php
#components/bitrix/example/class.php

use Bitrix\Main\Error;
use Bitrix\Main\ErrorCollection;

if(!defined("B_PROLOG_INCLUDED") || B_PROLOG_INCLUDED!==true)die();

class ExampleComponent extends \CBitrixComponent implements \Bitrix\Main\Engine\Contract\Controllerable, \Bitrix\Main\Errorable
{
	/** @var ErrorCollection */
	protected $errorCollection;

	public function configureActions()
	{
		//when actions does not require configuration, write them as is. They will use default config 
		return [];
	}

	public function onPrepareComponentParams($arParams)
	{
		$this->errorCollection = new ErrorCollection();
		
		//parameter preparation
		//This code **will** be executed upon launching ajax-actions
	}
	
	public function executeComponent()
	{
		//Attention! This code **won't be** executed upon launching ajax-actions
	}
		
	//data from REQUEST will be inserted to parameter $person 
	public function greetAction($person = 'guest')
	{
		return "Hi {$person}!";
	}

	//example of error processing
	public function showMeYourErrorAction():? string
	{
		if (rand(3, 43) === 42)
		{
			$this->errorCollection[] = new Error('You are so beautiful or so handsome');
			//now response will contain errors and automatic 'error' response status. 
			
			return  null;					
		}

		return "Ok";
	}
	
	/**
	 * Getting array of errors.
	 * @return Error[]
	 */
	public function getErrors()
	{
		return $this->errorCollection->toArray();
	}

	/**
	 * Getting once error with the necessary code.
	 * @param string $code Code of error.
	 * @return Error
	 */
	public function getErrorByCode($code)
	{
		return $this->errorCollection->getErrorByCode($code);
	}
}

ajax.php

Query controller handler in the ajax.php file allows creating lightweight ajax-query handler, by directly dedicating logic from component.

To implement this:

  • Create ajax.php file in the component root
  • Define action method with Action suffix

The controller logic is fully the same as module controller description.

<?php
#components/bitrix/example/ajax.php

if(!defined("B_PROLOG_INCLUDED") || B_PROLOG_INCLUDED!==true)die();

class ExampleAjaxController extends \Bitrix\Main\Engine\Controller
{
	#data from REQUEST is automatically inserted into the parameter $person 
	public function sayByeAction($person = 'guest')
	{
		return "Goodbye {$person}";
	}

	public function listUsersAction(array $filter)
	{
		$users = [];
		//user list as per filter
		//array data composition for response
		
		return $users;
	}
}



Courses developed by Bitrix24