Navigation Menu

Skip to content

Instantly share code, notes, and snippets.

@mylgeorge
Last active August 18, 2017 13:40
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save mylgeorge/2a8ce9dd47650b3e756289209d717a63 to your computer and use it in GitHub Desktop.
Save mylgeorge/2a8ce9dd47650b3e756289209d717a63 to your computer and use it in GitHub Desktop.
Integrate modX in Laravel
<?php
namespace App\Http\Controllers;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Log;
use Illuminate\Support\Facades\Input;
use App\ModxModels\ModxContentModel;
use App\Providers\ModxServiceProvider;
class ModxController extends Controller
{
protected $modx;
public function __construct( \modX $modx )
{
$this->modx = $modx;
if(!$this->modx->getRequest()) return redirect('/');
$this->modx->request->sanitizeRequest();
}
public function index($param = '')
{
if(!$param) {
$resourceId= $this->modx->getOption('site_start');
}
else {
$resourceId= $this->modx->findResource($param);
}
if (is_numeric($resourceId)) {
$resource= $this->modx->request->getResource('id',$resourceId);
}
else {
return redirect('/');
}
$this->modx->resource = $resource;
$this->modx->resourceIdentifier = $resource->get('id');
$this->modx->elementCache = array();
$html= $resource->getContent(); //or ->process();
if (!$this->modx->parser) {
$this->modx->getParser();
}
$maxIterations= intval($this->modx->getOption('parser_max_iterations', null, 10));
// Process the non-cacheable content of the Resource, but leave any unprocessed tags alone
$this->modx->parser->processElementTags('', $html, true, false, '[[', ']]', array(), $maxIterations);
// Process the non-cacheable content of the Resource, this time removing the unprocessed tags
$this->modx->parser->processElementTags('', $html, true, true, '[[', ']]', array(), $maxIterations);
return view('modx',array(
'pagetitle' => $this->modx->resource->get('pagetitle'),
'content' => $html,
));
}
public function login($param = ''){
$this->modx->runSnippet('Login');
$user= $this->modx->getUser('web');
if($user->isAuthenticated('web') || Input::get('service') === 'logout') {
return redirect()->back();
}
else {
return redirect()->back()->withInput(Input::except('password'))->withErrors(array('msg' => $this->modx->getPlaceholder('errors')));
}
}
}
<?php
namespace App\Providers;
use Illuminate\Support\ServiceProvider;
require_once dirname(dirname(__DIR__)) . '/public/config.core.php';
require_once MODX_CORE_PATH . 'model/modx/modx.class.php';
class ModxServiceProvider extends ServiceProvider
{
public $defer = false;
/**
* Bootstrap the application services.
*
* @return void
*/
public function boot(\modX $singleton)
{
global $modx;
$singleton->initialize('web');
$modx= $singleton;
view()->composer('*', function($view) use ($singleton) {
$view->with('modx',$singleton);
});
}
/**
* Register the application services.
*
* @return void
*/
public function register()
{
$this->app->singleton(\modX::class, function ($app) {
return new \modX();
});
}
}
/*
|--------------------------------------------------------------------------
| Web Routes
|--------------------------------------------------------------------------
|
| Here is where you can register web routes for your application. These
| routes are loaded by the RouteServiceProvider within a group which
| contains the "web" middleware group. Now create something great!
|
*/
Route::any('/login', 'ModxController@login');
Route::any('/{alias?}', 'ModxController@index');
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment