承接 martinbean/laravel-menu-builder 相关项目开发

从需求分析到上线部署,全程专人跟进,保证项目质量与交付效率

邮箱:yvsm@zunyunkeji.com | QQ:316430983 | 微信:yvsm316

martinbean/laravel-menu-builder

Composer 安装命令:

composer require martinbean/laravel-menu-builder

包简介

Menu builder package for Laravel 5.x.

README 文档

README

A simple menu builder package for Laravel 5.x

Installation

Composer

This package can be installed via Composer. Run the following from the command line:

$ composer require martinbean/laravel-menu-builder

Usage

The menu builder consists of two models: Menu and MenuItem. A Menu has many MenuItem instances, and a MenuItem morphs itself to the models in your application. Therefore, you need to do some linking up to get the menu builder working.

Say you have a Page model. You will need to implement the Navigatable interface like so:

<?php namespace App;

use Illuminate\Database\Eloquent\Model;
use MartinBean\MenuBuilder\Contracts\Navigatable as NavigatableContract;

class Page extends Model implements NavigatableContract
{
    protected $table = 'pages';
}

Adding the interface means you then need to implement two methods on your model class. These methods are getTitle() and getUrl().

In your theoretical Page model, this can be implemented simply like this:

<?php namespace App;

use Illuminate\Database\Eloquent\Model;
use MartinBean\MenuBuilder\Contracts\Navigatable as NavigatableContract;

class Page extends Model implements NavigatableContract
{
    protected $table = 'pages';

    public function getTitle()
    {
        return $this->title;
    }

    public function getUrl()
    {
        return route('page.show', [$this->slug]);
    }
}

For getTitle() we’re simply returning the value of the model’s title attribute; and for getUrl() we return a route using Laravel’s route() helper.

The interface means you can make any of your existing models into a menu item, so long as they expose a title and a URL. This makes the menu builder flexible, and akin to the one in WordPress (which inspired this package) where menu items can be a post, page, category etc.

Rendering menus

Rendering of menus are done via presenter classes. This means you can change how menus are rendered by creating new presenters.

Out of the box, the package comes with a UnorderedListPresenter class. As the name suggests, this renders the menu in a plain ol’ HTML unordered list (<ul>) element.

You can create your own presenters: all you need to do is implement the Presenter contract, which will enforce you to implement three methods:

  • render()
  • hasItems()
  • getItems()

Your presenter should also have a constructor that takes an Menu instance as a parameter.

Take a look at the UnorderedListPresenter class source for an example of how to implement these methods.

Displaying menus in your templates

The Menu model is added to Laravel’s container and exposed via a facade, making it available in your templates. To render a menu, you can simply call:

{!! Menu::build(1) !!}

Where the first parameter is the ID (primary key value) of the menu you wish to render. There is also an optional second parameter which is the presenter class you wish to use if you don’t want to use the default UnorderedListPresenter class.

CRUD

You’ll also need to implement your own CRUD routines for building menus.

Issues

If you have any problems with this package, create a new Issue.

License

Licensed under the MIT License.

martinbean/laravel-menu-builder 适用场景与选型建议

martinbean/laravel-menu-builder 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 448 次下载、GitHub Stars 达 9, 最近一次更新时间为 2014 年 10 月 30 日, 在 PHP 生态内属于活跃度较高的组件。

我们在过去多个企业项目中使用过 martinbean/laravel-menu-builder 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。

围绕 martinbean/laravel-menu-builder 我们能提供哪些服务?
定制开发 / 二次开发

基于 martinbean/laravel-menu-builder 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。

BUG 修复 & 性能优化

线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。

项目外包 & 长期维护

承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。

yvsm@zunyunkeji.com QQ:316430983 微信:yvsm316 西安尊云信息科技 · 专注 PHP / Go / 分布式系统研发

统计信息

  • 总下载量: 448
  • 月度下载量: 0
  • 日度下载量: 0
  • 收藏数: 9
  • 点击次数: 20
  • 依赖项目数: 0
  • 推荐数: 0

GitHub 信息

  • Stars: 9
  • Watchers: 2
  • Forks: 3
  • 开发语言: PHP

其他信息

  • 授权协议: MIT
  • 更新时间: 2014-10-30