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

Php 通过检查url中的参数来激活菜单,php,arrays,regex,yii2,Php,Arrays,Regex,Yii2,我的菜单结构是 Customer Add estimate=url=index.php?r=estimate/estimate/create&entity\u type=customer Lead 管理估算=url=index.php?r=估算/估算/索引&实体类型=客户 Lead Add estimate=url=index.php?r=estimate/estimate/create&entity\u type=lead 管理估算=url=index.php?r=estimate/es

我的菜单结构是

Customer
Add estimate=url=index.php?r=estimate/estimate/create&entity\u type=customer

Lead
管理估算=url=index.php?r=估算/估算/索引&实体类型=客户

Lead
Add estimate=url=index.php?r=estimate/estimate/create&entity\u type=lead

管理估算=url=index.php?r=estimate/estimate/create&entity\u type=lead

我想通过选中来创建活动菜单

url的“创建和实体类型=潜在客户或客户”和“索引和实体类型=潜在客户或客户”部分

如果我将完整的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>
功能活动菜单($link){
return$\u GET['r']==$link'active':'';
}
使用提取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>

以防万一,如果您仍在寻找适合您的案例的答案

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') ?>">

布局中的

<?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,.
    }
}

Html格式的

<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>

更新我的条目