如何在CakePHP中识别活动菜单链接
我正在为管理员侧边栏创建一个手风琴布局。现在我需要识别活动链接,并向该链接添加一个类如何在CakePHP中识别活动菜单链接,cakephp,accordion,Cakephp,Accordion,我正在为管理员侧边栏创建一个手风琴布局。现在我需要识别活动链接,并向该链接添加一个类active。这是我的密码: <div class="accordion-group"> <div class="accordion-heading"> <a href="#collapseSeven" data-parent="#side_accordion" data-toggle="collapse" class="accordion-toggle">
active
。这是我的密码:
<div class="accordion-group">
<div class="accordion-heading">
<a href="#collapseSeven" data-parent="#side_accordion" data-toggle="collapse" class="accordion-toggle">
<i class="icon-th"></i> Gallery Manager
</a>
</div>
<div class="accordion-body collapse" id="collapseSeven">
<div class="accordion-inner">
<ul class="nav nav-list">
<li>
<?php echo $this->Html->link('View All',array('controller' => 'gallaries', 'action' => 'index'));?>
</li>
<li>
<?php echo $this->Html->link('Add New',array('controller' => 'gallaries', 'action' => 'add'));?>
</li>
</ul>
</div>
</div>
</div>
-
-
最好的方法是什么?提前谢谢 有很多方法,下面是一些将类添加到容器中的方法
<li <?php echo ($url == 'users/account')? 'class="current"' : ''?>>
<li <?php echo (preg_match("/addresses/", $url))? 'class="current"' : ''?>>
<li <?php echo ($this->params['controller'] == 'attributes')? 'class="current"' : ''?>>
我找到了解决办法:
$url = $this->Html->url('INPUT_THE_URL') ;
$active = $this->request->here == $url? true: false;
下面是添加活动类的简单方法:
<ul class="nav nav-list">
<li class="<?php echo (($this->params['controller']==='gallaries')&&($this->params['action']=='index') )?'active' :'' ?>">
<?php echo $this->Html->link('View All',array('controller' => 'gallaries', 'action' => 'index'));?>
</li>
<li class="<?php echo (($this->params['controller']==='gallaries')&& ($this->params['action']=='add') )?'active' :'' ?>">
<?php echo $this->Html->link('Add New',array('controller' => 'gallaries', 'action' => 'add'));?>
</li>
</ul>
要检查给定的URL在Cakephp 2.x中是否当前处于活动状态,您应该检查它的规范化形式(在的意义上)是否与当前请求的URL的规范化形式(在的意义上)相同
有时,如果当前显示的是子页面,则可能需要松散匹配以在菜单中将页面显示为活动页面。您是否想在
/fruits/Banana/
浏览香蕉详情网站时,将指向水果概览网站的菜单链接显示为活动状态。您可以通过仅查找部分匹配来轻松实现这一点
$isActive = (0 === strpos($currentUrl, $checkedUrl));
当然,您的匹配可能会变得更复杂,例如,如果您大量使用命名参数等,并希望在菜单中反映它,但您应该从这里找到方法
针对特定问题的解决方案可能如下所示:
$currentUrl = Router::normalize($this->request->here);
$links = array(
array(
'label' => __('View All'),
'url' => array('controller' => 'galleries', 'action' => 'index'),
),
array(
'label' => __('Add New'),
'url' => array('controller' => 'galleries', 'action' => 'add'),
),
/* ... */
);
foreach ($links as $link) {
$linkLabel = $link['label'];
$linkUrl = Router::url($link['url']);
$linkHtml = $this->Html->link($linkLabel, $linkUrl);
$linkActive = $currentUrl === $linkUrl;
echo $this->Html->tag('li', $linkHtml, array(
'class' => $linkActive ? 'active' : '',
'escape' => false, // to not escape anchor markup
));
}
为了让你的生活变得简单一点,你甚至不去想这个问题,你也可以使用一个助手来创建菜单,就像其他人创建的那样。我知道这很古老,但我找到了一个很好的解决方案
根据费萨尔的回答,我写了我自己的简单助手:
App::uses('AppHelper', 'View/AppHelper');
class PVHtmlHelper extends AppHelper {
public $helpers = array('Html');
public function link($title = null, $url = null, $options) {
if ($title == null || $url == null)
return;
$class = (($this->params['controller']===$url['controller']) && ($this->params['action']==$url['action']) )?'active' :'';
echo "<li class='" . $class . "'>" . $this->Html->link($title, $url, $options) . "</li>";
}
}
通过这种方法,我必须在每个链接上/之前设置条件,如果有任何帮助者或任何其他方法可以这样做的话dynamically@Krishna这通常是您在PHP中必须使用的方法。不幸的是,如果您具有一致的布局,您可以创建自己的helper类。您可以使用现有的helper等。试着用谷歌搜索它们或者搜索cakephp插件网站。你可以尝试新的帮助程序,比如这一个,设计用于Twitter引导。这是一种非常奇怪和不合理的方法,在PHP中,您肯定可以获得更一致、更灵活和更优雅的…向下投票,因为here()
包含所有命名参数和查询字符串。此$This->request->here
中的URL不一定与路由器生成的URL相同,即使它应该相同。在比较URL之前,您应该先将URL规范化。这种方法在使用URL时会导致误报,在使用规范URL以外的URL请求内容时也会导致误报。而且它非常冗长和不合理。更好的方法是在与规范化后检查匹配的URL。谢谢,这对我帮助很大!=)我使用CakePHP3.1.5。因此,对于阅读本文的任何人来说,3.x的语法必须是:$this->request->params['action']
。以及文件开头的一些差异。见:
$currentUrl = Router::normalize($this->request->here);
$links = array(
array(
'label' => __('View All'),
'url' => array('controller' => 'galleries', 'action' => 'index'),
),
array(
'label' => __('Add New'),
'url' => array('controller' => 'galleries', 'action' => 'add'),
),
/* ... */
);
foreach ($links as $link) {
$linkLabel = $link['label'];
$linkUrl = Router::url($link['url']);
$linkHtml = $this->Html->link($linkLabel, $linkUrl);
$linkActive = $currentUrl === $linkUrl;
echo $this->Html->tag('li', $linkHtml, array(
'class' => $linkActive ? 'active' : '',
'escape' => false, // to not escape anchor markup
));
}
App::uses('AppHelper', 'View/AppHelper');
class PVHtmlHelper extends AppHelper {
public $helpers = array('Html');
public function link($title = null, $url = null, $options) {
if ($title == null || $url == null)
return;
$class = (($this->params['controller']===$url['controller']) && ($this->params['action']==$url['action']) )?'active' :'';
echo "<li class='" . $class . "'>" . $this->Html->link($title, $url, $options) . "</li>";
}
}
echo $this->PVHtml->link('Login', array('controller' => 'users', 'action' => 'login'));