Reputation: 1391
Laravel Nova Tools extend the abstract ServiceProvider
which only offers a method to $this->loadViewFrom( $path, $namespace )
, but there is no data field offered to provide any custom data to the navigation.blade.php
.
I need some custom Root-Level Links that should only act as a default resource link - so not as submenu in resources where they are are stored by default.
I tried to add a Tool and then simply add the resource that is referenced in it's constructor, and also allowing adding children to it using a ->addChild()
method. My initial idea was to misuse the config and update it on runtime, but quickly realized that this won't work, as the app boots for several routes and only the very last custom link will be in the config. Also, I won't be able to figure out which link to display in which position in the Navigation, as config would contain all CustomLinks.
Here is what I have now
The Tool
class CustomLink extends Tool
{
protected $name;
protected $uri;
protected $resource;
protected $icon;
/**
* CustomLink constructor.
*
* @param null $resource
* @param null $name
* @param null $uri
* @param null $icon
* @throws \Exception
*/
public function __construct($resource = null, $name = null, $uri = null, $icon = null)
{
if (empty($resource) && empty($uri)) {
throw new \Exception('Missing Resource or URI');
}
// Get current config
$conf = config('bty.custom_links');
// Add new element to config
$conf[$this->getId()] = array(
'name' => empty($name) ? $resource::label() : $name,
'uri' => $uri,
'icon' => $icon,
'children' => [],
);
// Store Config
config(['bty.custom_links' => $conf]);
parent::__construct();
}
/**
* Add a SubNav Element
*
* @param null $resource
* @param null $name
* @param null $uri
* @return $this
*/
public function addChild($resource = null, $name = null, $uri = null)
{
if (empty($resource) && empty($uri)) {
return $this;
}
// Find parent and append child
$conf = config('bty.custom_links');
$conf[$this->getId()]['children'][] =
array(
'name' => empty($name) ? $resource::label() : $name,
'uri' => $uri,
'resource' => $resource,
);
// Save
config(['bty.custom_links' => $conf]);
return $this;
}
/**
* Generate an id as hash from own properties
*
* @return string
*/
public function getId()
{
return md5($this->name . $this->uri . $this->icon . $this->resource);
}
/**
* Build the view that renders the navigation links for the tool.
*
* @return \Illuminate\View\View
*/
public function renderNavigation()
{
return view('custom-link::navigation');
}
}
The ToolServiceProvider only loads the view with no options offered
class ToolServiceProvider extends ServiceProvider
{
/**
* Bootstrap any application services.
*
* @return void
*/
public function boot()
{
$this->loadViewsFrom(__DIR__ . '/../resources/views', 'custom-link');
}
}
Any suggestions on how I could get custom data into the blade or a better idea how to achieve custom top-level navigation links?
Thanks a lot!
Upvotes: 1
Views: 1325
Reputation: 724
you could try using View::share()
view share doc
example:
View::share('key', 'value');
if you want a more advanced way to share variable only to specific view you can use view composers
view composers doc
simple example:
View::composer('view-name', function ($view) {
$view->with('key', 'value');
});
Upvotes: 1