manchenkov/yii2-helpers

Useful short helper functions to work with Yii components

1.1.0 2021-02-25 00:49 UTC

This package is auto-updated.

Last update: 2022-06-25 03:50:09 UTC


README

Useful short helper functions to work with default Yii 2 components.

Installation

You have to run the following command to add a dependency to your project

composer require manchenkov/yii2-helpers

or you can add this line to require section of composer.json

"manchenkov/yii2-helpers": "^1.1.0"

Usage

After installation, you may use it in existing code like this:

use yii\web\Controller;

class SiteController extends Controller 
{
    public function actionIndex(): string
    {
        if (app()->user->isGuest) {
            return view('guests');
        }
        
        return view('main');
    }
    
    // ...
    
    public function actionAjaxGetData(): string
    {
        if (request()->isAjax) {
            $number = request()->get('number');
            
            // ...
        }
        
        return view('main');
    }
}

Functions

Function Description
app() returns Yii::$app object
url() instead of yii\helpers\Url::to()
request() returns Yii::$app->request object
response() returns Yii::$app->response object
debug() instead of Yii::debug()
e() instead of yii\helpers\Html::encode()
t() instead of Yii::t()
formatter() instead of Yii::$app->formatter
config() returns some parameter from Yii::$app->params by key or set if value passed
cookies() returns a cookie from request or set new value to response
view() calls render() method of current controller (example: return view('index', ['list' => $list]) instead of return $this->render('index', ['list' => $list]))
alias() get or set alias value (instead of Yii::getAlias() and Yii::setAlias())