I have been working on some projects for some customers that require MODX to be integrated with custom functionality (e.g. forms, database calls, map APIs, etc). I really like the security and validation functionality that comes with an MVC like CodeIgniter.
I decided to run two index.php pages side by side in the same root folder ("www"). One called index.php for MODX and one called index_codeigniter.php for CodeIgniter. Then I used a .htaccess to route GET and POST messages to the appropriate index page. CodeIgniter lets you move the application and system folders to a different path to avoid collision with MODX folders. I moved these to a parent folder of www. I am using CodeIgniter version 2.1.0 and MODX Revolution version 2.2.0-pl2
At first, I was somewhat successful at reading a template within index_codeigniter.php using the following code:
$modx->getObject('modTemplate',array('templatename'=>'Basic2fl'));
Then I was able to break the HTML into two parts, the part before "[ [*content] ]" and the part after "[ [*content] ]" and insert it into the CodeIgniter response within the controller class. The only problem with this approach is that the getObject method does not parse the snippets (e.g. Wayfinder). This led me to another approach that was much more complex, which was to retrieve a parsed resource page and use that HTML as a template. I'm posting this solution in hopes that it may be helpful to others. Also, this might encourage someone else to come up with a better solution.
index_codeigniter.php (modified copy of CodeIgniter index.php)
<?php
define('RESOURCE_ID_AS_TEMPLATE', '7'); // specify the resourceIdentifier of a document to use as the template
require_once('modx_apimode_include.php');
// create header and footer from resource HTML
$templateHtmlSegs = explode('{{Content}}',$templateHtml);
$templateHtmlHeader = '';
$templateHtmlFooter = '';
if (count($templateHtmlSegs) > 0) {
$templateHtmlHeader = $templateHtmlSegs[0];
}
if (count($templateHtmlSegs) > 1) {
$templateHtmlFooter = $templateHtmlSegs[1];
}
...
// within the "CUSTOM CONFIG VALUES" section
$assign_to_config['modxTemplateHtmlHeader'] = $templateHtmlHeader;
$assign_to_config['modxTemplateHtmlFooter'] = $templateHtmlFooter;
index_apimode_include.php - this is a new file I wrote from scratch
<?php
$mtime= microtime();
$mtime= explode(" ", $mtime);
$mtime= $mtime[1] + $mtime[0];
$tstart= $mtime;
/* this can be used to disable caching in MODX absolutely */
$modx_cache_disabled= false;
require_once(dirname(__FILE__) . '/config.core.php');
define('MODX_API_MODE', true);
require_once(MODX_CORE_PATH . 'model/modx/modx.class.php');
$modx= new modX();
$modx->startTime= $tstart;
$modx->initialize('web');
// The following lines are a slimmed down version of $modx->handleRequest where the
// resourceIdentifier can be set programmatically instead of being pulled from the URL.
// This is useful if you want to use a given page as a template. Retrieving a raw template
// in this case will not work because the snippets (e.g. Wayfinder) within the template
// are never parsed.
$modx->getRequest();
$modx->request->loadErrorHandler();
$modx->request->sanitizeRequest();
$modx->invokeEvent('OnHandleRequest');
$modx->beforeRequest();
$modx->invokeEvent("OnWebPageInit");
$modx->resource = $modx->request->getResource('id', RESOURCE_ID_AS_TEMPLATE);
$modx->request->prepareResponse();
$templateHtml = $modx->resource->_output;
code to add to your CodeIgniter controller
$templateHtmlHeader = $this->config->item('modxTemplateHtmlHeader');
$templateHtmlFooter = $this->config->item('modxTemplateHtmlFooter');
// instead of including the CI header and footer template, we are including
// the modx header and footer
$this->output->append_output($templateHtmlHeader);
$this->load->view($viewPath, $data);
$this->output->append_output($templateHtmlFooter);
There is one last hack you need to code in the /core/model/modresponse.class.php. This is because MODX seems to ignore the MODX_API_MODE constant. The very end of the outputContent method needs to be modified to include the following if statement (starting with line 183):
} else {
if ($this->contentType->get('binary')) {
$this->modx->resource->_output = $this->modx->resource->process();
}
@session_write_close();
// This conditional added to facilitate API mode
if (!MODX_API_MODE) {
echo $this->modx->resource->_output;
while (@ob_end_flush()) {}
flush();
exit();
}
}
}
It would be nice if MODX provided a way to parse a template so that the snippets processed. That would be the ultimate solution. But for now, this is the work around that I came up with.
[ed. note: davedev last edited this post 14 years, 3 months ago.]