add frontend/admin links dynamically from module service provider

This commit is contained in:
Nabeel Shahzad 2017-12-01 17:21:48 -06:00
parent 5e2bbe69dc
commit ca6c5cbc0a
5 changed files with 133 additions and 15 deletions

View File

@ -0,0 +1,69 @@
<?php
/**
* Created by IntelliJ IDEA.
* User: nshahzad
* Date: 12/1/17
* Time: 4:49 PM
*/
namespace App\Services;
class ModuleService extends BaseService
{
protected static $adminLinks = [];
/**
* @var array 0 == logged out, 1 == logged in
*/
protected static $frontendLinks = [0 => [], 1 => []];
/**
* Add a module link in the frontend
* @param string $title
* @param string $url
* @param string $icon
*/
public function addFrontendLink(string $title, string $url, string $icon = '', $logged_in=true)
{
self::$frontendLinks[$logged_in][] = [
'title' => $title,
'url' => $url,
'icon' => 'pe-7s-users',
];
}
/**
* Get all of the frontend links
* @return array
*/
public function getFrontendLinks($logged_in): array
{
return self::$frontendLinks[$logged_in];
}
/**
* Add a module link in the admin panel
* @param string $title
* @param string $url
* @param string $icon
*/
public function addAdminLink(string $title, string $url, string $icon='')
{
self::$adminLinks[] = [
'title' => $title,
'url' => $url,
'icon' => 'pe-7s-users'
];
}
/**
* Get all of the module links in the admin panel
* @return array
*/
public function getAdminLinks(): array
{
return self::$adminLinks;
}
}

View File

@ -9,24 +9,23 @@ use Route;
class SampleServiceProvider extends ServiceProvider
{
/**
* Indicates if loading of the provider is deferred.
*
* @var bool
*/
protected $defer = false;
protected $moduleSvc;
/**
* Boot the application events.
*
* @return void
*/
public function boot()
{
$this->moduleSvc = app('App\Services\ModuleService');
$this->registerRoutes();
$this->registerTranslations();
$this->registerConfig();
$this->registerViews();
$this->registerLinks();
$this->registerFactories();
$this->loadMigrationsFrom(__DIR__ . '/../Database/migrations');
}
@ -39,6 +38,18 @@ class SampleServiceProvider extends ServiceProvider
//
}
/**
* Add module links here
*/
public function registerLinks()
{
// Show this link if logged in
$this->moduleSvc->addFrontendLink('Sample', '/sample', '', $logged_in=true);
// Admin links:
$this->moduleSvc->addAdminLink('Sample', '/sample/admin');
}
/**
* Register the routes
*/

View File

@ -9,24 +9,23 @@ use Route;
class $CLASS$ extends ServiceProvider
{
/**
* Indicates if loading of the provider is deferred.
*
* @var bool
*/
protected $defer = false;
protected $moduleSvc;
/**
* Boot the application events.
*
* @return void
*/
public function boot()
{
$this->moduleSvc = app('App\Services\ModuleService');
$this->registerRoutes();
$this->registerTranslations();
$this->registerConfig();
$this->registerViews();
$this->registerLinks();
$this->registerFactories();
$this->loadMigrationsFrom(__DIR__ . '/../$MIGRATIONS_PATH$');
}
@ -39,6 +38,18 @@ class $CLASS$ extends ServiceProvider
//
}
/**
* Add module links here
*/
public function registerLinks()
{
// Show this link if logged in
$this->moduleSvc->addFrontendLink('$STUDLY_NAME$', '/$LOWER_NAME$', '', $logged_in=true);
// Admin links:
$this->moduleSvc->addAdminLink('$STUDLY_NAME$', '/$LOWER_NAME$/admin');
}
/**
* Register the routes
*/

View File

@ -46,7 +46,10 @@
<div class="collapse" id="addons_menu" aria-expanded="true">
<ul class="nav">
@stack('admin.addons.links')
@inject('moduleSvc', 'App\Services\ModuleService')
@foreach($moduleSvc->getAdminLinks() as &$link)
<li><a href="{!! url($link['url']) !!}"><i class="{!! $link['icon'] !!}"></i>{!! $link['title'] !!}</a></li>
@endforeach
</ul>
</div>
</li>

View File

@ -1,3 +1,4 @@
@inject('moduleSvc', 'App\Services\ModuleService')
<!DOCTYPE html>
<html lang="en">
@ -58,7 +59,19 @@
<p>Register</p>
</a>
</li>
{{-- Show the module links for being logged out --}}
@foreach($moduleSvc->getFrontendLinks($logged_in=false) as &$link)
<li class="nav-item">
<a class="nav-link" href="{!! url($link['url']) !!}">
<i class="{!! $link['icon'] !!}"></i>
<p>{!! $link['title'] !!}</p>
</a>
</li>
@endforeach
@else
<li class="nav-item">
<a class="nav-link" href="{!! url('/dashboard') !!}">
<i class="fa fa-tachometer" aria-hidden="true"></i>
@ -91,6 +104,17 @@
</a>
</li>
@endif
{{-- Show the module links for being logged out --}}
@foreach($moduleSvc->getFrontendLinks($logged_in=true) as &$link)
<li class="nav-item">
<a class="nav-link" href="{!! url($link['url']) !!}">
<i class="{!! $link['icon'] !!}"></i>
<p>{!! $link['title'] !!}</p>
</a>
</li>
@endforeach
<li class="nav-item">
<a class="nav-link" href="{!! url('/logout') !!}">
<i class="fa fa-external-link-square" aria-hidden="true"></i>