YII的CMenu使用中url和active的问题
2016-06-10 00:00
561 查看
摘要: 只是讨论YII的CMenu使用中url和active的一个特殊情况:
'url'=>array('/posts/index&postType=c')
'url'=>array('/posts/index&postType=php')
不详细介绍CMenu的使用
YII的CMenu一般的使用方法如下:
要获取controller的名称,可以直接使用$this->id。
要获取action的名称,可以使用$this->action->id。
CMenu的具体使用可以参考下面的链接,个人觉得这个链接的内容比其他地方更易懂
http://lhj0206.lofter.com/post/1cb9f491_4d2a72c
但是如果URL如下所示:
'url'=>array('/posts/index&postType=c')
'url'=>array('/posts/index&postType=php')
要区分这两个url,就要区分postType的值,但是大部分讲解CMenu的地方都没有说到怎么获取这些值。
这时候要获取postType,可以使用 Yii::app()->request->requestUri 获取到整个URI,然后进行相应操作,比如:
'active'=>(
$this->id=='posts'
&& $this->action->id=='index'
&& strpos(Yii::app()->request->requestUri,'php')
) ? true : false,
上面代码的意思是,当controller为posts,action为index,URI中包含php时,active才为true。
这是直接对URI进行比较,我们也可以通过URI得到postType的具体值后再做比较。
这里使用了strpos方法,这个方法的使用可以参考下面的链接:
http://www.w3school.com.cn/php/func_string_strpos.asp
感谢stackoverflow的一个回答:
http://stackoverflow.com/questions/9802662/yii-how-to-highlight-the-current-menu-item
'url'=>array('/posts/index&postType=c')
'url'=>array('/posts/index&postType=php')
不详细介绍CMenu的使用
YII的CMenu一般的使用方法如下:
$this->widget('zii.widgets.CMenu',array( 'linkLabelWrapper' => 'span', 'items'=>array( array('label'=>'Home', 'url'=>array('post/index'),'active'=>$this->id=='default'?true:false), array('label'=>'About', 'url'=>array('site/page'),'active'=>$this->action->id=='page'?true:false), array('label'=>'Contact', 'url'=>array('site/contact'),'active'=>$this->action->id=='contact'?true:false), array('label'=>'Login', 'url'=>array('site/login'), 'visible'=>Yii::app()->user->isGuest), array('label'=>'Logout ('.Yii::app()->user->name.')', 'url'=>array('site/logout'), 'visible'=>!Yii::app()->user->isGuest) ), ));
要获取controller的名称,可以直接使用$this->id。
要获取action的名称,可以使用$this->action->id。
CMenu的具体使用可以参考下面的链接,个人觉得这个链接的内容比其他地方更易懂
http://lhj0206.lofter.com/post/1cb9f491_4d2a72c
但是如果URL如下所示:
'url'=>array('/posts/index&postType=c')
'url'=>array('/posts/index&postType=php')
要区分这两个url,就要区分postType的值,但是大部分讲解CMenu的地方都没有说到怎么获取这些值。
这时候要获取postType,可以使用 Yii::app()->request->requestUri 获取到整个URI,然后进行相应操作,比如:
'active'=>(
$this->id=='posts'
&& $this->action->id=='index'
&& strpos(Yii::app()->request->requestUri,'php')
) ? true : false,
上面代码的意思是,当controller为posts,action为index,URI中包含php时,active才为true。
这是直接对URI进行比较,我们也可以通过URI得到postType的具体值后再做比较。
这里使用了strpos方法,这个方法的使用可以参考下面的链接:
http://www.w3school.com.cn/php/func_string_strpos.asp
感谢stackoverflow的一个回答:
http://stackoverflow.com/questions/9802662/yii-how-to-highlight-the-current-menu-item
相关文章推荐
- java-用HttpURLConnection发送Http请求.
- 网友问答之:AD、DNS转发器、WINS不能自动启动
- VBScript 剪贴板抓取URL并在浏览器中打开
- 使用 Iisftp.vbs 设置Active Directory 用户隔离
- 用vbs 实现从剪贴板中抓取一个 URL 然后在浏览器中打开该 Web 站点
- asp获取URL参数的几种方法分析总结[原创]_应用技巧_脚本之家
- C#实现在网页中根据url截图并输出到网页的方法
- zend framework框架中url大小写问题解决方法
- YII Framework框架教程之安全方案详解
- yii2中使用Active Record模式的方法
- Yii PHP Framework实用入门教程(详细介绍)
- url decode problem 解决方法
- php url地址栏传中文乱码解决方法集合
- 探讨:parse url解析URL,返回其组成部分
- PHP的Yii框架中Model模型的学习教程
- 详解PHP的Yii框架中自带的前端资源包的使用
- C#实现自动识别URL网址的方法
- C#自定义针对URL地址的处理类实例
- JavaScritp添加url参数并将参数加入到url中及更改url参数的方法
- ASP让url的中文显示为编码