1、\yii\helpers\Url组件

    

<?php
        /以http://localhost:8080/yii2-demo/web/index.php?r=article/index为例
        //base输出根目录
        echo \yii\helpers\Url::base();
        //输出/yii2-demo/web
        echo \yii\helpers\Url::base(true);
        //输出http://localhost:8080/yii2-demo/web

        //home是输出首页 , 加上true是输出加域名的首页
        echo \yii\helpers\Url::home();
        //输出/yii2-demo/web/index.php
        echo \yii\helpers\Url::home(true);
        //输出http://localhost:8080/yii2-demo/web/index.php

        //当前的Url
        echo \yii\helpers\Url::current();
        //输出/yii2-demo/web/index.php?r=article/index

        //to和toRoute都是生成Url , 后面加true都是生成带域名的Url
        echo \yii\helpers\Url::to(['article/add']);
        //输出/yii2-demo/web/index.php?r=article/add
        echo \yii\helpers\Url::to(['article/edit' , 'id' => 1]);
        //输出/yii2-demo/web/index.php?r=article/add&id=1
        echo \yii\helpers\Url::to(['article/add'] , true);
        //输出http://localhost:8080/yii2-demo/web/index.php?r=article/add
        echo \yii\helpers\Url::to(['article/edit' , 'id' => 1] , true);
        //输出http://localhost:8080/yii2-demo/web/index.php?r=article/add&id=1

        echo \yii\helpers\Url::toRoute(['article/add']);
        //输出/yii2-demo/web/index.php?r=article/add
        echo \yii\helpers\Url::toRoute(['article/edit' , 'id' => 1]);
        //输出/yii2-demo/web/index.php?r=article/add&id=1
        echo \yii\helpers\Url::toRoute(['article/add'] , true);
        //输出http://localhost:8080/yii2-demo/web/index.php?r=article/add
        echo \yii\helpers\Url::toRoute(['article/edit' , 'id' => 1] , true);
        //输出http://localhost:8080/yii2-demo/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');
        //输出/yii2-demo/web/index.php?r=article/add
?>
更加具体的请查阅:http://www.yiiframework.com/doc-2.0/yii-helpers-url.html

版权声明:未经博主允许不得转载。http://smister.com/post-32.html