如何将数据从 ToolServiceProvider 传递到 Laravel Nova 中的导航 blade?

How to pass data from ToolServiceProvider to navigation blade in Laravel Nova?

Laravel Nova Tools 扩展了 abstract ServiceProvider,它只提供了 $this->loadViewFrom( $path, $namespace ) 的方法,但没有提供数据字段来向 navigation.blade.php 提供任何自定义数据.

我需要一些自定义 Root-Level 链接,这些链接只能作为默认资源 link - 所以不能作为默认存储它们的资源中的子菜单。

我尝试添加一个工具,然后简单地添加在其构造函数中引用的资源,并且还允许使用 ->addChild() 方法向其添加 children。我最初的想法是滥用配置并在运行时更新它,但很快意识到这行不通,因为应用程序启动多个路由并且只有最后一个自定义 link 会在配置中。此外,我将无法确定在导航中的哪个位置显示哪个 link,因为配置将包含所有 CustomLinks。

这是我现在拥有的

工具

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');
    }
}

ToolServiceProvider 只加载视图,不提供任何选项

class ToolServiceProvider extends ServiceProvider
{
    /**
     * Bootstrap any application services.
     *
     * @return void
     */
    public function boot()
    {
        $this->loadViewsFrom(__DIR__ . '/../resources/views', 'custom-link');
    }
}

关于如何将自定义数据放入 blade 或如何实现自定义 top-level 导航 link 的更好想法的任何建议?

非常感谢!

你可以尝试使用 View::share() view share doc

示例:

  View::share('key', 'value');

如果你想要一种更高级的方法来仅将变量共享到特定视图,你可以使用 view composers view composers doc

简单示例:

View::composer('view-name', function ($view) {
             $view->with('key', 'value');
        });