nbsp; 关于yii的bootstrap插件,虽然官网上有api文档,个人总觉得不是很详尽,很多细节的地方仍然搞不清楚怎么用,只好一点一点去试,这不,今天又碰到一问题。需求是这样的,我想在bootstrap.widgets.TbMenu生成的menu中加入非本站点的链接,当我把完整的链接填入
'items'=>array(
array('label'=>'Home', 'url'=>array('http://b.qq.com')),
),
这里面的'url'一项时,出来的链接总是在头上带上了我本站的host,度娘上搜了一圈未果,于是只好自己去看bootstrap.widgets.TbMenu这个类,发现这个类是继承了TbBaseMenu这个基类,而在TbBaseMenu这里面的renderMenuItem方法中调用了CHtml::link()这个方法,于是再接再厉研究了一下CHtml::link()这个方法,这里调用了CHtml里面的另一个方法normalizeUrl,顾名思义也就是格式化url,看了代码还是没懂,后来看了注释终于懂了
/**
* Normalizes the input parameter to be a valid URL.
*
* If the input parameter is an empty string, the currently requested URL will be returned.
*
* If the input parameter is a non-empty string, it is treated as a valid URL and will
* be returned without any change.
*
* If the input parameter is an array, it is treated as a controller route and a list of
* GET parameters, and the {@link CController::createUrl} method will be invoked to
* create a URL. In this case, the first array element refers to the controller route,
* and the rest key-value pairs refer to the additional GET parameters for the URL.
* For example, <code>array('post/list', 'page'=>3)</code> may be used to generate the URL
* <code>/index.php?r=post/list&page=3</code>.
也就是说
1.如果原url是空字符串,返回当前url地址
2.如果url是字符串,返回字符串对应的地址
3.如果url是数组,则将数组内的内容以controller路由的形式由yii框架带的规则生成相应的url地址
其实我想要的就是第二种情况,改了一下TbMenu中的配置就得到我想要的啦
'items'=>array(
array('label'=>'Home', 'url'=>'http://b.qq.com',)
),
然后我又想如何可以在新窗口打开我的url呢?研读了一下TbBaseMenu中的代码发现只要加一个linkOptions选项就好啦
'items'=>array( array('label'=>'Home','url'=>'http://b.qq.com','linkOptions'=>array('target'=>'_blank'),), ),
至此,问题解决