Asked  1 Year ago    Answers:  5   Viewed   10 times

Here, I have extended User Identity of Yii2.

This is my configuration.

'user' => [
            'identityClass' => appmodelsUserMaster::class,
            'enableAutoLogin' => false,
            'loginUrl' => ['/auth/login'],
            'authTimeout' => 86400
        ],

Here, I have defined authTimout statically. But, What I want to do is that I want to fetch timeout value from database and set it in authTimeout.

Thanks.

 Answers

5

You can use event to set authTimeout before request will be handled:

'as beforeRequest' => [
    'class' => function (Event $event) {
        /* @var $app yiiwebApplication */
        $app = $event->sender;
        $app->getUser()->authTimeout = (new Query())
            ->select('value')
            ->from('{{%settings}}')
            ->where('name = :name', ['name' => 'authTimeout'])
            ->scalar($app->getDb());
    }
],

But probably more clear approach would be to create custom component and handle this in init().

class WebUser extends yiiwebUser {

    public function init() {
        parent::init();

        $this->authTimeout = (new Query())
            ->select('value')
            ->from('{{%settings}}')
            ->where('name = :name', ['name' => 'authTimeout'])
            ->scalar();
    }
}

Then use new component in your config:

'components' => [
    'user' => [
        'class' => WebUser::class,
        'identityClass' => appmodelsUserMaster::class,
        'enableAutoLogin' => false,
        'loginUrl' => ['/auth/login'],
    ],
    // ...
],
Saturday, May 29, 2021
 
4

I have tried this way in a simple ControllerAction and work right ...in var_dump($test) there the aspected result

public function actionView($id)
{
    $test =  $this->renderPartial('_form', [
        'model' => $this->findModel($id),
    ]);
    var_dump($test);

}
Saturday, May 29, 2021
 
2

I hope this could help you

 public function actionIndex()
 {
    $searchModel = new ContribuenteSearch();
    $dataProvider = $searchModel->search(Yii::$app->request->queryParams);

    $dataProvider->pagination->pageSize=15;

    return $this->render('index', [
        'searchModel' => $searchModel,
        'dataProvider' => $dataProvider,
    ]);
 }
Friday, August 13, 2021
 
bsd
 
bsd
4

try this variant of db.php to specify defaultSchema

return [
    'class' => 'yiidbConnection',
    'dsn' => 'pgsql:host=localhost;dbname=db_name', 
    'username' => 'db_username',
    'password' => 'db_password',
    'charset' => 'utf8',
    'schemaMap' => [
      'pgsql'=> [
        'class'=>'yiidbpgsqlSchema',
        'defaultSchema' => 'public' //specify your schema here
      ]
    ], // PostgreSQL
];
Sunday, August 15, 2021
 
5

You can use it this way: http://www.yiiframework.com/extension/yii2-helpers/

Create a folder /common/helpers/, then create a helper class there and add your static methods.

namespace commonhelpers;

class DateHelper 
{
    public static function dt(){
        return date('Y-m-d H:i:s');
    }
}

Usage

use commonhelpersDateHelper;

echo DateHelper::dt();
Wednesday, December 1, 2021
Only authorized users can answer the question. Please sign in first, or register a free account.
Not the answer you're looking for? Browse other questions tagged :