How To Create [Private] Helper Class

Can someone provide a simple, complete & authoritative answer to this question.

For clarity…

  • The class is not for distribution, it will only be used within this application

  • I’d prefer NOT to use a “quick and dirty hack”, but I’d like the simplest method for doing this

so for the sake of this question let’s assume this is the entire contents of the class




namespace fred\helpers;


Class Eric {

	public static function displayEric() {

		return "Eric";

	}

}



use of the helper would then be (I assume) as follows:




use fred\helpers\Eric;

echo Eric::displayEric();



so -

  1. where SHOULD this file be saved

  2. what other files need to be updated, and explicitly what should they be updated with.

Many thanks in advance

Hi there. What I did was creating a components folder and place my helper file in it. Now, assuming that you use the “Advance Application Template” which has both ‘frontend’ and ‘backend’, it would be place inside appname\common\components. This would make the helper available in both ‘frontend’ and ‘backend’.

Your helper namespace should be like this:-

appname/common/components/helpers.php


<?php

namespace common\components;


class Helpers

{

    public static function random()

    {

        return rand(1,5);

    }

    

}

?>

Then in your main-local.php, you need to load your helper class.

appname/common/config/main-local.php


<?php

return [

    'components' => [

        'Helpers' => [

            'class' => 'common\components\helpers',

        ],

    ],

];

?>

Whenever you want to use your helper:

appname/frontend/controllers/PostController.php


<?php

$randomNumber = Yii::$app->Helpers->random();

?>

This way, you don’t have to load your helper separately in model, controller or view each time you want to use it.

Let me know if this helps :D

Thanks for your response.

Unfortunately I didn’t want the class autoloaded - its only used in one section of the application and autoloading felt a bit like a sledgehammer to crack a nut.

In the end I’ve simply stored it in a sub-folder of the ‘vendors’ folder:

\vendors\myorganisation\helpers\Fred.php

containing:




<?php

namespace vendor\myorganisation\helpers;

Class Fred {

//.... various static methods

}



I then use this where required:




use vendor\myorganisation\helpers\Fred;



If you’re taking about the ‘right’ things, then place your custom helpers in helpers directory of your application (not vendor):

./helpers/

./helpers/Formatter.php

your helper class:




<?php


namespace app\helpers;


/**

 * PHPDoc

 */

class Formatter

{

    /**

     * PHPdoc

     */

    public static function displayEric()

    {

    }

}




And in your code use:




<?php


use app\helpers\Formatter;


Formatter::displayEric();




And place all your future helpers in that directory.

So as result you’ll have:




use yii\helpers\ArrayHelper; // Yii2 core helper

use app\helpers\Formatter;   // Your custom helper



what’s pretty easy to understand, organize and remember.