PHPFixing
  • Privacy Policy
  • TOS
  • Ask Question
  • Contact Us
  • Home
  • PHP
  • Programming
  • SQL Injection
  • Web3.0

Sunday, June 26, 2022

[FIXED] How to set separate homeUrl for authenticated users and guests in yii2

 June 26, 2022     yii2, yii2-advanced-app, yii2-user     No comments   

Issue

I'm wondering is it possible to make different homeUrl for authenticated and guest users? I have such rules in SiteController.php

public function behaviors()
{
    return [
        'access' => [
            'class' => AccessControl::className(),
            'only' => ['logout', 'signup'],
            'rules' => [
                [
                    'actions' => ['signup'],
                    'allow' => true,
                    'roles' => ['?'],
                    'denyCallback' => function() {
                        return $this->redirect('/account');
                    }
                ],
                [
                    'actions' => ['logout', 'condition'],
                    'allow' => true,
                    'roles' => ['@'],
                ],
            ],
        ],
        'verbs' => [
            'class' => VerbFilter::className(),
            'actions' => [
                'logout' => ['post'],
            ],
        ],
    ];
}

Solution

That shouldn't be much of a problem. My answer is structured as follows:

  1. Config for guest users
  2. Config for logged in users (overwriting)
  3. Alternatives

1. Config for guest users

This one is easy. Simply set the home URL to whatever you want your guest visitors to land on. Put this in your config:

//...
'homeUrl'=>'site/home-guests',
//...

2. Config for logged in users (overwriting)

A login happens on each and every page-load either via form submission, cookie (remember me), session or access token. You won't notice this since it happens automatically except for the actual form-login.

The yii\web\User-class has an event which will be triggered after every login. Its called yii\web\User::EVENT_AFTER_LOGIN. You can find the doc for the triggering method here.

To fulfill your requirement simply extend the user class and within the init()-method attach an event handler. If the event mentioned gets thrown, change the home URL. Done!

Heres the code of the custom user class:

class User extends \yii\web\User {
    //...
    public function init() {
        parent::init();

        //listen to after login event
        $this->on(static::EVENT_AFTER_LOGIN, function ($event) {
            Yii::$app->setHomeUrl(Url::to(['site/home-logged-in']));
        });
    }
    //...
}

For Yii to use your custom and extended class, simply tell it to within the config:

//...
'user'=>[
    'class'=>'app\components\User',    
],    
//...

3. Alternatives

If it's not about the URL but simply to show the users different contents depending on their login-status you have other / easier options:

  • show a different view when logged in but use the same action
  • redirect them within the action method
  • if/else within the actual home-view

Which one is right depends on your detailed requirement. This is difficult to tell with the information you provided.

Tell me if you have further questions. I'll be happy to help!



Answered By - PLM57
Answer Checked By - Terry (PHPFixing Volunteer)
  • Share This:  
  •  Facebook
  •  Twitter
  •  Stumble
  •  Digg
Newer Post Older Post Home
View mobile version

0 Comments:

Post a Comment

Note: Only a member of this blog may post a comment.

Total Pageviews

Featured Post

Why Learn PHP Programming

Why Learn PHP Programming A widely-used open source scripting language PHP is one of the most popular programming languages in the world. It...

Subscribe To

Posts
Atom
Posts
Comments
Atom
Comments

Copyright © PHPFixing