<?php
namespace Vendor\Example\Controller;
use \Bitrix\Main\Error;
class Item extends \Bitrix\Main\Engine\Controller
{
private $actionsConfig = [
'view' => [
'-prefilters' => [
'\Bitrix\Main\Engine\ActionFilter\Authentication'
]
],
'add' => [
'-prefilters' => [
'\Bitrix\Main\Engine\ActionFilter\Authentication'
]
]
];
public function init()
{
parent::init();
foreach($this->actionsConfig as $name => $arConfig) $this->setActionConfig($name, $arConfig);
}
public function addAction(array $fields):? array
{
$item = Item::add($fields);
if (!$item)
{
$this->addError(new Error('Could not create item.', 500));
return null;
}
return $item->toArray();
}
public function viewAction($id):? array
{
$item = Item::getById($id);
if (!$item)
{
$this->addError(new Error('Could not find item.',404));
return null;
}
return $item->toArray();
}
}
<?php
//modules/vendor.example/.settings.php
return [
'controllers' => [
'value' => [
'defaultNamespace' => '\\Vendor\\Example\\Controller',
],
'readonly' => true,
]
];
BX.ajax.runAction('vendor:example.Item.add',{
data: {
fields: {
ID: 1,
NAME: "test"
}
}
}).then(function (response) {
console.log(response);
}, function (response) {
console.log(response);
});
Спасибо что помогли!