Skip to content

Instantly share code, notes, and snippets.

@phroggyy
Created April 28, 2016 13:40
Show Gist options
  • Save phroggyy/7a3608bdc5264a2036679f7bc9e0f3ab to your computer and use it in GitHub Desktop.
Save phroggyy/7a3608bdc5264a2036679f7bc9e0f3ab to your computer and use it in GitHub Desktop.
A tiny service provider to allow you to register views by modules
<?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