Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to get current router in Yii Framework?

Tags:

php

yii

I am a Zend Framework [version 1] developer, now I am trying to learn the Yii Framework.

How do I get the current router in Yii Framework?

like image 977
vietean Avatar asked Dec 02 '22 23:12

vietean


2 Answers

For an example URL of http://site.ru/forum/index?var=val:

  • Yii::app()->getRequest()->getQueryString() will return var=val,

  • Yii::app()->getRequest()->getPathInfo() will return forum/index,

  • Yii::app()->getRequest()->getBaseUrl() will return /forum,

  • Yii::app()->getRequest()->getUrl() will return forum/index?var=val,

  • Yii::app()->getRequest()->getHostInfo() will return http://site.ru,

  • Yii::app()->getRequest()->getRequestUri() will return forum/index?var=val.

like image 140
davetoxa Avatar answered Dec 05 '22 13:12

davetoxa


In case you were after the route, not path related information: in most view files (and the layout) you can use $this->route and if you're not in the right context for that to be available view $this then you can often use Yii::app()->controller->route.

Both return the current matching route from the url manager. Something like:

site/index

site/error

user/edit

Additionally, to just get the current controller in use you can use $this->id to return something like site or user

like image 29
Harry B Avatar answered Dec 05 '22 14:12

Harry B