Add the ability to include middleware in CMS / Backend controllers

Add init.php script to plugins
Fixes https://github.com/octobercms/library/issues/106
This commit is contained in:
Samuel Georges 2015-04-21 20:07:34 +10:00
parent b1d1d9dae1
commit 1a037bad04
6 changed files with 86 additions and 50 deletions

View File

@ -6,6 +6,7 @@ use File;
use Config;
use Illuminate\Routing\Controller as ControllerBase;
use October\Rain\Router\Helper as RouterHelper;
use Closure;
/**
* The Backend controller class.
@ -16,6 +17,13 @@ use October\Rain\Router\Helper as RouterHelper;
*/
class BackendController extends ControllerBase
{
use \October\Rain\Extension\ExtendableTrait;
/**
* @var array Behaviors implemented by this controller.
*/
public $implement;
/**
* @var string Allows early access to page action.
*/
@ -26,6 +34,22 @@ class BackendController extends ControllerBase
*/
public static $params;
/**
* Instantiate a new BackendController instance.
*/
public function __construct()
{
$this->extendableConstruct();
}
/**
* Extend this object properties upon construction.
*/
public static function extend(Closure $callback)
{
self::extendableExtendCallback($callback);
}
/**
* Finds and serves the requested backend controller.
* If the controller cannot be found, returns the Cms page with the URL /404.

View File

@ -0,0 +1,49 @@
<?php namespace Cms\Classes;
use App;
use Illuminate\Routing\Controller as ControllerBase;
use Closure;
/**
* The CMS controller class.
* The base controller services front end pages.
*
* @package october\cms
* @author Alexey Bobkov, Samuel Georges
*/
class CmsController extends ControllerBase
{
use \October\Rain\Extension\ExtendableTrait;
/**
* @var array Behaviors implemented by this controller.
*/
public $implement;
/**
* Instantiate a new CmsController instance.
*/
public function __construct()
{
$this->extendableConstruct();
}
/**
* Extend this object properties upon construction.
*/
public static function extend(Closure $callback)
{
self::extendableExtendCallback($callback);
}
/**
* Finds and serves the request using the primary controller.
* @param string $url Specifies the requested page URL.
* If the parameter is omitted, the current URL used.
* @return string Returns the processed page content.
*/
public function run($url = null)
{
return App::make('Cms\Classes\Controller')->run($url);
}
}

View File

@ -1052,7 +1052,7 @@ class Controller
$url = substr($url, 1);
}
$routeAction = 'Cms\Classes\Controller@run';
$routeAction = 'Cms\Classes\CmsController@run';
$actionExists = Route::getRoutes()->getByAction($routeAction) !== null;
if ($actionExists) {
@ -1264,50 +1264,4 @@ class Controller
}
}
}
//
// Keep Laravel Happy
//
/**
* Get the middleware assigned to the controller.
*
* @return array
*/
public function getMiddleware()
{
return [];
}
/**
* Get the registered "before" filters.
*
* @return array
*/
public function getBeforeFilters()
{
return [];
}
/**
* Get the registered "after" filters.
*
* @return array
*/
public function getAfterFilters()
{
return [];
}
/**
* Execute an action on the controller.
*
* @param string $method
* @param array $parameters
* @return \Symfony\Component\HttpFoundation\Response
*/
public function callAction($method, $parameters)
{
return call_user_func_array(array($this, $method), $parameters);
}
}

View File

@ -8,5 +8,5 @@ App::before(function ($request) {
* The CMS module intercepts all URLs that were not
* handled by the back-end modules.
*/
Route::any('{slug}', 'Cms\Classes\Controller@run')->where('slug', '(.*)?');
Route::any('{slug}', 'Cms\Classes\CmsController@run')->where('slug', '(.*)?');
});

View File

@ -345,16 +345,17 @@ class ServiceProvider extends ModuleServiceProvider
*/
protected function registerPrivilegedActions()
{
$requests = ['/combine', '@/system/updates', '@/backend/auth'];
$requests = ['/combine', '@/system/updates', '@/system/install', '@/backend/auth'];
$commands = ['october:up', 'october:update'];
/*
* Requests
*/
$path = RouterHelper::normalizeUrl(Request::path());
$backendUri = RouterHelper::normalizeUrl(Config::get('cms.backendUri'));
foreach ($requests as $request) {
if (substr($request, 0, 1) == '@') {
$request = Config::get('cms.backendUri') . substr($request, 1);
$request = $backendUri . substr($request, 1);
}
if (stripos($path, $request) === 0) {

View File

@ -210,6 +210,14 @@ class PluginManager
View::addNamespace($pluginNamespace, $viewsPath);
}
/*
* Add init, if available
*/
$initFile = $pluginPath . '/init.php';
if (!self::$noInit && File::exists($initFile)) {
require $initFile;
}
/*
* Add routes, if available
*/