How you can use the widgets in Yii Framework

In this tutorial I will explain to you how you can use the widgets in Yii Framework for your project.

As you might know, at you can find a lot of free widgets that you can use for your projects. If you want to use an Yii Framework's widget in your project, you must first download it manually and copy it in your  nameofyourproject\vendor\yiisoft\yii2\widgets folder. Also, you can download it by using the composer.phar file or composer.json.

Next you must add a code in your view file to display the widget. If you want to display the standart Yii2's Menu widget, you must only add this code in your views file:


use yii/widgets/Menu;

echo Menu::widget([
'items' => [
// Important: you need to specify url as 'controller/action',
// not just as 'controller' even if default action is used.
['label' => 'Home', 'url' => ['site/index']],
// 'Products' menu item will be selected as long as the route is 'product/index'
['label' => 'Products', 'url' => ['product/index'], 'items' => [
['label' => 'New Arrivals', 'url' => ['product/index', 'tag' => 'new']],
['label' => 'Most Popular', 'url' => ['product/index', 'tag' => 'popular']],
['label' => 'Login', 'url' => ['site/login'], 'visible' => Yii::$app->user->isGuest],

If you want to use the Breadcrumbs widget, you must add this code:


use yii/widgets/Breadcrumbs;

// $this is the view object currently being used
echo Breadcrumbs::widget([
'itemTemplate' => "

  • {link}


\n", // template for all links
'links' => [
'label' => 'Post Category',
'url' => ['post-category/view', 'id' => 10],
'template' => "

  • {link}


\n", // template for this link only
['label' => 'Sample Post', 'url' => ['post/edit', 'id' => 1]],

Video tutorial:

Leave a Comment