yidas/yii2-nav-locator
Composer 安装命令:
composer require yidas/yii2-nav-locator
包简介
Yii 2 Navigation Routing Locator for active menu identification
README 文档
README
Yii 2 Navigation Locator
Yii 2 Navigation Routing Locator for active menu identification
FEATURES
-
Smartly identifying active navigation on current controller action
-
Multi-validators for grouping active navigation
-
Route prefix setting support
OUTLINE
DEMONSTRATION
Giving a 3-layer controller action route structure for Yii 2 framework:
yii2/
├── controllers/
├── data/
├── ListController.php
└── StructureSettingController.php
├── datacenters/
├── ClusterSettingController.php
└── ListController.php
└── SiteController.php
In the view of global navigation menu, write active conditions by Nav Locator:
<?php use yidas\NavLocator as Locator; use yii\helpers\Url; ?> <li class="treeview <?php if(Locator::in('data/')):?>active menu-open<?php endif ?>"> <a href="#"> <i class="fa fa-database"></i> <span>Data</span> <span class="pull-right-container"> <i class="fa fa-angle-left pull-right"></i> </span> </a> <ul class="treeview-menu"> <li class="<?php if(Locator::in('data/list')):?>active<?php endif ?>"><a href="<?=Url::to(['data/list'])?>"><i class="fa fa-circle-o"></i> Data List </a></li> <li class="<?php if(Locator::in('data/structure-setting')):?>active<?php endif ?>"><a href="<?=Url::to(['data/structure-setting'])?>"><i class="fa fa-circle-o"></i> Structure Setting </a></li> </ul> </li> <li class="treeview <?php if(Locator::in('datacenters/')):?>active menu-open<?php endif ?>"> <a href="#"> <i class="fa fa-server"></i> <span>Data Centers</span> <span class="pull-right-container"> <i class="fa fa-angle-left pull-right"></i> </span> </a> <ul class="treeview-menu"> <li class="<?php if(Locator::in('datacenters/list/')):?>active<?php endif ?>"><a href="<?=Url::to(['datacenters/list'])?>"><i class="fa fa-circle-o"></i> Node List </a></li> <li class="<?php if(Locator::in('datacenters/cluster-setting/')):?>active<?php endif ?>"><a href="<?=Url::to(['datacenters/cluster-setting'])?>"><i class="fa fa-circle-o"></i> Cluster Setting </a></li> </ul> </li>
- Example 1 active URI:
data/list,data/list/action - Example 2 active URI:
data/structure-setting,data/structure-setting/action - Example 3 active URI:
datacenters/list,datacenters/list/action - Example 4 active URI:
datacenters/cluster-setting,datacenters/cluster-setting/action
Nav Locator even supports route rule mapping. If you have a rule 'test' => 'data/list', the Nav Locator could identify as in data/list when you enter with test route.
REQUIREMENTS
This library requires the following:
- PHP 5.4.0+
- Yii 2.0.0+
INSTALLATION
Install via Composer in your Yii2 project:
composer require yidas/yii2-nav-locator
USAGE
is()
Validate current controller action is completely matched giving route
public static boolean is(string $route)
Example:
Suppose site/index as the current controller action:
use yidas\NavLocator as Locator; Locator::is('site'); // False (Route `site` could not refer to a actual action) Locator::is('site/'); // False (There is no difference between using a slash or not) Locator::is('site/index'); // True (Successfully match the same controller ID and same action ID) Locator::is('site/index/'); // True Locator::is('site/other'); // False (Failed to match the same controller ID but the different action ID) Locator::is('site/other/'); // False
The giving route need to be defined precisely, the format is
module-ID/controller-ID/action-ID.
in()
Validate current controller action is under giving route
public static boolean in(string $route)
Example:
Suppose site/index as the current controller action:
use yidas\NavLocator as Locator; Locator::in('site'); // True (Current route `site/index` is indeed in `site` layer) Locator::in('site/'); // True Locator::in('site/index'); // True (Current route `site/index` is indeed the `site/index` layers) Locator::in('site/index/'); // True Locator::in('site/other'); // False (Current route `site/index` is not in `site/other` layers) Locator::in('site/other/'); // False Locator::in('si'); // False (Current route `site/index` is not in `si` layer, `site` != `si`) Locator::in('si/'); // False Locator::in('site/index/index');// False (This route means `site` module with `index` controller and `index` action)
The giving route will divide into independent and precise route layers by each separator, letting you distinguish whether the current controller action belongs to the parent navigation.
setPrefix()
Set prefix route for simplifying declaring next locator routes
public static self setPrefix(string $prefix)
Example:
<?php use yidas\NavLocator as Locator; ?> <li class="treeview <?php if(Locator::setPrefix('data/')->in('/')):?>active menu-open<?php endif ?>"> <a href="#"> <i class="fa fa-database"></i> <span>Data</span> <span class="pull-right-container"> <i class="fa fa-angle-left pull-right"></i> </span> </a> <ul class="treeview-menu"> <li class="<?php if(Locator::in('list/')):?>active<?php endif ?>"><a href="<?=Url::to(['data/list'])?>"><i class="fa fa-circle-o"></i> Data List </a></li> <li class="<?php if(Locator::in('structure-setting/')):?>active<?php endif ?>"><a href="<?=Url::to(['data/structure-setting'])?>"><i class="fa fa-circle-o"></i> Structure Setting </a></li> </ul> </li> <li class="treeview <?php if(Locator::setPrefix('datacenters/')->in('/')):?>active menu-open<?php endif ?>"> <a href="#"> <i class="fa fa-server"></i> <span>Data Centers</span> <span class="pull-right-container"> <i class="fa fa-angle-left pull-right"></i> </span> </a> <ul class="treeview-menu"> <li class="<?php if(Locator::in('list/')):?>active<?php endif ?>"><a href="<?=Url::to(['datacenters/list'])?>"><i class="fa fa-circle-o"></i> Node List </a></li> <li class="<?php if(Locator::in('cluster-setting/')):?>active<?php endif ?>"><a href="<?=Url::to(['datacenters/cluster-setting'])?>"><i class="fa fa-circle-o"></i> Cluster Setting </a></li> </ul> </li>
You could call it without parameter for reset prefix:
\yidas\NavLocator::setPrefix()
yidas/yii2-nav-locator 适用场景与选型建议
yidas/yii2-nav-locator 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 74 次下载、GitHub Stars 达 2, 最近一次更新时间为 2018 年 11 月 20 日, 在 PHP 生态内属于活跃度较高的组件。
它主要适用于以下技术方向: 「extension」 「navigation」 「helper」 「locator」 「yii」 「yii2」 等业务场景。在实际项目中,围绕这些方向常见需要落地的问题包括:接口对接、性能调优、并发安全、与既有框架(Laravel / ThinkPHP / Yii / Webman 等)的兼容适配,以及生产环境的日志埋点与稳定性保障。
我们在过去多个企业项目中使用过 yidas/yii2-nav-locator 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。
基于 yidas/yii2-nav-locator 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。
线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。
承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。
与 yidas/yii2-nav-locator 相关的其它包
同方向 / 同关键字的高下载量 PHP Composer 包推荐,方便对比选型:
A block to display a list of links to child pages, or pages in current level
A custom URL rule class for Yii 2 which allows to create translated URL rules
Datetime helpers for timezone handling
The Yii2 extension uses jQuery jquery.carousel-1.1.min.js and makes image carousel from php array of structure defined.
TYPO3 CMS extension to create gallery content element with preset crop ratios and pagination
UI Kit 3 Extension for Yii2
统计信息
- 总下载量: 74
- 月度下载量: 0
- 日度下载量: 0
- 收藏数: 2
- 点击次数: 1
- 依赖项目数: 0
- 推荐数: 0
其他信息
- 授权协议: MIT
- 更新时间: 2018-11-20



