/前台

v1.0.0 2023-10-24 19:30 UTC

README

PHP Version Support GitHub Workflow Status

前台

前台简化了在Laravel应用程序中使用模型构建导航栏的方式。前台将导航菜单视为任何其他模型,因此您可以完全动态地控制菜单内容。

安装

您可以通过composer安装此包

composer require plank/frontdesk

用法

前台将导航栏的概念分为两部分:菜单和超链接。菜单是一组超链接。每个超链接都可以有一个父级超链接和一组子级超链接。

因此,您可能有一些“可链接”内容和一些“可菜单”内容。

要使用前台,只需在您的模型上添加特性和实现相应的接口。

可链接

class MyModel extends Model implements Linkable
{
    use IsLinkable;
    
    public function linkTitle(): Attribute
    {
        return Attribute::make(
            get: fn () => $this->title
        );
    }
    
    public function linkUrl(): Attribute
    {
        return Attribute::make(
            get: fn () => route('my-model.show', $this)
        );
    }
}

可菜单

class MyMenuModel extends Model implements Menuable 
{
    use HasMenus;
}

保存菜单和链接

一旦您有了一些实现适当接口的模型,您就可以开始构建您的导航栏。

// Create a menu
$myMenu = MyMenuModel::find(1)->menus()->create([
    'identifier' => 'header-nav'
]); 
$myOtherMenu = MyMenuModel::find(1)->menus()->create([
    'identifier' => 'footer-nav'
]);

// Create a hyperlink referencing 
$myModelLink = MyModel::find(1)->hyperlinks()->create([
    'menu_id' => $myMenu->id,
]);

// A link also doesn't strictly need to be attached to a model
$myMenuLink = Hyperlink::create([
    'menu_id' => $myMenu->id,
    'title' => 'My Link',
    'url' => 'https://example.com',
]);

// You can also associate an existing hyperlink to an existing menu
$myMenuLink->menus()->associate($myOtherMenu)->save();

获取菜单和链接

构建了一些菜单之后,您可以使用菜单模型或通过模型与菜单模型的关联来检索它们。

// Get a menu by identifier
$myMenu = Menu::where('identifier', 'header-nav')->first();

// Via a model relationship
$myMenu = MyMenuModel::find(1)->menus()->where('identifier', 'header-nav')->first();

从菜单中获取链接就像在菜单模型上调用hyperlinks关系一样简单。

$myMenu->hyperlinks;

测试

composer test

更新日志

请参阅更新日志以获取有关最近更改的更多信息。

贡献

请参阅贡献指南以获取详细信息。

安全

如果您发现任何与安全相关的问题,请通过电子邮件security@plankdesign.com而不是使用问题跟踪器。

鸣谢

许可证

MIT许可证(MIT)。有关更多信息,请参阅许可证文件