Introduce new Cms helper

This is modeled after the Backend helper. Primarily used to generate URLs for the frontend, these are piped through the CmsController action. It would also be a good place to add a hook, if necessary later.
This commit is contained in:
Samuel Georges 2016-08-20 14:43:49 +10:00
parent 3698f13acb
commit c8d8d4e931
7 changed files with 65 additions and 17 deletions

View File

@ -7,9 +7,7 @@ class Backend extends Facade
/** /**
* Get the registered name of the component. * Get the registered name of the component.
* *
* Resolves to: * @see \Backend\Helpers\Backend
* - Backend\Helpers\Backend
*
* @return string * @return string
*/ */
protected static function getFacadeAccessor() protected static function getFacadeAccessor()

View File

@ -13,6 +13,7 @@ use Backend\Classes\Skin;
* Backend Helper * Backend Helper
* *
* @package october\backend * @package october\backend
* @see \Backend\Facades\Backend
* @author Alexey Bobkov, Samuel Georges * @author Alexey Bobkov, Samuel Georges
*/ */
class Backend class Backend

View File

@ -1,12 +1,12 @@
<?php namespace Cms\Classes; <?php namespace Cms\Classes;
use Cms;
use Url; use Url;
use Str; use Str;
use App; use App;
use File; use File;
use View; use View;
use Lang; use Lang;
use Route;
use Event; use Event;
use Config; use Config;
use Session; use Session;
@ -1124,19 +1124,7 @@ class Controller
return null; return null;
} }
if (substr($url, 0, 1) == '/') { return Cms::url($url);
$url = substr($url, 1);
}
$routeAction = 'Cms\Classes\CmsController@run';
$actionExists = Route::getRoutes()->getByAction($routeAction) !== null;
if ($actionExists) {
return Url::action($routeAction, ['slug' => $url]);
}
else {
return Url::to($url);
}
} }
/** /**

View File

@ -0,0 +1,17 @@
<?php namespace Cms\Facades;
use October\Rain\Support\Facade;
class Cms extends Facade
{
/**
* Get the registered name of the component.
*
* @see \Cms\Helpers\Cms
* @return string
*/
protected static function getFacadeAccessor()
{
return 'cms.helper';
}
}

View File

@ -0,0 +1,39 @@
<?php namespace Cms\Helpers;
use Url;
use Route;
/**
* CMS Helper
*
* @package october\cms
* @see \Cms\Facades\Cms
* @author Alexey Bobkov, Samuel Georges
*/
class Cms
{
protected static $actionExists = null;
/**
* Returns a URL in context of the Frontend
*/
public function url($path = null)
{
$routeAction = 'Cms\Classes\CmsController@run';
if (self::$actionExists === null) {
self::$actionExists = Route::getRoutes()->getByAction($routeAction) !== null;
}
if (substr($path, 0, 1) == '/') {
$path = substr($path, 1);
}
if (self::$actionExists) {
return Url::action($routeAction, ['slug' => $path]);
}
else {
return Url::to($path);
}
}
}

View File

@ -94,6 +94,10 @@ class ServiceProvider extends ModuleServiceProvider
*/ */
protected function registerSingletons() protected function registerSingletons()
{ {
App::singleton('cms.helper', function () {
return new \Cms\Helpers\Cms;
});
App::singleton('backend.helper', function () { App::singleton('backend.helper', function () {
return new \Backend\Helpers\Backend; return new \Backend\Helpers\Backend;
}); });

View File

@ -52,6 +52,7 @@ return [
'Twig' => October\Rain\Support\Facades\Twig::class, 'Twig' => October\Rain\Support\Facades\Twig::class,
'DbDongle' => October\Rain\Support\Facades\DbDongle::class, 'DbDongle' => October\Rain\Support\Facades\DbDongle::class,
'Schema' => October\Rain\Support\Facades\Schema::class, 'Schema' => October\Rain\Support\Facades\Schema::class,
'Cms' => Cms\Facades\Cms::class,
'Backend' => Backend\Facades\Backend::class, 'Backend' => Backend\Facades\Backend::class,
'BackendMenu' => Backend\Facades\BackendMenu::class, 'BackendMenu' => Backend\Facades\BackendMenu::class,
'BackendAuth' => Backend\Facades\BackendAuth::class, 'BackendAuth' => Backend\Facades\BackendAuth::class,