關於Url類的操作在這個頁面http://www.yiichina.com/doc/guide/2.0/helper-url;
Url::to()
和 toRoute() 非常類似。這兩個方法的唯一區別在於,前者要求一個路由必須用數組來指定。 如果傳的參數為字符串,它將會被直接當做 URL
Url::to()
的第一個參數可以是:
- 數組:將會調用 toRoute() 來生成URL。比如:
['site/index']
,['post/index', 'page' => 2]
。 詳細用法請參考 toRoute() 。 - 帶前導
@
的字符串:它將會被當做別名, 對應的別名字符串將會返回。 - 空的字符串:當前請求的 URL 將會被返回;
- 普通的字符串:返回本身
Url::to('@web/imgs/loading2.gif') 返回的是basic/web/imgs/loading2.gif , 如果Url::to('/imgs/loading2.gif',true)則返回的是http://localhost/basic/web/imgs/loading2.gif(添加了域名,變成了絕對路徑)。
// /index.php?r=site/index echo Url::to(['site/index']); // /index.php?r=site/index&src=ref1#name echo Url::to(['site/index', 'src' => 'ref1', '#' => 'name']); // /index.php?r=post/edit&id=100 assume the alias "@postEdit" is defined as "post/edit" echo Url::to(['@postEdit', 'id' => 100]); // the currently requested URL echo Url::to(); // /images/logo.gif echo Url::to('@web/images/logo.gif'); // images/logo.gif echo Url::to('images/logo.gif'); // http://www.example.com/images/logo.gif echo Url::to('@web/images/logo.gif', true); // https://www.example.com/images/logo.gif echo Url::to('@web/images/logo.gif', 'https');