Skip to content

Instantly share code, notes, and snippets.

@but1head
Created April 1, 2018 15:38
Show Gist options
  • Save but1head/23afb7c8d36f0956d9f27084a86d9d64 to your computer and use it in GitHub Desktop.
Save but1head/23afb7c8d36f0956d9f27084a86d9d64 to your computer and use it in GitHub Desktop.
мини рест для modx на slim
<?php
use \Psr\Http\Message\ServerRequestInterface as Request;
use \Psr\Http\Message\ResponseInterface as Response;
require '........../autoload.php'; // путь к композеру
define('MODX_API_MODE', true);
require $_SERVER["DOCUMENT_ROOT"].'/index.php';
$app = new \Slim\App;
$app->add(function ($req, $res, $next) {
$response = $next($req, $res);
return $response
->withHeader('Access-Control-Allow-Origin', '*')
->withHeader('Access-Control-Allow-Headers', 'X-Requested-With, Content-Type, Accept, Origin, Authorization')
->withHeader('Access-Control-Allow-Methods', 'GET, POST, PATCH, PUT, DELETE, OPTIONS');
});
// check token
$app->add(function ($request, $response, $next) {
// токен в headers = Authorization: Bearer ТОКЕН
/* if(!$request->isOptions() && $request->getHeader('Authorization')[0] != 'Bearer ТОКЕН') {
return $response->withStatus(401);
} */
return $next($request, $response);
});
// get resources
$app->get('/v1/resources', function ($request, $response, $args) use ($modx) {
$result = $modx->_functions->SQL("SELECT * FROM `modx_site_content` ORDER BY `id` DESC");
return $response->withJson($result);
});
// get resource
$app->get('/v1/resources/{id}', function ($request, $response, $args) use ($modx) {
$id = $args['id'];
$result = $modx->_functions->SQL("SELECT * FROM `modx_site_content` WHERE `id` = $id");
return $response->withJson($result);
});
// save resource
$app->patch('/v1/resources/{id}', function ($request, $response, $args) use ($modx) {
$id = $args['id'];
$result = $modx->_functions->_UPDATE('modx_site_content', $request->getParsedBody(), 'id', $id);
return $response->withJson(array('success' => $result));
});
// delete resource
$app->delete('/v1/resources/{id}', function (Request $request, Response $response, array $args) use ($modx) {
$id = $args['id'];
$result = $modx->_functions->SQL("DELETE FROM `modx_site_content` AND `id` = $id");
return $response->withJson(array('success' => $result));
});
$app->run();
<?php
if($modx->context->key == 'mgr') return;
switch($modx->event->name) {
case 'OnMODXInit':
require_once MODX_BASE_PATH .'assets/.............../functions.php'; // путь к файлу
$modx->_functions = $functions;
break;
}
<?php
$functions = new functions($modx);
class functions {
public $modx;
function __construct(modX &$modx){
$this->modx =& $modx;
$this->pdoFetch = $this->modx->getService('pdofetch','pdoFetch', MODX_CORE_PATH . 'components/pdotools/model/pdotools/', array());
}
function SQL($SQL, $results = true, $one = false){
$output = '';
$q = $this->modx->prepare($SQL);
if(!$q->execute()){
$this->modx->log(1, print_r($SQL, true));
return false;
}
if($results) $output = $one ? $q->fetch(PDO::FETCH_ASSOC) : $q->fetchAll(PDO::FETCH_ASSOC);
return $output;
}
// 'modx_table_name', array('row1' => 1, 'row2' =>2)
function _INSERT($table, $data) {
$keys = array_keys($data);
$fields = '`' . implode('`,`', $keys) . '`';
//$placeholders = mb_substr(str_repeat('?,', count($keys)), 0, -1);
$tmp = [];
foreach($keys as $k) $tmp[] = ":{$k}";
$placeholders = implode(',', $tmp);
$sql = "INSERT INTO {$table} ({$fields}) VALUES ({$placeholders});";
//$sql = "INSERT INTO {$table} ({$fields}) VALUES ({$placeholders});";
$stmt = $this->modx->prepare($sql);
if (!$stmt->execute($data)) {
$this->modx->log(1, print_r($stmt->errorInfo, true) . ' SQL: ' . $sql);
return false;
}
return true;
}
// 'modx_table_name', array('row1' => 1, 'row2' => 2), 'id', 1
function _UPDATE($table, $data, $key, $val ){
if(!$key || !$val) return false;
$placeholders = array();
foreach(array_keys($data) as $k) $placeholders[] = "`{$k}` = :{$k}";
$placeholders = implode(', ', $placeholders);
$sql = "UPDATE {$table} SET {$placeholders} WHERE `{$key}` = '{$val}';";
$stmt = $this->modx->prepare($sql);
if (!$stmt->execute($data)) {
$this->modx->log(1, print_r($stmt->errorInfo, true));
$this->modx->log(1, ' SQL: ' . $sql);
return false;
}
/* foreach($data as $k => $v) $placeholders[] = "`{$k}` = '{$v}'";
$placeholders = implode(', ', $placeholders);
$sql = "UPDATE {$table} SET {$placeholders} WHERE `{$key}` = '{$val}';";
$stmt = $this->modx->prepare($sql);
if (!$stmt->execute()) {
$this->modx->log(1, print_r($stmt->errorInfo, true));
$this->modx->log(1, ' SQL: ' . $sql);
return false;
} */
return true;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment