32

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
Hilarius L. Doren
  • 757
  • 1
  • 12
  • 29
Hamid Ghorashi
  • 1,003
  • 3
  • 14
  • 29
  • 3
    For **Yii2** it is similar: `Yii::$app->controller->id`, `Yii::$app->controller->action->id` – d.raev May 27 '16 at 09:29

8 Answers8

51

Did you try:

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

and:

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

?

Sergi Juanola
  • 6,531
  • 8
  • 56
  • 93
48

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

Yii::app()->urlManager->parseUrl(Yii::app()->request)
trejder
  • 17,148
  • 27
  • 124
  • 216
eXtreme
  • 866
  • 6
  • 9
  • 1
    Just a note, this doesn't work when the index page is loaded, i.e `http://localhost/yiiapp/` as there is no request in the URL. @korcholis's is the best way to make it work for all actions, whether index or not. – echo_salik Jan 16 '15 at 12:00
  • 2
    Simple and fast : Yii::$app->requestedRoute – Maksim Tikhonov Jun 29 '17 at 22:54
22

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
Harry B
  • 2,864
  • 1
  • 24
  • 44
Kuldeep Dangi
  • 4,126
  • 5
  • 33
  • 56
12

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
CheckeredFlag
  • 121
  • 1
  • 4
8

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))
Jinsong Li
  • 6,347
  • 1
  • 23
  • 20
5

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

CreatoR
  • 1,654
  • 10
  • 14
4

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');
}
?>
GAMITG
  • 3,810
  • 7
  • 32
  • 51
Sankalp Singha
  • 4,461
  • 5
  • 39
  • 58
  • Thank you so much, but the I don't want to write access rules for every controller. I just want to make a check if the user is guest and then redirect it to the login page. But other than the scenario I explained above, I am trying to find find out how to get access to the controller and action ID while using urlManager to rewrite the URL. – Hamid Ghorashi Oct 15 '13 at 09:18
  • I have edited my post. Maybe it should help you now. If it does, please dont forget to upvote and rate it as the answer :) – Sankalp Singha Oct 15 '13 at 09:27
  • Thank you very much. I got my answer. – Hamid Ghorashi Oct 15 '13 at 11:13
0
if (Yii::$app->requestedAction->id == "index") {
    //do something
}
sergey
  • 35
  • 6
  • 3
    Please refrain from answering with just code and explain how your answer differs from the (long) existing other answers. – Tobi Nary Jan 25 '16 at 10:57