Home » Php » php – Get current controller and action id in Yii

php – Get current controller and action id in Yii

Posted by: admin April 23, 2020 Leave a comment

Questions:

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
How to&Answers:

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

Yii::app()->urlManager->parseUrl(Yii::app()->request)

Answer:

Did you try:

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

and:

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

?

Answer:

As now in Yii2

get current controller name

Yii::$app->controller->id

current controller object

Yii::$app->controller

current action name:

Yii::$app->controller->action->id

current route:

Yii::$app->requestedRoute

Answer:

Using Yii2, obtain the current controller object with:

Yii::$app->controller

From the controller, obtain the current action as a string using:

Yii::$app->controller->action->id

Answer:

In Yii2:

The problem of calling Yii::$app->controller->id is that when you call it somewhere (example: in one of your top-level abstract controller), Yii::$app->controller might not be instantiated yet, so it will return error.

Just directly call urlManager to map request to route:

var_dump(Yii::$app->urlManager->parseRequest(Yii::$app->request))

Answer:

Try Yii::app()->controller->getRoute()

Answer:

If I get you question correctly, you are basically trying to stop access to certain actions in the controller from being accessed without being logged in right?

If this is what you are after, the correct method to do it is this :

  1. Make a actionMethod() in the controller like so :

    class SomeController extends CController{
    
      public function actionSomeAction(){
    
        ... More code...
    
    }
    
  2. After that, you can access the site using : path/to/application/controllerName/actionName

  3. Now if you want to force the user to log in before accessing the action, do this :

Make an access control like so :

 /**
     * @return array action filters
     */
    public function filters()
    {
        return array(
            'accessControl', // perform access control for CRUD operations
        );
    }

/**
 * Specifies the access control rules.
 * This method is used by the 'accessControl' filter.
 * @return array access control rules
 */
public function accessRules()
{
    return array(

        array('allow', // allow authenticated user to perform 'create' and 'update' actions
            'actions' => array('**yourActionMethodName**'),
            'users' => array('@'),
        ),

        array('deny', // deny all users
            'users' => array('*'),
        ),
    );
}

Now only authenticated users would be able to access the URL.

I hope it solved your problem.

If you simply want to check if the user is a guest and if he is, send him to the login page everytime:

In the config/main.php, add the following :

'defaultController' => 'controllerName/actionMethod',

And in that controller just add the above access rule. Now, by default you are opening the site to an access controlled method. So it would automatically redirect you to the login page.

Even another method :

Just add this in the views/layouts/main.php

<?php 

if(Yii::app()->user->isGuest)
{ 
   $this->redirect('/site/login');
}
?>

Answer:

if (Yii::$app->requestedAction->id == "index") {
    //do something
}