How to redirect users to their homepage after logging in

Hi, been stuck on this for 2 days now. and to be honest, i’m still new to Yii2. I have to know the framework since I’m the one who designs the frontend of our web app.

I have to create 2 home screens, one for non-registered users and the other is for the registered users.

I am able to display the home screen for non registered users. The one that can be found on


site/index

.

Now, the problem is that, I have to redirect the users who logs in to


app/index

.

Here’s the current code I have on the SiteController.


public function actionLogin()

{


    $this->layout='login-main';


    if (!Yii::$app->user->isGuest) {

        $this->layout='app-main';

        return $this->redirect( array('app/index') );

    }


    $model = new LoginForm();

    if ($model->load(Yii::$app->request->post()) && $model->login()) {

         $this->layout='app-main'; 

         return $this->redirect( array('app/index') );

    } else {

        return $this->render('login', [

            'model' => $model,

        ]);

    }

}

When this code is implemented, I get this error. (see attached)

7444

error.png

Can you let me know the right way on how to do this? I’m not sure if i’m doing it right.

It just looks like that ‘app/index’ is not found. Can you access ‘app/index’ manually? Or, in other words, have you implemented app controller and its index action?

And additionally it makes no sense specifying layout before redirection.

Thank you for your reply. I’ve created an index file on the app folder. Can’t access it manually too. How do I create an app controller? If I’ll create one, will the app folder recognize it?

Sorry, I’m not sure if what I’m asking is right. I’m just new to Yii2.

You will see "SiteController.php" in your "controllers" folder and find "actionIndex" method in it. It implements "site/index" route. So you need to write "AppController.php" and "actionIndex" method in it for your "app/index" route.

I think you dont have to use r=app/index

but http://server/app/index.php if app is a folder.

That might not be called Yii app any more. :D

@Jaegyo

Please read through the Definitive Guide, at the very least the first half of it (from “Getting Started” to “Displaying Data”). It may look a long way round, but it’s the fastest path. :)

If you don’t have any controllers beside the site controller then ‘app/index’ is essentially ‘site/index’ - unless you have explicitly created a controller called App.

And I guess you haven’t.

Thank you! This will definitely help.

Yes, I agree. and I haven’t added one. Thought that it might mess the sitecontroller of I add one. But thanks for commenting :)

Hmm, yeah I suppose. But I’m still working on it on my local computer, so this might not be a good one. But will definitely become useful if I’ll upload it online. Thanks!