URL generation


Use the \think\facade\Route::buildUrl() method to generate URL.

This method will return a think\route\Url object instance. Because the __toString method is used, the routing address can be output directly.

echo \think\facade\Route::buildUrl();

If data is returned to the client, you can cast it to a string type before returning it.

$url = (string) \think\facade\Route::buildUrl();

Use routing identifier

The definition of address expression is different for different routing address methods. The parameters are passed in separately through the second parameter. Suppose we define a routing rule as follows:

Route::rule('blog/:id','index/blog/read');

In the absence of a specified routing identifier, the routing address can be used directly to generate the URL address:

Route::buildUrl('index/blog/read', ['id' => 5, 'name' => 'thinkphp']);

If we specify the routing identifier when registering the route

Route::rule('blog/:id','index/blog/read')->name('blog_read');

then the routing identifier must be used to generate the URL address

Route::buildUrl('blog_read', ['id' => 5, 'name' => 'thinkphp']);

The above methods will generate the following URL address:

/index.php/blog/5/name/thinkphp.html

If your environment supports REWRITE, then the generated URL address will become:

/blog/5/name/thinkphp.html

If you configure:

'url_common_param'=>true

, then the generated URL address will become:

/index.php/blog/5.html?name=thinkphp

Variables that are not in routing rules will directly use ordinary URL parameters.

It should be noted that URL address generation does not detect the validity of the route, but only generates qualifying routing rules based on the given routing address and parameters.

Use routing address

We can also directly use routing address to generate URL, for example:

We define routing rules as follows:

Route::get('blog/:id' , 'blog/read');

You can use the following method to directly generate a URL address using routing rules:

Route::buildUrl('/blog/5');

Then the automatically generated URL address becomes:

/index.php/blog/5.html

URL suffix

By default, the system will automatically read the url_html_suffix configuration parameter as the URL suffix (default is html). If we set:

'url_html_suffix'   => 'shtml'

Then the automatically generated URL address becomes For:

/index.php/blog/5.shtml

If we set multiple URL suffixes to support

'url_html_suffix'   => 'html|shtml'

, the first suffix will be used to generate the URL address, so the automatically generated URL address is still:

/index.php/blog/5.html

If you want to specify URL suffix generation, you can use:

Route::buildUrl('blog/read', ['id'=>5])->suffix('shtml');

Domain name generation

The default generated URL address does not include a domain name. If If you have deployed multiple domain names or want to generate a URL address with a domain name, you need to pass in the fourth parameter. This parameter has two uses:

Automatically generate domain names

Route::buildUrl('index/blog/read',  ['id'=>5])
    ->suffix('shtml')
    ->domain(true);

If true is passed in as the fourth parameter, it means that the domain name is automatically generated. If you enable url_domain_deploy, the domain name that matches the current URL rule will be automatically identified.

For example, we registered the domain name routing information as follows:

Route::domain('blog','index/blog');

Then the above URL address is generated as:

http://blog.thinkphp.cn/read/id/5.shtml

Specified domain name

You can also explicitly pass in the domain name that needs to be generated, for example:

Route::buildUrl('blog/read', ['id'=>5])->domain('blog');

or pass in the complete domain name

Route::buildUrl('index/blog/read', ['id'=>5])->domain('blog.thinkphp.cn');

The generated URL address is:

http://blog.thinkphp.cn/read/id/5.shtml

You can also directly pass in the domain name in the first parameter, for example:

Route::buildUrl('index/blog/read@blog',  ['id'=>5]);
Route::buildUrl('index/blog/read@blog.thinkphp.cn',  ['id'=>5]);

Generate anchor point

Supports generating URL anchor points, Can be used directly in URL address parameters:

Route::buildUrl('index/blog/read#anchor@blog', ['id'=>5]);

When using the anchor point and the domain name together, pay attention to the anchor point in front and the domain name in the back.

The generated URL address is:

http://blog.thinkphp.cn/read/id/5.html#anchor

Add the entry file

Sometimes the URL address we generate may need to be added with index .php or remove index.php. Most of the time, the system will automatically determine. If there is a problem with the automatically generated address, you can use the following method:

Route::buildUrl('index/blog/read', ['id'=>5])->root('/index.php');

Assistant function

The system provides a url helper function to complete the same function, for example:

url('index/blog/read', ['id'=>5])->suffix('html')->domain(true)->root('/index.php');