[Yii2] How to create and use Functions on Yii2

bardimin pic

Written by Bardimin

On September 12, 2021
Home » Blogs » Technology » [Yii2] How to create and use Functions on Yii2

A function is an organized and reusable block of code that is used to perform one related action or achieve a specific result.

In creating applications using Yii2, often we need a function to simplify and speed up the creation of the application we are creating.

Well… Now how to make the function we create we can access in view or controller. In this article, we will learn how to create and use functions on Yii2 basic and advanced templates.

The steps of creating and using functions on Yii2 are basic and advanced templates.

  1. Create a with the name “components”. For Yii2 basic create the in root and on Yii2 advanced create that folder in the “common” folder.
yii2 function 01
  1. Create a“MyFunction.php”file in the components folder, and write the function as follows:
namespace commoncomponents; //Yii2 Advanced template
namespace appcomponents;    //Yii2 basic template
 
use yiibaseComponent;
 
class MyFunction extends Component
{
    public function hello()
    {
        ......
        return "Hello, World!";
    }
}
}
  1. In Yii2 Advanced, edit the file“common/config/main-local.php”and add the following code:
return [
    'components' => [
        ...............
        'MyFunction'=>[
            'class' =>'commoncomponentsMyFunction'
        ],
        ...........
    ],
];

While on Yii2 Basic, edit the file“config/web.php” and copy the following code

...........
$config = [
    ............
    'components' => [
        ............
        'MyFunction'=>[
            'class' =>'appcomponentsMyFunction',
        ],
    ],
    ..............
];
...........
  1. To use the function that you have created is enough to call it by
Yii::$app->MyFunction->hello())

You can call the function on both Controller and View.

Good luck…………… Good luck… …

Latest Articles

Basic Computer and Laptop Maintenance Techniques

Basic Computer and Laptop Maintenance Techniques

You may not realize it, but computers and cars have something in common, both requiring regular maintenance. If your car needs regular oil changes, your computer should also regularly update its software, keep its antivirus up to date, and check for spyware....

The desktop version of ChatGPT for Windows, Linux, and Mac

The desktop version of ChatGPT for Windows, Linux, and Mac

Do you know what ChatGPT is? Do you know how to get and install ChatGPT on your device? ChatGPT is fast becoming one of the most important inventions in the world of natural language processing. You can use it to generate human-like responses based on your input. You...

24 Pinout Voltage at ATX Power Supply to the Motherboard

24 Pinout Voltage at ATX Power Supply to the Motherboard

The Power Supply converts alternating current (AC) power into low-voltage controlled direct current (DC). Some Power Supply devices include a choice of manual input voltages, while others automatically adjust. The Power Supply converts the voltage from the power line...

Google Chrome – Open Bookmarks to a New Tab By Default

Google Chrome – Open Bookmarks to a New Tab By Default

Bookmarks are shortcuts to open website pages that you have saved for you to visit again later. Have you ever visited a website page that is quite interesting and you want to visit it again later? Considering the address of a website page is certainly difficult,...