通过检查网址

时间:2017-07-21 06:52:04

标签: php arrays regex yii2

我的菜单结构就像

Customer

添加估计= url = index.php?r =估计/估算/创建& entity_type =客户

管理估算=网址= index.php?r =估算/估算/指数& entity_type =客户

Lead

添加估计= url = index.php?r =估计/估算/创建& entity_type =铅

管理估算=网址= index.php?r =估算/估算/创建& entity_type =铅

我想通过检查

来制作活动菜单

“创建& entity_type =线索或客户”和“index& 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>

3 个答案:

答案 0 :(得分:0)

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

答案 1 :(得分:0)

如果您仍在寻找完全符合您案例的答案

,请填写
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 :(得分:0)

布局:

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

&GT;

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