Google News
logo
Yii framework - Interview Questions
What is Extensions in Yii Framework?
Extensions are redistributable software packages specifically designed to be used in Yii applications and provide ready-to-use features. For example, the yiisoft/yii2-debug extension adds a handy debug toolbar at the bottom of every page in your application to help you more easily grasp how the pages are generated. You can use extensions to accelerate your development process. You can also package your code as extensions to share with other people your great work.
 
Info : We use the term "extension" to refer to Yii-specific software packages. For general purpose software packages that can be used without Yii, we will refer to them using the term "package" or "library".
 
Using Extensions : To use an extension, you need to install it first. Most extensions are distributed as Composer packages which can be installed by taking the following two simple steps:
 
modify the composer.json file of your application and specify which extensions (Composer packages) you want to install.
run composer install to install the specified extensions.
 
By default, Composer installs packages registered on Packagist - the biggest repository for open source Composer packages. You can look for extensions on Packagist. You may also create your own repository and configure Composer to use it. This is useful if you are developing private extensions that you want to share within your projects only.
 
Extensions installed by Composer are stored in the BasePath/vendor directory, where BasePath refers to the application's base path. Because Composer is a dependency manager, when it installs a package, it will also install all its dependent packages.
 
For example, to install the yiisoft/yii2-imagine extension, modify your composer.json like the following :
{
    // ...

    "require": {
        // ... other dependencies

        "yiisoft/yii2-imagine": "*"
    }
}
After the installation, you should see the directory yiisoft/yii2-imagine under BasePath/vendor. You should also see another directory imagine/imagine which contains the installed dependent package.
 
Info : The yiisoft/yii2-imagine is a core extension developed and maintained by the Yii developer team. All core extensions are hosted on Packagist and named like yiisoft/yii2-xyz, where xyz varies for different extensions.
 
Now you can use the installed extensions like they are part of your application. The following example shows how you can use the yii\imagine\Image class provided by the yiisoft/yii2-imagine extension:
use Yii;
use yii\imagine\Image;

// generate a thumbnail image
Image::thumbnail('@webroot/img/test-image.jpg', 120, 120)
    ->save(Yii::getAlias('@runtime/thumb-test-image.jpg'), ['quality' => 50]);
Advertisement