Skip to content

Instantly share code, notes, and snippets.

@opengeek
Created June 9, 2015 15:52
Show Gist options
  • Save opengeek/1ed163084ba7298f64fd to your computer and use it in GitHub Desktop.
Save opengeek/1ed163084ba7298f64fd to your computer and use it in GitHub Desktop.
This is a proof of concept for using MODX 2.x as a dependency in a Slim application
<?php
/*
* This file is part of the MODSlim package.
*
* Copyright (c) Jason Coward <[email protected]>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace MODX\Slim\Middleware;
use Slim\Middleware;
class MODSlim extends Middleware
{
/**
* Call
*
* Perform actions specific to this middleware and optionally
* call the next downstream middleware.
*/
public function call()
{
$this->app->container->singleton('modx', function() {
define('MODX_CORE_PATH', $this->app->config('modx.core_path'));
define('MODX_CONFIG_KEY', $this->app->config('modx.config_key') ?: 'config');
require MODX_CORE_PATH . 'model/modx/modx.class.php';
$modx = new \modX('', $this->app->config('modx.config') ?: []);
return $modx;
});
$this->next->call();
}
}
<?php
$startTime = microtime(true);
require __DIR__ . '/../vendor/autoload.php';
$config = [
"debug" => false,
"https.port"=> 443,
];
if (is_readable(__DIR__ . '/../config.php')) {
$loaded = include __DIR__ . '/../config.php';
if (is_array($loaded)) {
$config = array_merge($config, $loaded);
}
}
$app = new \Slim\Slim($config);
$app->add(new \MODX\Slim\Middleware\MODSlim());
$app->error(function(\Exception $e) use ($app) {
$app->getLog()->error($e->getMessage());
$app->halt(500, $e->getMessage());
$app->stop();
});
require __DIR__ . '/routes.php';
$app->run();
<?php
$app->notFound(function() use ($app) {
/** @var \modX $modx */
$modx = $app->container->get('modx');
$modx->initialize($app->config('modx.context') ?: 'web');
$_GET[$modx->getOption('request_param_alias', null, 'q')]
= $_REQUEST[$modx->getOption('request_param_alias', null, 'q')]
= ltrim($app->request->getResourceUri(), '/');
$modx->handleRequest();
});
$app->get('/', function() use ($app) {
/** @var \modX $modx */
$modx = $app->container->get('modx');
$modx->initialize($app->config('modx.context') ?: 'web');
$modx->resource = $modx->getObject('modResource', (int)$modx->getOption('site_start'));
$modx->resourceIdentifier = $modx->resource->get('id');
$modx->elementCache = array();
$resourceOutput = $modx->resource->process();
$modx->parser->processElementTags('', $resourceOutput, true, false, '[[', ']]', array(), 10);
$modx->parser->processElementTags('', $resourceOutput, true, true, '[[', ']]', array(), 10);
echo $resourceOutput;
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment