Learn to create Extensions in yii2 framework

So guyz , in this tutorial you will learn how to create Extensions in yii2 framework

Our plan is as follows:

We will develop our extension in a folder, which is different from our example CRM application.

This extension will be named yii2-malicious, to be consistent with the naming of other Yii 2 extensions.

Given the kind of payload we saw earlier, our extension will consist of a single controller and some special wiring code (which we haven’t learned about yet) to automatically attach this controller to the application.

Finally, to consider this subproject a true Yii 2 extension and not just some random library, we want it to be installable in the same way as other Yii 2 extensions.

Preparing the boilerplate code for the extension

Let’s make a separate directory, initialize the Git repository there, and add the AppInfoController to it. In the bash command line, it can be achieved by the following commands:

$ mkdir yii2-malicious && cd $_
$ git init
$ > AppInfoController.php

Inside the AppInfoController.php file, we’ll write the usual boilerplate code for the Yii 2 controller as follows:

namespace malicious;
use yii\web\Controller;
class AppInfoController extends Controller
{
    // Action here
}

Put the action defined in the preceding code snippet inside this controller and we’re done with it. Note the namespace: it is not the same as the folder this controller is in, and this is not according to our usual auto-loading rules.

Now this controller needs to be wired to the application somehow. We already know that the application has a special property called controllerMap, in which we can manually attach controller classes. However, how do we do this automatically, better yet, right at the application startup time? Yii 2 has a special feature called bootstrapping to support exactly this: to attach some activity at the beginning of the application lifetime, though not at the very beginning but before handling the request for sure. This feature is tightly related to the extensions concept in Yii 2, so it’s a perfect time to explain it.

FEATURE – bootstrapping

To explain the bootstrapping concept in short, you can declare some components of the application in the \yii\base\Application::$bootstrap property. They’ll be properly instantiated at the start of the application. If any of these components implement the BootstrapInterface interface, its bootstrap() method will be called, so you’ll get the application initialization enhancement for free. Let’s elaborate on this.

The \yii\base\Application::$bootstrap property holds the array of generic values that you tell the framework to initialize beforehand. It’s basically an improvement over the preload concept from Yii 1.x. You can specify four kinds of values to initialize as follows:

  • The ID of an application component
  • The ID of some module
  • A class name
  • A configuration array

If it’s the ID of a component, this component is fully initialized. If it’s the ID of a module, this module is fully initialized. It matters greatly because Yii 2 has lazy loading employed on the components and modules system, and they are usually initialized only when explicitly referenced. Being bootstrapped means to them that their initialization, regardless of whether it’s slow or resource-consuming, always happens, and happens always at the start of the application.

Tip

If you have a component and a module with identical IDs, then the component will be initialized and the module will not be initialized!

If the value being mentioned in the bootstrap property is a class name or configuration array, then the instance of the class in question is created using the \yii\BaseYii::createObject() facility. The instance created will be thrown away immediately if it doesn’t implement the \yii\base\BootstrapInterface interface. If it does, its bootstrap() method will be called. Then, the object will be thrown away.

So, what’s the effect of this bootstrapping feature? We already used this feature while installing the debug extension. We had to bootstrap the debug module using its ID, for it to be able to attach the event handler so that we would get the debug toolbar at the bottom of each page of our web application. This feature is indispensable if you need to be sure that some activity will always take place at the start of the application lifetime.

The BootstrapInterface interface is basically the incarnation of a command pattern. By implementing this interface, we gain the ability to attach any activity, not necessarily bound to the component or module, to the application initialization.



Pin It on Pinterest

Shares

Get the best in web dev

Join dunebook.com and recieve best in web dev , once a week FREE

An email has been Sent to your Inbox ! Please Confirm your Subscription :)