【问题标题】:Active menu by checking parameters from the url通过从 url 检查参数来激活菜单
【发布时间】:2017-07-21 06:52:04
【问题描述】:

我的菜单结构是这样的

Customer

添加估算 = url = index.php?r=estimate/estimate/create&entity_type=customer

管理估算 = url = index.php?r=estimate/estimate/index&entity_type=customer

Lead

添加估算 = url = index.php?r=estimate/estimate/create&entity_type=lead

管理估计= url = index.php?r=estimate/estimate/create&entity_type=lead

我想通过检查来制作活动菜单

“create&entity_type=lead or customer”和“index&entity_type=lead or customer”部分的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>

【问题讨论】:

    标签: php arrays regex yii2


    【解决方案1】:

    如果您仍在寻找适合您的情况的答案

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

    【讨论】:

      【解决方案2】:

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

      【讨论】:

      • 更新我的条目
      【解决方案3】:

      在布局中:

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

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2014-06-17
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多