yii1tech/session-dummy

Provides support for Yii1 application runtime configuration

1.0.1 2023-06-14 15:23 UTC

This package is auto-updated.

Last update: 2024-04-14 17:10:50 UTC


README

134691944

Dummy Session Extension for Yii 1


This extension provides a mock for the standard Yii session, which avoids direct operations over PHP standard session.

For license information check the LICENSE-file.

Latest Stable Version Total Downloads Build Status

Installation

The preferred way to install this extension is through composer.

Either run

php composer.phar require --prefer-dist yii1tech/session-dummy

or add

"yii1tech/session-dummy": "*"

to the "require" section of your composer.json.

Usage

This extension provides a mock for the standard Yii session, which avoids direct operations over PHP standard session. It introduces \yii1tech\session\dummy\DummySession class, which does not actually store session data anywhere, except current process's memory, and avoid sending any headers to HTTP response.

This class is useful while writing unit tests, as it avoids sending headers and cookies to the StdOut.

Application configuration example:

<?php

return [
    'name' => 'Test Application',
    'components' => [
        'session' => [
            'class' => yii1tech\session\dummy\DummySession::class,
        ],
        // ...
    ],
    // ...
];

This extension may also come in handy in API development. For example: if you need to authenticate user via OAuth token, but keep tracking him in the code using \CWebUser abstraction. In this case you may switch session component "on the fly". For example:

<?php

namespace app\web\controllers;

use app\oauth\AuthUserByTokenFilter;
use CController;
use Yii;
use yii1tech\session\dummy\DummySession;

class ApiController extends CController
{
    public function init()
    {
        parent::init();
        
        Yii::app()->setComponent('session', new DummySession(), false); // mock session, so it does not send any Cookies to the API client
    }
    
    public function filters()
    {
        return [
            AuthUserByTokenFilter::class, // use custom identity to authenticate user via OAuth token inside {@see CWebUser}
            'accessControl', // now we can freely use standard "access control" filter and other features
        ];
    }
    
    public function accessRules()
    {
        return [
            // ...
        ];
    }
    
    // ...
}