Home  >  Article  >  PHP Framework  >  How to generate hyperlinks in yii

How to generate hyperlinks in yii

王林
王林Original
2020-02-17 11:42:491819browse

How to generate hyperlinks in yii

In the project, it is recommended to use the built-in URL tool class of Yii2 to generate links, which can very conveniently manage the URL behavior of the entire site: such as changing the URL format of the entire site by modifying the configuration, etc. .

Yii2 Default URL link format

Yii2 Default URL link format refers to the format when URL beautification is enabled.

Recommended learning: yii framework

The URL format of the submodule is not enabled:

// 参数 r 中的 article 表示控制器, view 表示动作http://www.example.com/index.php?r=article/view&id=100

The URL format of the submodule is enabled:

// 参数 r 中的 kernel 表示子模块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 generation tool can beautify the URL of the entire site through configuration without changing the source code.

URL Manager

The URL manager is a built-in application component called urlManager. In WEB applications and console applications, URLs can be created in the following two ways:

\Yii::$app->urlManager->createUrl($params)
\Yii::$app->urlManager->createAbsoluteUrl($params, $schema = null)

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

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

Common examples of using URL managers to create URLs:

// 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

Compared with the URL manager, using the yii\helpers\Url helper class can greatly simplify the creation of URLs.

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

How to generate hyperlinks in yii

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

How to generate hyperlinks in yii

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

How to generate hyperlinks in yii

For more programming-related content, please pay attention to the

Programming Tutorial column on the php Chinese website!

The above is the detailed content of How to generate hyperlinks in yii. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Previous article:How to update data in yiiNext article:How to update data in yii