Created
April 28, 2016 13:40
-
-
Save phroggyy/7a3608bdc5264a2036679f7bc9e0f3ab to your computer and use it in GitHub Desktop.
A tiny service provider to allow you to register views by modules
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
namespace App\Providers; | |
use Illuminate\Support\ServiceProvider; | |
class ViewServiceProvider extends ServiceProvider | |
{ | |
public function register() | |
{ | |
$this->registerViews('module', ['section']); | |
// if you just have one section, you can also do | |
// $this->registerView('module', 'section'); | |
} | |
private function registerViews($moduleName, $accessLevels, $namespace = null) | |
{ | |
if (! is_array($accessLevels)) { | |
$accessLevels = [$accessLevels]; | |
} | |
foreach ($accessLevels as $accessLevel) { | |
$this->registerView($moduleName, $accessLevel, $namespace); | |
} | |
} | |
private function registerView($moduleName, $accessLevel, $namespace = null) | |
{ | |
$namespaceAccessLevel = $accessLevel; | |
// If the user passes in "public" or "publik", the directory should be "publik", but the namespace "public" | |
if ($accessLevel == 'public') { | |
$accessLevel = 'publik'; | |
} | |
if ($namespaceAccessLevel == 'publik') { | |
$namespaceAccessLevel = 'public'; | |
} | |
$namespace = $namespace ?: "{$moduleName}.{$namespaceAccessLevel}"; | |
$baseDirectory = __DIR__.'/../Modules/'; | |
$this->loadViewsFrom($baseDirectory.ucfirst($moduleName).'/'.ucfirst($accessLevel).'/Views', $namespace); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment