获取当前URL

获取当前URL有两种方式,URL::current()URL::full(),区别是返不返回GET参数如

  1. Route::get('/current/url',function()
  2. {
  3. return URL::current();
  4. });

输入/current/url?foo=bar时只显示http://myapp.dev/current/url。使用URL::full()则显示http://myapp.dev/current/url?foo=bar

获取之前的URL

  1. // app/routes.php
  2. Route::get('first',function()
  3. {
  4. // Redirect to the second route.
  5. returnRedirect::to('second');
  6. });
  7. Route::get('second',function()
  8. {
  9. eturn URL::previous();
  10. });

输入/first,返回http://loacahost,URL::previous()返回的是之前到first的路由

生成URL

使用URL::to()生成URL,如

  1. Route::get('example',function()
  2. {
  3. return URL::to('another/route', array('foo','bar'));
  4. });

生成的URL为http://myapp.dev/another/route/foo/bar,如需将HTTP协议变为HTTPS,则用

  1. URL::to('another/route', array('foo','bar'),true);

或是使用

  1. URL::secure('another/route', array('foo','bar'));

使用路由别名生成URL

  1. Route::get('the/best/avenger', array('as'=>'ironman',function()
  2. {
  3. return'Tony Stark';
  4. }));
  5. Route::get('example',function()
  6. {
  7. return URL::route('ironman');
  8. });

使用URL参数

  1. Route::get('the/{first}/avenger/{second}', array(
  2. 'as'=>'ironman',
  3. function($first, $second){
  4. return"Tony Stark, the {$first} avenger {$second}.";
  5. }
  6. ));
  7. Route::get('example',function()
  8. {
  9. return URL::route('ironman', array('best','ever'));
  10. });

到控制器的URL

  1. // Route to the Stark controller.
  2. Route::get('tony/the/{first}/genius','Stark@tony');
  3. Route::get('example',function()
  4. {
  5. return URL::action('Stark@tony', array('narcissist'));
  6. });

到资源的绝对URL

  1. Route::get('example',function()
  2. {
  3. return URL::asset('img/logo.png');
  4. });

返回http://myapp.dev/img/logo.png,同样,使用HTTPS

  1. return URL::asset('img/logo.png',true);

或是

  1. return URL::secureAsset('img/logo.png');

在视图中生成URL

使用url()在视图中生成URL,方法跟参数跟以上的没什么区别,使用如下

  1. <ahref="">My Route</a>

或是

  1. <ahref="">My Route</a>

使用路由别名

  1. <ahref="">My Route</a>

使用控制器

  1. <ahref="">My Route</a>

使用资源

  1. <ahref="">My Route</a>
  2. <ahref="">My Route</a>

结束