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?
4 Answers
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.

- 9,307
- 3
- 38
- 49
-
5There was no folder called components in basic template. So I just created a folder called helpers in app and followed your way and it worked. – user7282 Dec 02 '14 at 05:04
-
1you got the idea yes, just make it to suit how you want it to work. – Mihai P. Dec 02 '14 at 06:15
-
@MihaiP. Good answer, why should the methods be static though. I'm just going to write some of my own and not sure why you say this? – Jonnny Jul 15 '16 at 10:34
-
@Jonnny they do not have to be. They can be however you want/need. – Mihai P. Jul 16 '16 at 23:06
-
@MihaiP What is the pros and cons to them being static in this case? – Jonnny Jul 16 '16 at 23:07
-
@Jonnny Try to use static methods for very, very simple things. – Mihai P. Jul 21 '16 at 05:00
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");

- 1,971
- 20
- 29
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"
]
}
...
- *RUN in terminal (In your current project folder):
composer dump-autoload
- *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.

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

- 44
- 4