关于yii2里面的\yii\helpers\Url组件

    在完成实验室网站的yii2移植过程中,发现在处理网页的跳转中需要经常用到\yii\helpers\Url组件,所以今天来总结一下。

                

       //以http://localhost/basic/web/index.php?r=article/index为例

              

                 //base输出根目录

                 echo \yii\helpers\Url::base();

                 //输出/basic/web

                 echo \yii\helpers\Url::base(true)

                 //输出http://localhost/basic/web

  

               //home是输出首页,加上true是输出加域名的首页

               echo \yii\helpers\Url::home();

               //输出/basic/web/index.php

                echo\yii\helpers\Url::home(true);

               //输出http:://localhost/basic/web/index.php

             

              //当前的Url

               echo \yii\helpers\Url::current();

               //输出/basic/web/index.php?r=article/index

 

              //to和toRoute都是生成Url,后面加true都是生成带域名的Url

              echo \yii\helpers\Url::to([‘article/add’]);

              //输出/basic/web/index.php?r=article/add

              echo \yii\helpers\Url::to([‘article/edit’,’id’=>1]);

              //输出/basic/web/index.php?r=article/add&id=1

              echo \yii\helpers\Url::to([‘article/add’],true);

              //输出http://localhost/basic/web/index.php?r=article/add

              echo \yii\helpers\Url::to([‘article/edit’,’id’=>1],true);

              //输出http://localhost/basic/web/index.php?r=article/add&id=1

             

               echo\yii\helpers\Url::toRoute([‘article/add’]);

               //输出/basic/web/index.php?r=article/add

               echo \yii\helpers\Url::toRoute([‘article/edit’,’id’=>1]);

               //输出/basic/web/index.php?r=article/add&id=1

               echo \yii\helpers\Url::toRoute([‘article/add’],true);

               //输出http://localhost/basic/web/index.php?r=article/add

               echo\yii\helpers\Url::toRoute([‘article/edit’,’id’=>1],true);

               //输出http://localhost/basic/web/index.php?r=article/add&id=1

 

                 //to和toRoute之间的区别,传入string时,to 会直接把string当成url和toRoute则会解析

                 echo\yii\helpers\Url::to(‘article/add’);

                 //输出article/add

                 echo\yii\helpers\Url::toRoute(‘article/add’);

                 //输出/basic/web/index.php?r=article/add



              由于在打字过程中没有注意区分英文标点的切换,所以上面的语句如果有错,需要修改为英文标点即可。

  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值