Custom Widgets: Where To? How To?

Hello,

I want to create my own widgets.

  1. Can the directory stucture be the same as Yii1, where I put my widget in components/ folder and views for the widget in componenets/views folder?

  2. How should I call a widget in a view? The same as the bootstrap widgets are used? In view:


<?= MyWidgetClassName::widget(); ?>

?

Yes, but you forgot the namespace i guess, for example:


<?php echo \frontend\widgets\RegisterForm::widget(); ?>

or


<?php 

use frontend\widgets\RegisterForm;

echo RegisterForm::widget();

 ?>

And what about rendering a view inside the widget:




<?php


namespace app\widgets;


use Yii;

use yii\base\Widget;


class BottomBar extends Widget

{

	public function run()

	{

		return $this->render('bottomBar');

	}

}



I put the bottomBar.php in widgets/views/. Now I use it in the view, generated by SiteController:


The view file does not exist: /home/user/public_html/views/site/bottomBar.php

Create a views directory inside your widgets directory. So it will be like widgets/views/bottomBar.php.