Laravel URL management and usage

Source: Internet
Author: User

 

Get current URL

You can obtain the current URL in either of the following ways,URL::current()OrURL::full()The difference is that the get parameter is not returned, as shown in figure

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

Input/current/url?foo=barOnly showhttp://myapp.dev/current/url. UseURL::full()Displayhttp://myapp.dev/current/url?foo=bar

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

Input/first, Returnhttp://loacahost,URL::previous()The returned route is the previous route to first.

 

Generate URL

UseURL::to()Generate a URL, as shown in figure

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

The generated URL ishttp://myapp.dev/another/route/foo/barTo change the HTTP protocol to https, use

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

Or use

  1. URL::secure(‘another/route‘, array(‘foo‘,‘bar‘));
Use route alias to generate 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. });

Use URL parameters

  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 to the Controller
  1. // Route to the Stark controller.
  2. Route::get(‘tony/the/{first}/genius‘,‘[email protected]‘);
  3. Route::get(‘example‘,function()
  4. {
  5. return URL::action(‘[email protected]‘, array(‘narcissist‘));
  6. });
Absolute URL to resource
  1. Route::get(‘example‘,function()
  2. {
  3. return URL::asset(‘img/logo.png‘);
  4. });

Returnhttp://myapp.dev/img/logo.png, Similarly, Use https

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

Or

  1. return URL::secureAsset(‘img/logo.png‘);
Generate URL in view

Useurl()Generate a URL in the view. The method and parameters are similar to the preceding one.

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

Or

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

Use route alias

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

Use Controller

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

Use resources

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

For URL information, see the API on the official website.

From: http://www.cnblogs.com/steven9801/p/3560738.html

More: http://codego.net/573717/

Laravel URL management and usage

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.