How to modify the URL () function in Laravel to generate the root address of a URL

Source: Internet
Author: User
This article mainly introduces you how to modify the URL () function in Laravel to generate the URL root address of the relevant information, the text through the sample code introduced in very detailed, for everyone to learn or use Laravel has a certain reference learning value, Friends need to follow the small series to learn together.

Objective

This article mainly introduces you to modify the URL () function in Laravel to generate the URL of the root address of the relevant content, I believe we all know that larevel a ticket help function has a URL (), you can give the directory to generate a complete URL, is a very convenient function:


Return:https://skin.dev/user/profileurl (' User/profile ')

But the part of the URL that is generated by this thing is that the inside of the frame is automatically judged based on the Request, and what is automatically judged sometimes goes wrong (for example, in cases where a layer of reverse proxy is set up).

The document does not mention how we can customize the root address and the protocol header portion of the URL it generates (HTTP (s)), which is very flat. What are we going to do about it?

First, let's look at the location where the URL () is defined:


# file:src/illuminate/foundation/helpers.php/** * Generate a URL for the application. * * @param string $path * @param mixed $parameters * @param bool $secure * @return Illuminate\contracts\routing\urlgenerat or|string */function url ($path = null, $parameters = [], $secure = null) {if (Is_null ($path)) {  return app (Urlgenerato R::class); } return App (Urlgenerator::class)->to ($path, $parameters, $secure);}

As you can see, it resolves a illuminate\contracts\routing\urlgenerator from the Laravel service container and transfers the parameters to the object's to method.

And this urlgenerator class is bound to the service container in the src/illuminate/routing/routingserviceprovider.php service provider:


/** * Register the URL generator service. * * @return void */protected function Registerurlgenerator () {$this->app[' url '] = $this->app->share (function ($ APP) {  //slightly  $url = new Urlgenerator (   $routes, $app->rebinding (    ' request ', $this- Requestrebinder ()   )  );  return $url; });}

This means that we can access the Urlgenerator in the service container at any time through the abstract URL, and modify it.

And it does expose the methods we need: Forceschema and Forcerooturl.

The code to modify the root address in the URL generated by the URL () function is as follows:


Use the method it provides to detect if the URL is valid if (app (' URL ')->isvalidurl ($ROOTURL)) {app (' URL ')->forcerooturl ($ROOTURL);} Force generation of Urlapp (' URL ')->forceschema (' https ') using the HTTPS protocol;

The above code is recommended for custom serviceprovider so that all URL () function generated links will use the root address and protocol defined above.

So to speak, to really master the Laravel of those things, it is not enough to read the document. and Laravel source documents do very well, read very clear, can learn a lot of things.

Related Article

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.