Yii2 How to generate URLs

Source: Internet
Author: User
Tags yii
In the project, it is recommended to use the YII2 built-in URL tool class to generate links, so that it is very convenient to manage the URL behavior of the whole site: for example, by modifying the configuration to change the URL format of the whole station. For more advanced usage of URLs see the official documentation, this article only describes several ways that Yii2 generates URLs.

Yii2 default URL-link format

Yii2 the default URL link format is the format to turn on URL beautification.

The URL format of the submodule is not enabled:


The article in parameter R represents the controller, and the view represents the action http://www.example.com/index.php?r=article/view&id=100

The URL format of the submodule is enabled:


The kernel in parameter R represents a submodule http://www.example.com/index.php?r=kernel/article/view&id=100

Yii2 built-in URL generation tool

    1. URL Manager: Urlmanager

    2. URL Helper Class: Yii\helpers\url

Use the built-in URL Generator tool, can be configured in the future without changing the source code to beautify the entire site URL

URL Manager

The URL Manager is a built-in application component called Urlmanager. In WEB apps and console apps, you create URLs in the following two ways:

    1. \yii:: $app->urlmanager->createurl ($params)

    2. \yii:: $app->urlmanager->createabsoluteurl ($params, $schema = null)

createUrl Method generates a relative path to the root directory, for example:/index.php?r=article/view

createAbsoluteUrl() Method generates an absolute path, for example:http://www.example.com/index.php?r=article/view

A common example of creating URLs using the URL Manager:


Url:/index.php?r=article/view\yii:: $app->urlmanager->createurl (' Article/view ');//url:/index.php?r= Article/view&id=2\yii:: $app->urlmanager->createurl ([' Article/view ', ' ID ' =>2]);//url:http:// Www.example.com?r=kernel/article/viewecho \yii:: $app->urlmanager->createabsoluteurl (' Kernel/article/view ' );

URL Helper Class

Using the yii\helpers\Url helper class can greatly simplify the creation of URLs relative to the URL manager.

1. Assuming the current URL /index.php?r=kernel/article/view&id=10 , the following describes how the URL helper class Url::to() works (not recommended):


Create current url//display:/?r=kernel/article/view&id=10echo url::to ();//Create current url//display: http://www.example.com/?r=kernel/ Article/view&id=10echo url::to (', true);//character parameter, no use//display: Kernel/article/viewecho url::to (' Kernel/article/view ' );//Create a route, an automatic call to an array parameter Url::toroute (...) Showing:/index.php?r=kernel/article/viewecho url::to ([' Article/view ']);

2. Assuming the current URL /index.php?r=kernel/article/view&id=10 , the following describes how the URL helper class Url::toRoute() method works (recommended):


Create the current route (only the value of the parameter R is inherited)//display:/index.php?r=kernel/article/viewecho url::toroute ([]);//the same module and controller, different actions (only inherit the value of parameter R)//display:/ Index.php?r=kernel/article/listecho url::toroute (' list ');//the same module and controller, different actions (only inherit the value of parameter R)//display:/index.php?r=kernel/ Article/list&cat=contactecho url::toroute ([' List ', ' Cat ' =>10]);//the same module, different controllers and actions (only values that inherit parameter R)//display:/index.php?r =kernel/product/indexecho url::toroute (' Product/index ');//absolute routing, regardless of which module and controller is called//displayed:/index.php?r=product/ Indexecho url::toroute ('/product/index ');//Controller action ' actionlisthot ' URL format (only inherit the value of parameter r, case-sensitive)//display:/index.php?r=kernel/ Article/list-hotecho url::toroute (' list-hot ');//Get the URL from the alias//display: Http://www.baidu.com/Yii::setAlias (' @baidu ', ' http ://www.baidu.com/'); Echo url::to (' @baidu ');

3. Assuming the current URL /index.php?r=kernel/article/view&id=10 , the following describes how the URL helper class Url::current() method works (recommended):


//Create the current URL (including routes and parameters)//display:/?r=kernel/article/view&id=10echo url::current ( );//Remove parameter id//display:/?r=kernel/article/viewecho url::current ([' ID ' =>null]);//New parameter cat//display:/?r=kernel/article/view &id=10&cat=2echo url::current ([' Cat ' =>2]); 
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.