Force Redirect For Users Of Certain Status

Hello everyone,

So I’m trying to figure out how to create a force redirect for logged in users who have not yet confirmed their email address. I found this searching the web:

But this is written for Yii 1.1. How can I achieve something similar in Yii 2.0? I know I can add a check to every single page that a user needs to be logged in to view, but I imagine this would be cumbersome and very easy to forget.

Some help would greatly be appreciated.

I’d create a BaseController and extend from that.

Could you please explain a little further? I’m very new to all this. I think I somewhat understand what you’re saying, but even so, I wouldn’t know how to implement it.

And let me take a stab at what you mean. Are you saying to create another controller ("BaseController") and then have "SiteController" extend from "BaseController"? Then have the forced redirect in the "BaseController"?

If that’s the case, how would I call functions from “SiteController”?

Just guessing here…

Yes, that’s exactly right. Something simple like app\controllers\BaseController and then class SiteController extends BaseController. Then you can override BaseController::init() to add the check.

Not sure what you mean by your last question…

Sorry about that, I’ve corrected it, meant to say “call”.

Another ‘quick’ question, what exactly do you mean “override BaseController::init() to add the check”?

I’m looking at SiteController now and don’t see any “init()”.

Sorry if my questions are rather rudimentary, still trying to piece it all together.

Oh and thank you for helping me so far, I really appreciate it!

You need to read the API documentation for yii\web\Controller.

You can also setup access rules within the controller to achieve your needs.

Hi Kartik,

Thanks for the link, but I’m still not clear on what exactly I would need to do. When I look in yii\base\object.php, init() is empty. Not sure what I would have to do.

As for the access rules, I played with this for a while but ultimately didn’t get anywhere. Seemed to be that I would have to create a rule for every single action EXCEPT the redirect.

Unless I understand it improperly or implemented it wrong, I don’t see access rules being a viable method to achieve this.

Thanks again for taking the time to help me!

Object::init() is sort of a secondary contructor that Yii uses. It is called after a new object is initialized and its params set. So yes, by default it’s empty.

All you need to do it put your logic in there.





class BaseController extends \yii\web\Controller

{

    public function init()

    {

        // if user has not confirmed email, then redirect

    }

}




On another note, I’m not sure you should be using frameworks yet. It seems like you still have a bit to learn of basic PHP (ie, OOP), so I’d recommend spending some time just writing your own custom code from scratch.

Yes, I know I am diving right into the deep end, however, I’m up for the challenge, just need some assistance to get going.

I’ve gone through the tutorial on OOP on the PHP website and I think I have a decent grasp of it. Still, putting everything together is a bit of challenge. I have absorbed so much information in a very short amount of time and it’s rather jumbled and unorganized (I’ve read both guides of Yii, 1.1 and 2.0, all of w3schools, etc.). And I have written my own code. I created my own login page and a couple javascript driven pages, but neither using OOP (didn’t really know what OOP was until I started working with Yii).

I REALLY like Yii. I’ve already tweaked a few things to my liking and I’d really like to continue on this path.

Anyway, getting back on topic. I did what was suggested and modified a few things but I need to understand/fix/address something before going further.

I created a BaseController and extended SiteController from it as previously suggested. And I added this piece of code to it.


    public function init()

    {

        if (Yii::$app->user->userStatus) {

            return $this->render('about');

        }

    }



Very simple piece, that doesn’t do much on its own. You may notice the “userStatus” function there. I added these lines to yii\web\user.php.


    public function getUserStatus(){


        return TRUE;

    }

As you can see, nothing too special here. I’m just trying to get the redirect to work.

Let’s say I click on the homepage link, what’s happening is that (from logs), “about” IS being rendered, but then, immediately afterwards, SiteController is invoked and the index is rendered. I’m obviously missing something here, but I’m not sure what.

Some help would be appreciated.