Yii URL Helper

Source: Internet
Author: User
Tags php language yii

URL Helper Class?
    1. Get a generic URL
    2. Remember URLs
    3. Check relative URLs

The URL helper class provides a series of static methods to help manage URLs.

Get a generic URL?

There are two ways to get generic URLs: The home URL of the current request and the base URL. To get the home URL, use the following code:

$relativeHomeUrl = Url::home();$absoluteHomeUrl = Url::home(true);$httpsAbsoluteHomeUrl = Url::home(‘https‘);

If no arguments are passed, this method generates a relative URL. You can true get an absolute URL for the current protocol, or you can explicitly specify a specific protocol type ( https , http ).

The following code can get the base URL for the current request:

`PHP $relativeBaseUrl = url::base (); $ABSOLUTEBASEURL = Url::base (true); $HTTPSABSOLUTEBASEURL = url::base (' https ');

这个方法的调用方式和 `Url::home()` 的完全一样。## 创建 URLs <span id="creating-urls"></span>为了创建一个给定路由的 URL 地址,请使用 `Url::toRoute()`方法。 这个方法使用 [[\yii\web\UrlManager]] 来创建一个 URL :```php$url = Url::toRoute([‘product/view‘, ‘id‘ => 42]);

You can specify a string to be used as a route, such as: site/index . If you want to specify additional query parameters for the URL that will be created, you can also use an array as a route. The format of the array must be:

// generates: /index.php?r=site/index&param1=value1&param2=value2[‘site/index‘, ‘param1‘ => ‘value1‘, ‘param2‘ => ‘value2‘]

If you want to create a URL with anchor, you can use an # array with parameters. Like what:

// generates: /index.php?r=site/index&param1=value1#name[‘site/index‘, ‘param1‘ => ‘value1‘, ‘#‘ => ‘name‘]

A route can be both absolute and relative. An absolute route preceded by a backslash (such as: /site/index ), while a relative route does not (for example: site/index or index ). A relative route will be converted to an absolute route according to the following rules:

    • If the route is an empty string, the current yii\web\controller::route will be used as the route;
    • If the route does not have any slashes (for example index ), it will be considered an action ID of the current controller, and then the Yii\web\controller::uniqueid will be inserted in front of the route.
    • If the route does not have a leading slash (for example: site/index ), it is considered a route relative to the current module (modules) and will then insert the Yii\base\module::uniqueid in front of the route.

Starting with version 2.0.2, you can use alias to specify a route. In this case, alias will first be converted to the actual route and then converted to an absolute route according to the above rules.

Here are some examples of this approach:

/index.php?r=site/indexEcho Url::toroute (' Site/index ');/index.php?r=site/index&src=ref1#nameEcho Url::toroute ([ ' Site/index ',  ' src ' =  ' ref1 ', Span class= "hljs-string" > ' # ' =  ' name ']); ///index.php?r=post/edit&id=100 assume the alias "@postEdit" is defined as "Post/edit" Span class= "Hljs-keyword" >echo url::toroute ([ ' @postEdit ',  ' ID ' = = 100]); //http://www.example.com/index.php?r=site/indexecho URL:: Toroute ( ' Site/index ', true);  Https://www.example.com/index.php?r=site/indexecho Url::toroute (  ' https ');       

There is another method that Url::to() is very similar to Toroute (). The only difference between the two methods is that the former requires that a route be specified with an array. If the passed argument is a string, it will be used as a URL directly.

Url::to()The first parameter can be:

    • Array: Toroute () will be called to generate the URL. For example: [‘site/index‘] , [‘post/index‘, ‘page‘ => 2] . Please refer to toroute () for detailed usage.
    • String with leading: it will be treated as an @ alias, and the corresponding alias string will be returned.
    • Empty string: The URL of the current request will be returned;
    • Normal string: returns itself.

When $scheme specified (whether string or true), an absolute URL with host information (obtained via Yii\web\urlmanager::hostinfo) will be returned. If $url it is already an absolute URL, its protocol information will be replaced with the specified (HTTPS or HTTP).

Here are some examples of use:

/index.php?r=site/indexEcho Url::to ([' Site/index ']);/index.php?r=site/index&src=ref1#nameEcho Url::to ([' Site/index ',' src ' =' Ref1 ',' # ' =' name ']);/index.php?r=post/edit&id=100 assume the alias "@postEdit" is defined as "Post/edit"Echo Url::to ([ ' @postEdit ',  ' id ' = 100]); //the currently requested Urlecho url::to ();  /images/logo.gifecho url::to ( ' @web/images/logo.gif '); Span class= "hljs-comment" >//images/logo.gifecho url::to ( " Images/logo.gif '); //http://www.example.com/images/logo.gifecho Url::to ( Span class= "hljs-string" > ' @web/images/logo.gif ', true); //https://www.example.com/images/logo.gifecho Url::to ( Span class= "hljs-string" > ' @web/images/logo.gif ',      

Starting with version 2.0.3, you can use yii\helpers\url::current () to create a URL based on the current request routing and GET parameters. You can $params add or remove a get parameter by passing one to this method. For example:

// assume $_GET = [‘id‘ => 123, ‘src‘ => ‘google‘], current route is "post/view"// /index.php?r=post/view&id=123&src=googleecho Url::current();// /index.php?r=post/view&id=123echo Url::current([‘src‘ => null]);// /index.php?r=post/view&id=100&src=googleecho Url::current([‘id‘ => 100]);
Remember URLs?

Sometimes, you need to remember a URL and use it in subsequent request processing. You can accomplish this in the following ways:

// Remember current URL Url::remember();// Remember URL specified. See Url::to() for argument format.Url::remember([‘product/view‘, ‘id‘ => 42]);// Remember URL specified with a name givenUrl::remember([‘product/view‘, ‘id‘ => 42], ‘product‘);

In subsequent request processing, you can get the remembered URLs in the following ways:

$url = Url::previous();$productUrl = Url::previous(‘product‘);
Check relative URLs?

You can use the following code to detect whether a URL is relative (for example, contains the host information section).

$isRelative = Url::isRelative(‘test/it‘);

Yii URL Helper

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.