I was diggin into yii2 for learn a test the new capabilities, recently I staterd to learn how to create a Yii2 Extension and I have the following code.
<?php
namespace jabbon\customselect;
use yii\helpers\Html;
use yii\helpers\Json;
/**
* Description of CustomSelect
*
* @author jabbon
*/
class CustomSelect extends yii\widgets\InputWidget {
public $options = array();
/**
* Runs the widget.
*/
public function run() {
if ($this->hasModel()) {
echo Html::activeTextInput($this->model, $this->attribute, $this->options);
} else {
echo Html::textInput($this->name, $this->value, $this->options);
}
$this->registerClientScript();
}
/**
* Registers the needed JavaScript.
*/
public function registerClientScript() {
$options = empty($this->options) ? '' : Json::encode($this->options);
$id = $this->options['id'];
$js = "jQuery(\"#{$id}\").customSelect({$options});";
$view = $this->getView();
CustomSelectAsset::register($view);
$view->registerJs($js);
}
}
and in my view
<?php
use yii\helpers\Html;
use yii\widgets\ActiveForm;
use app\models\UtilConstante;
use jabbon\customselect\CustomSelect;
?>
<?=
CustomSelect::widget([
'model' => $model,
'attribute' => 'tipo_raza'
]);
?>
and yii show me the following error
PHP Fatal Error – yii\base\ErrorException
Class ‘jabbon\customselect\CustomSelect’ not found
my widget resides in /vendor/jabbon/yii2-customselect
Did you install your extension with composer? Because, if not, it is not added to your composer autoloader.
You can check this, by looking at the files in /vendor/composer/autoload_*
I’d also recommend you to use PSR-4 instead of the classmap, you may try the new extension generator from Gii, which should give you a quick-start and some hints for using the newly created extension. Please report any issues here or at github.