Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Get current controller and action id in Yii

Tags:

php

yii

yii1.x

I want to force all users to log in before accessing pages of my site. I have followed Larry Ullman's tutorial Forcing Login for All Pages in Yii.

According to the tutorial you can make an exception for some pages to avoid redirecting to the log in page. In order to check the current controller it has checked $_GET value. My problem is that I have used urlManager to rewrite the URL and $_GET gives me a null value. Is there any method I can use to get the current controller and action in the score of my class?

I tried the following but it is not accessible in the scope of my component class:

Yii::app()->controller->getId 
like image 250
Hamid Ghorashi Avatar asked Oct 15 '13 08:10

Hamid Ghorashi


People also ask

What is controller in Yii?

Advertisements. Controllers in web applications should extend from yii\web\Controller or its child classes. In console applications, they should extend from yii\console\Controller or its child classes. Let us create an example controller in the controllers folder.

What is Yii ::$ app?

Applications are objects that govern the overall structure and lifecycle of Yii application systems. Each Yii application system contains a single application object which is created in the entry script and is globally accessible through the expression \Yii::$app .


2 Answers

Did you try:

Yii::app()->controller->id 

and:

Yii::app()->controller->action->id 

?

like image 150
Sergi Juanola Avatar answered Sep 21 '22 01:09

Sergi Juanola


Yes you can get the current controller/action route, by reversing urlManager rule:

Yii::app()->urlManager->parseUrl(Yii::app()->request) 
like image 36
eXtreme Avatar answered Sep 25 '22 01:09

eXtreme