M
M
ms23452016-01-04 10:57:54
Yii
ms2345, 2016-01-04 10:57:54

How to connect your functions in Yii2?

Good time, there was a need to connect my functions in the yii 2.0.6 framework so that they can be used in views.
Can you please tell me how to connect them? Or implement them through methods?

Answer the question

In order to leave comments, you need to log in

3 answer(s)
A
Alexander N++, 2016-01-04
@sanchezzzhak

Create static helper classes
Create a class in a folder for example helper
set namespace app\helpers
your class will be available like this
app\helpers\ClassName::Method()
To connect functions and other stuff via BootstrapInterface
Create a class with yii\base\BootstrapInterface interface

namespace app\extensions\components;
use yii\base\Application;
use yii\base\BootstrapInterface;
class AppBootstrap implements BootstrapInterface
{
    /**
    * Bootstrap method to be called during application bootstrap stage.
    * @param Application $app the application currently running
    */
    public function bootstrap(Application $app)
    {
                    // Подключаем файлик с функциями
    }
}

In config
'bootstrap' => [
    'app\extensions\components\AppBootstrap',
],

Bootstraps are needed to override or set the $app->set config (within a module or globally) before starting the initialization of the application / individual modules

E
Egor, 2016-01-04
@want2know

Using global functions

D
Dmitry Bay, 2016-01-04
@kawabanga

I have a similar answer to Alexander N++ , but usually there is no need to include it as a class in the application.
My main helper class is located at app/components/Helper.php and has all the static functions I need.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question