Skip to content

Instantly share code, notes, and snippets.

@valentineus
Created March 3, 2019 12:21
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save valentineus/dee51bb2130c03330c06260eb43e7909 to your computer and use it in GitHub Desktop.
Save valentineus/dee51bb2130c03330c06260eb43e7909 to your computer and use it in GitHub Desktop.
An example of integration Slim Framework into 1C-Bitrix.
<?php
# local/php_interface/bootstrap.php
declare(strict_types = 1);
define('NOT_CHECK_PERMISSIONS', true);
define('NO_AGENT_CHECK', true);
$_SERVER['DOCUMENT_ROOT'] = dirname(__DIR__, 2);
require $_SERVER['DOCUMENT_ROOT'] . '/bitrix/modules/main/include/prolog_before.php';
use Bitrix\Main\Application;
/**
* Урезанная инициализация 1C-Bitrix.
*
* @throws \Bitrix\Main\SystemException
*/
function initBitrixCore()
{
global $DB;
$DB->db_Conn = Application::getInstance()::getConnection()->getResource();
// Авторизация под администратором
$_SESSION['SESS_AUTH']['USER_ID'] = 1;
}
<?php
# api/index.php
declare(strict_types = 1);
define('NOT_CHECK_PERMISSIONS', true);
require_once $_SERVER['DOCUMENT_ROOT'] . '/local/php_interface/bootstrap.php';
use Slim\App as Slim;
use Slim\Container;
use Slim\Http\Request;
use Slim\Http\Response;
$container = new Container([
'settings' => [
'addContentLengthHeader' => true,
'debug' => true,
'displayErrorDetails' => true,
'routerCacheFile' => false,
],
]);
/**
* Переписанный обработчик исключений.
* Все ошибки генерируются в JSON формате.
*
* @return \Closure
*/
$container['errorHandler'] = function () {
return function (Request $request, Response $response, Throwable $exception) {
return $response->withJson([
'file' => $exception->getFile(),
'message' => $exception->getMessage(),
'trace' => $exception->getTrace(),
], 500);
};
};
$app = new Slim($container);
$app->group('/api', function (Slim $app) use ($key) {
$app->get('/test', function (Request $request, Response $response) {
return $response->withJson(['msg' => 'Hello, World!']);
});
});
$app->run();
<?php
# urlrewrite.php
declare(strict_types = 1);
$arUrlRewrite = [
0 => [
'CONDITION' => '#^/rest/#',
'RULE' => '',
'ID' => null,
'PATH' => '/bitrix/services/rest/index.php',
'SORT' => 100,
],
1 => [
'CONDITION' => '#^/api/#',
'RULE' => '',
'ID' => null,
'PATH' => '/api/index.php',
'SORT' => 100,
],
];
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment