不要忘记添加以下命名空间
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->queryParams | Array ( ) |
Yii::$app->request->queryString |
重定向URL,登录URL,访客URL
$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 | – |
获取请求方法
DELETE请求,Ajax请求,Get请求等,我们可以使用下面的方法来判断请求类型。
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 |
服务器和用户系统信息
检查主机信息,用户代理,引用者详细信息,服务器ip等,我们可以使用下面的方法来获取请求信息
Yii::$app->request->hostInfo | http://127.0.0.1 |
Yii::$app->request->referrer | http://127.0.0.1/testapp/backend/web/index.php?r=site/index |
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 |
当前控制器、模块和操作
echo Yii::$app->controller->id;
echo Yii::$app->controller->action->id;
echo Yii::$app->controller->module->id;
所有评论
加载评论 ...
发表评论