// 字符串形式
U('[項目://][路由@][分組名-模塊/]操作?參數1=值1[&參數N=值N]')
// 數組形式(推薦,避免特殊字符轉義問題)
U('[項目://][路由@][分組名-模塊/]操作', array('參數1'=>'值1', '參數N'=>'值N'))
U('Blog/read/id/1') 在 PATHINFO 模式下有效,但切換 URL 模式時極易出錯,強烈不推薦。以 U('Blog/read?id=1') 為例:
| URL 模式 | 生成地址 |
|---|---|
| 普通模式 | https://<server>/index.php?m=Blog&a=read&id=1 |
| PATHINFO | https://<server>/index.php/Blog/read/id/1 |
| REWRITE | https://<server>/Blog/read/id/1 |
| REWRITE + .html 後綴 | https://<server>/Blog/read/id/1.html |
同一調用代碼,零修改適配不同部署環境,極大提升項目可移植性。
定義路由 View → Blog/read(參數 id),則:
U('View@?id=1') // 生成 https://<server>/index.php/View/id/1 (PATHINFO下)
限制:僅支持簡單路由,不支持泛路由、正則路由。
U('Myapp://User/add') // 跨項目生成 Myapp 項目 User/add URL
U('Blog/read?id=1') // 當前項目 Blog 模塊 read 操作帶參
U('Admin-User/select') // Admin 分組 User 模塊 select 操作
U('Index/index', array('page'=>2, 'size'=>20)) // 數組傳參,自動拼接 ?page=2&size=20