user7282
user7282

Reputation: 5196

Which folder to add custom helper functions to in Yii

I am trying to create a helper function in Yii 2. To which folder is the PHP file to be added to create a custom helper function in Yii 2 and how do I use it in controllers?

Upvotes: 19

Views: 15447

Answers (4)

Carlos M Gomez
Carlos M Gomez

Reputation: 85

I did this:

*CREATE an Helpers.php in (is a function without class, for that reason is a helper):

/components

p.e. I use a function to debug:

<?php
namespace app\components\Helpers;
// +-----------------------------------------------
// | @author          : Carlos Gomez
// | @created_at      : 18/03/2022
// | @updated_by      : 
// | @Version (branch): FEAT-001-2022
// +-----------------------------------------------
// Add ___ function.
// +---------------------------


/**
 * ___
 *
 * @param  mixed $xVariable
 * @param  mixed $bBreakPoint
 * @param  mixed $bDumpMode
 * @param  mixed $type
 * @param  mixed $line
 * @return void
 */
function ___($xVariable, $bBreakPoint = true, $bDumpMode = false, $type = '000', $line = 0) {
    echo "<pre>";
        if (!$bDumpMode) {
            print_r($xVariable);
        }
        else {
            var_dump($xVariable);
        }
    echo '</pre>';

    if ($bBreakPoint) {
        exit($line);
    }
}

2.- *ADD the autoload in composer.json with the path of the helpers php file.

...

"autoload":{
        "files": [
            "components/Helpers.php"
        ]
    }
...
  1. *RUN in terminal (In your current project folder):
composer dump-autoload
  1. *IMPLEMENT the function using "use" in the file that you need:
use function app\components\Helpers\___; //-> After namespace obviously

...

___( $variableToDebug ); //-> In any part of your code when you need to use the function.
I hope this can be useful to anybody.

Upvotes: 0

johnsnails
johnsnails

Reputation: 2031

To create some static helpers do the following (these instructions are for the 'Advanced Yii2 Template'. Create a folder under common called components. Inside there create a class called something like: MyHelpers (filename MyHelpers.php).

<?php
namespace common\components;
// namespace app\components; // For Yii2 Basic (app folder won't actually exist)
class MyHelpers
{
    public static function hello($name) {
        return "Hello $name";
    }
}

Don't forget to include it in your controller etc that you would like to use it in. use common\components\MyHelpers; // use app\components\MyHelpers; // For Yii2 Basic (app folder won't actually exist)

And to use it: MyHelpers::hello("John");

Upvotes: 12

Mihai P.
Mihai P.

Reputation: 9357

You can put it in the components folder. Afterwards use the namespace to access it. For example

use app\components\Helper;

and in your code

Helper::something();

Make the helper functions static functions.

Upvotes: 25

Sepuka
Sepuka

Reputation: 54

For example, models->helpers->SomeHelper.php In your controller this helper will access like \models\helpers\SomeHelper.php

Upvotes: -1

Related Questions