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
URL Manager: Urlmanager
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:
\yii:: $app->urlmanager->createurl ($params)
\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]);