通过检查 url 中的参数来激活菜单



我的菜单结构是这样的

Customer

添加估计 = 网址 = 索引.php?r=估计/估计/创建&entity_type=客户

管理估算 = url = 索引.php?r=估计/估计/索引&entity_type=客户

Lead

添加估计 = 网址 = 索引.php?r=估计/估计/创建&entity_type=潜在客户

管理估计= url = 索引.php?r=估计/估计/创建&entity_type=潜在客户

我想通过检查来激活菜单

网址的"创建entity_type=潜在客户或客户"和"索引&entity_type=潜在客户或客户"部分。

如果我将完整的 url 添加到数组列表以匹配它会使两个菜单项都处于活动状态。 TIA 寻求帮助

到目前为止我的尝试

function activeMenu($link){
return  $_GET['r']==$link?'active':'';  
}
<li class="<?=activeMenu('estimate/estimate/create&entity_type=customer')?>">
<a href="index.php?r=estimate/estimate/create&entity_type=customer"><?php echo Yii::t('app', 'Add Estimate');?> </a>
</li>

以防万一您仍在寻找完全适用于您的情况的答案

function activesubmenuMenu( $action, $entity_type )
{
$path = parse_url( $_SERVER['REQUEST_URI']);
$route = $_GET['r'];
$route = explode( "/", trim( $route, "/" ) );
return ( $action == $route[2] && $_GET['entity_type'] == $entity_type ) ? 'active' : '';
}

function activecustomerMenu($entity_type)
{
return ($_GET['entity_type'] == $entity_type ) ? 'active' : '';
}
function activeleadMenu($entity_type)
{
return ($_GET['entity_type'] == $entity_type ) ? 'active' : '';
}

然后添加到你的HTML喜欢

<li class="<?=activecustomerMenu('customer')?>">
<li class="<?=activeleadMenu('lead')?>">
<li class="<?= activesubmenuMenu('create', 'customer') ?>">
<li class="<?= activesubmenuMenu('index', 'customer') ?>">

parse_url提取URL的所有部分。自定义此函数:

function activeMenu( $action, $entity_type )
{
// With Yii i think that exist function that return uri_string
$path_c_url = parse_url( $_SERVER['REQUEST_URI'], PHP_URL_PATH );
$action_c_url = "";
// If exists path
if( ! empty( $path_c_url ) )
{
// array of parts slug
$path_c_url = explode( "/", trim( $path_c_url, "/" ) );
// last part of slug
$action_c_url = array_pop( $path_c_url );
}
// check with slug and get params
return ( $action == $action_c_url && $_GET['entity_type'] == $entity_type ) ? 'active' : '';
}

更新

使用此 HMTL:

<li class="<?php echo activeMenu('create', 'customer'); ?>">
<a href="index.php?r=estimate/estimate/create&entity_type=customer"><?php echo Yii::t('app', 'Add Estimate');?> </a>
</li>

在布局中:

<?php 
$actionName         = strtolower( $this->getAction()->id );
$contollerName      = strtolower( $this->getId() );
$estimate_create    = '';
$estimate_update    = '';
$estimate_index     = '';
$estimate_view      = '';
$site_dashboard = '';
$site_profile       = '';
// estimate controller example
if($contollerName=='estimate')
{
switch ($actionName)
{
case 'create':      $estimate_create    = 'active ';    break;
case 'update':      $estimate_update    = 'active ';    break;
case 'index' :      $estimate_index     = 'active ';    break;
case 'view'  :      $estimate_view      = 'active ';    break;
// etc,.
}
}
// another controller example
if($contollerName=='site')
{
switch ($actionName)
{
case 'dashboard':   $site_dashboard     = 'active ';    break;
case 'profile':     $site_profile       = 'active ';    break;
// etc,.
}
}

?>

在网页中

<li class="<?php echo $estimate_create; ?>">
<a href="index.php?r=estimate/estimate/create&entity_type=customer">
<?php echo Yii::t('app', 'Add Estimate');?> </a>
</li>
<li class="<?php echo $estimate_update; ?>">
<a href="index.php?r=estimate/estimate/update&entity_type=customer">
<?php echo Yii::t('app', 'Update Estimate');?> </a>
</li>
<li class="<?php echo $estimate_view; ?>">
<a href="index.php?r=estimate/estimate/view&entity_type=customer">
<?php echo Yii::t('app', 'View Estimate');?> </a>
</li>
<li class="<?php echo $site_dashboard; ?>">
<a href="index.php?r=site/dashboard">
<?php echo Yii::t('app', 'Dashboard');?> </a>
</li>
<li class="<?php echo $site_profile; ?>">
<a href="index.php?r=site/profile">
<?php echo Yii::t('app', 'Dashboard');?> </a>
</li>

最新更新