Yii 2.0 URL Creation

Yii 2.0 having the url manager to handle and create urls in different way. The URL manager is a built-in application component named urlManager. We can access this component in web and console application via \Yii::$app->urlManager and it is available in framework by default. We can use use yii\helpers\Url; namespace.

URL creation using Namespace In Yiiframework 2.0

Dont forget to add the below namespace

use yii\helpers\Url;

It will helpful to create simple URLs.

Url::base()
base URL of the current request.
/yiiframework2.0/project/backend/web
Url::home()
get home URL
/yiiframework2.0/project/backend/web/index.php
Url::to(”)
currently active route
/yiiframework2.0/project/backend/web/index.php
Url::to([‘page’,’id’=>’aboutus’])
currently active route
/yiiframework2.0/project/backend/web/index.php?r=site%2Fpage&id=aboutus
Url::toRoute(‘post/index’,’#’=>’tab2′)
Using # named parameter
/yiiframework2.0/project/backend/web/index.php?r=post%2Findex#tab2
Url::toRoute(‘post/index’)
same module, different controller and action
/yiiframework2.0/project/backend/web/index.php?r=post%2Findex
Yii::setAlias(‘@bsource’, ‘http://www.bsourcecode.com’);
echo Url::to(‘@bsource’);

get URL from alias
http://www.bsourcecode.com
Url::remember()
save URL to be used later
Url::previous()
get previously saved URL
/yiiframework2.0/project/backend/web/index.php?r=site%2Findex
Canonical()
currently executing action url.
Ignores the parameter values except action.
http://127.0.0.1/yiiframework2.0/project/backend/web/index.php?r=site%2Findex

Application urlManager And Request In Yiiframework 2.0

Yii::$app->basePath
Yii::$app->getBasePath()
D:\wamp\www\yiiframework2.0\project\backend
Yii::$app->homeUrl; /yiiframework2.0/project/backend/web/index.php
Yii::$app->getUrlManager()->createUrl(‘user’) /yiiframework2.0/project/backend/web/index.php?r=user
Yii::$app->urlManager->createUrl([‘site/page’, ‘id’ => ‘about’])
Creates an URL relative to the application root
/yiiframework2.0/project/backend/web/index.php?r=site%2Fpage&id=about
Yii::$app->urlManager->createUrl([‘site/view’, ‘id’ => 105]) /yiiframework2.0/project/backend/web/index.php?r=site%2Fview&id=105
Yii::$app->urlManager->createAbsoluteUrl(”)
Creates an URL prefixed with the proper protocol and hostname
http://127.0.0.1/yiiframework2.0/project/backend/web/index.php?r=
Yii::$app->urlManager->createAbsoluteUrl(‘site/view/index’) http://127.0.0.1/yiiframework2.0/project/backend/web/index.php?r=site%2Fview%2Findex
Yii::$app->request->baseUrl /yiiframework2.0/project/backend/web
Yii::$app->request->absoluteUrl
currently active route
http://127.0.0.1/yiiframework2.0/project/backend/web/index.php
Yii::$app->request->url
currently active route
/yiiframework2.0/project/backend/web/index.php
Yii::$app->request->queryParams Array
(
)
Yii::$app->request->queryString

Redirect URL, Login URL, Guest URL In Yiiframework 2.0

$url=Yii::$app->urlManager->createUrl([‘user/view’, ‘id’ => 1]);
Yii::$app->getResponse()->redirect($url);
Yii::$app->user->loginUrl Array
(
[0] => site/login
)
Yii::$app->user->isGuest

Get Request Method In Yiiframework 2.0

Whether to check the DELETE request, Ajax request, Get request etc, We can use the below methods to find the request type.

Yii::$app->request->method GET
Yii::$app->request->isAjax true or false
Yii::$app->request->isConsoleRequest true or false
Yii::$app->request->isDelete true or false
Yii::$app->request->isFlash true or false
Yii::$app->request->isGet true or false
Yii::$app->request->isHead true or false
Yii::$app->request->isOptions true or false
Yii::$app->request->isPatch true or false
Yii::$app->request->isPjax true or false
Yii::$app->request->isPost true or false
Yii::$app->request->isPut true or false
Yii::$app->request->isSecureConnection true or false

Server And User System Information In Yiiframework 2.0

Whether to check the host information, user agent, referrer details, server ip etc, We can use the below methods to find the request type.

Yii::$app->request->hostInfo http://127.0.0.1
Yii::$app->request->referrer http://127.0.0.1/yiiframework2.0/project/backend/web/index.php?r=site%2Findex
Yii::$app->request->userAgent Mozilla/5.0 (Windows NT 6.2; WOW64; rv:30.0) Gecko/20100101 Firefox/30.0
Yii::$app->request->userHost
Yii::$app->request->userIP 127.0.0.1
Yii::$app->request->securePort 443
Yii::$app->request->serverName 127.0.0.1
Yii::$app->request->serverPort 80

Current Controller, Module And Action In Yiiframework 2.0

Like Yii1.0, We are having option to get the current controller, action and module name. ‘basic’ is a default module in yii2.0. See the below code to get your current value

echo Yii::$app->controller->id;
echo Yii::$app->controller->action->id;
echo Yii::$app->controller->module->id;