coreplex/navigator 问题修复 & 功能扩展

解决BUG、新增功能、兼容多环境部署,快速响应你的开发需求

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

coreplex/navigator

Composer 安装命令:

composer require coreplex/navigator

包简介

A framework agnostic navigation package that handles active states and permissions for navigation items

README 文档

README

A PHP 5.4+ package to create navigation menus, and handle active and permissions.

Installation

This package requires PHP 5.4+, and includes a Laravel 5 Service Provider and Facade.

We recommend installing the package through composer. You can either call composer require coreplex/navigator in your command line, or add the following to your composer.json and then run either composer install or composer update to download the package.

"coreplex/navigator": "~0.1"

Laravel 5 Integration

To use the package with Laravel 5 firstly add the service providers to the list in app/config/app.php.

'providers' => array(

  Coreplex\Core\CoreServiceProvider::class,
  Coreplex\Navigator\NavigatorServiceProvider::class,

);

Then to run php artisan vendor:publish in your command line to publish the config.

Registering Menus

To register a menu you add it into the menus array in the navigator config file. To register a menu you set a key to retrieve a the menu by as the key, and the class to build the menu as the value.

'menus' => [
  'foo' => 'Navigators\FooNavigator'
]

By default the menu class will user a method called design, but if you want to set the method name your self just separate the class and method name with an @ symbol.

'menus' => [
  'foo' => 'Navigators\FooNavigator@bar'
]

To see how to create menus check the creating menus section below.

Retrieving Menus

Once you've registered a menu to retrieve it use the get method.

$menu = $navigator->get('foo');

Creating Menus

To create a menu you need to return a nested array of menu items. Each item is made up of a url, any nested menu items, and then any attributes wish the item to have. As an example check the code below.

class Sidebar 
{
  public function design()
  {
    return [
      [
        'url' => '/',
        'title' => 'Home',
      ],
      [
        'url' => 'about',
        'title' => 'about',
        'items' => [
          [
            'url' => 'meet-the-team',
            'title' => 'Meet The Team'
          ]
        ]
      ]
    ];
  }
}

This will add two items to then menu; one for the home page and the other is the about page. The about page also has nested items so we can display them in a drop down when rendering.

The url isn't required, but is used to check if the item is the active item.

You can add any attributes you wish to the menu items. In the example a title attribute has been set, but we could also add an icon, class etc. here and then access it on the item when it is rendered.

Filters

Occasionally you may need to only show a menu item if a condition is met; to do this with use filters.

To register a filter add it to the filters array in the config file by setting a unique key as a the key and a class to use as the value.

'filters' => [
  'hasAccess' => 'Navigators\Filters\HasAccess'
]

The filter classes by default use a method called filter but again if you want to specify a method then separate the class and method with an @ symbol.

'filters' => [
  'hasAccess' => 'Navigators\Filters\HasAccess@foo'
]

To call a filter no a menu item add a filter attribute to the menu item.

[
  'url' => '/admin',
  'title' => 'admin',
  'filter' => 'hasAccess',
  'permission' => 'admin.access',
],

The method on the filter class will then be passed the item so you can do any checking you need to.

use Coreplex\Navigator\Contracts\Item;

class HasAccess 
{
  public function filter(Item $item)
  {
    if ($user->canAccess($item->permission) {
      return true;
    }
    
    return false;
  }
}

Rendering Menus

To render a menu you have a couple of options. Either you can set a template to render it to, or you can access the menus properties.

Rendering With a Template

To render with a template simply call the render method on the menu. This will either use the default view set in the config file, or you can pass the path to the template to use to the render method.

$menu = $navigator->get('foo');

$menu->render();
$menu->render('path/to/template.php');

This package comes with a default template as an example.

Rendering From the Menu

To access the items in a menu use the items method or just pass the menu through a iterator.

$items = $menu->items();

OR

foreach ($menu as $item) {
  //
}

Then once you've got a menu item you can can check if it is the active menu item by calling the isActive method.

$item->isActive();

Then you can access any properties set on the item by just accessing the key on the item object. For example if you have set a url for the item then I could do the following.

$item->url;

coreplex/navigator 适用场景与选型建议

coreplex/navigator 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 299 次下载、GitHub Stars 达 1, 最近一次更新时间为 2015 年 04 月 07 日, 在 PHP 生态内属于活跃度较高的组件。

它主要适用于以下技术方向: 「navigation」 「laravel」 等业务场景。在实际项目中,围绕这些方向常见需要落地的问题包括:接口对接、性能调优、并发安全、与既有框架(Laravel / ThinkPHP / Yii / Webman 等)的兼容适配,以及生产环境的日志埋点与稳定性保障。

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

围绕 coreplex/navigator 我们能提供哪些服务?
定制开发 / 二次开发

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

BUG 修复 & 性能优化

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

项目外包 & 长期维护

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

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

统计信息

  • 总下载量: 299
  • 月度下载量: 0
  • 日度下载量: 0
  • 收藏数: 1
  • 点击次数: 24
  • 依赖项目数: 0
  • 推荐数: 0

GitHub 信息

  • Stars: 1
  • Watchers: 3
  • Forks: 0
  • 开发语言: PHP

其他信息

  • 授权协议: MIT
  • 更新时间: 2015-04-07