獲取當前URL
獲取當前URL有兩種方式,URL::current()或URL::full(),區別是返不返回GET參數如
Route::get('/current/url',function(){return URL::current();});
輸入/current/url?foo=bar時只顯示http://myapp.dev/current/url。使用URL::full()則顯示http://myapp.dev/current/url?foo=bar
獲取之前的URL
// app/routes.phpRoute::get('first',function(){// Redirect to the second route.return Redirect::to('second');});Route::get('second',function(){eturn URL::previous();});
輸入/first,返回http://loacahost,URL::previous()返回的是之前到first的路由
生成URL
使用URL::to()生成URL,如
Route::get('example',function(){return URL::to('another/route', array('foo','bar'));});
生成的URL為http://myapp.dev/another/route/foo/bar,如需將HTTP協議變為HTTPS,則用
URL::to('another/route', array('foo','bar'),true);
或是使用
URL::secure('another/route', array('foo','bar'));
使用路由別名生成URL
Route::get('the/best/avenger', array('as'=>'ironman',function(){return'Tony Stark';}));Route::get('example',function(){return URL::route('ironman');});
使用URL參數
Route::get('the/{first}/avenger/{second}', array('as'=>'ironman',function($first, $second){return"Tony Stark, the {$first} avenger {$second}.";}));Route::get('example',function(){return URL::route('ironman', array('best','ever'));});
到控制器的URL
// Route to the Stark controller.Route::get('tony/the/{first}/genius','Stark@tony');Route::get('example',function(){return URL::action('Stark@tony', array('narcissist'));});
到資源的絕對URL
Route::get('example',function(){return URL::asset('img/logo.png');});
返回http://myapp.dev/img/logo.png,同樣,使用HTTPS
return URL::asset('img/logo.png',true);
或是
return URL::secureAsset('img/logo.png');
在視圖中生成URL
使用url()在視圖中生成URL,方法跟參數跟以上的沒什么區別,使用如下
<ahref="">My Route</a>
或是
<ahref="">My Route</a>
使用路由別名
<ahref="">My Route</a>
使用控制器
<ahref="">My Route</a>
使用資源
<ahref="">My Route</a><ahref="">My Route</a>
結束
關於url可以看官網的api
