yii2 常用url

 超哥  Yii2  2020-04-02  885  发表评论

不要忘记添加以下命名空间

use yii\helpers\Url;

有助于创建简单的URL

Url::base()
base URL of the current request.
/testapp/backend/web
Url::home()
get home URL
/testapp/backend/web/index.php
Url::to("")
currently active route
/testapp/backend/web/index.php
Url::to(['page','id'=>'aboutus'])
currently active route
/testapp/backend/web/index.php?r=site/page&id=aboutus
Url::toRoute('post/index','#'=>'tab2′)
Using # named parameter
/testapp/backend/web/index.php?r=post/index#tab2
Url::toRoute('post/index')
same module, different controller and action
/testapp/backend/web/index.php?r=post/index
Yii::setAlias('@bsource', 'http://www.bagesoft.cn');
echo Url::to('@bsource');

get URL from alias
http://www.bagesoft.cn
Url::remember()
save URL to be used later
Url::previous()
get previously saved URL
/testapp/backend/web/index.php?r=site/index
Canonical()
currently executing action url.
Ignores the parameter values except action.
http://127.0.0.1/testapp/backend/web/index.php?r=site/index

Application urlManager And Request In Yiiframework 2.0

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

重定向URL,登录URL,访客URL



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

获取请求方法

DELETE请求,Ajax请求,Get请求等,我们可以使用下面的方法来判断请求类型。

Yii::$app->request->methodGET
Yii::$app->request->isAjaxtrue or false
Yii::$app->request->isConsoleRequesttrue or false
Yii::$app->request->isDeletetrue or false
Yii::$app->request->isFlashtrue or false
Yii::$app->request->isGettrue or false
Yii::$app->request->isHeadtrue or false
Yii::$app->request->isOptionstrue or false
Yii::$app->request->isPatchtrue or false
Yii::$app->request->isPjaxtrue or false
Yii::$app->request->isPosttrue or false
Yii::$app->request->isPuttrue or false
Yii::$app->request->isSecureConnectiontrue or false

服务器和用户系统信息

检查主机信息,用户代理,引用者详细信息,服务器ip等,我们可以使用下面的方法来获取请求信息



Yii::$app->request->hostInfohttp://127.0.0.1
Yii::$app->request->referrerhttp://127.0.0.1/testapp/backend/web/index.php?r=site/index
Yii::$app->request->userAgentMozilla/5.0 (Windows NT 6.2; WOW64; rv:30.0) Gecko/20100101 Firefox/30.0
Yii::$app->request->userHost
Yii::$app->request->userIP127.0.0.1
Yii::$app->request->securePort443
Yii::$app->request->serverName127.0.0.1
Yii::$app->request->serverPort80


当前控制器、模块和操作

echo Yii::$app->controller->id;
echo Yii::$app->controller->action->id;
echo Yii::$app->controller->module->id;
所有评论
加载评论 ...
发表评论