Class not found when creating new widget

In my vendor directory, i have the following structure:


- marcelodeandrade

-- yii2-izimodal-widget

--- assets

--- composer.json

--- IziModalAsset.php

--- IziModalWidget.php

composer.json




{

  "name": "marcelodeandrade/yii2-izimodal-widget",

  "description": "A Yii2 wrapper widget for the iziModal, a modal plugin with jQuery.",

  "keywords": [

    "yii2",

    "extension",

    "widget",

    "jquery",

    "plugin",

    "iziModal"

  ],

  "type": "yii2-extension",

  "license": "MIT",

  "authors": [

    {

      "name": "Marcelo de Andrade",

      "email": "andrade.asmarcelo@gmail.com"

    }

  ],

  "minimum-stability": "stable",

  "require": {

    "yiisoft/yii2": ">=2.0.5"

  },

  "autoload": {

        "psr-4": {

            "marcelodeandrade\\izimodal\\": ""

        }

    }

}



IziModalAsset.php




<?php


namespace marcelodeandrade\izimodal;


use yii\web\AssetBundle;


/**

 * iziModal asset bundle

 *

 * @author  <andrade.asmarcelo@gmail.com>

 * @since 1.0

 */

class IziModalAsset extends AssetBundle

{

    public $sourcePath = null;  //'@vendor/marcelodeandrade/izimodal-widget/assets';


    public $css = [

        'css/iziModal.min.css'

    ];


    public $js = [

    	'js/iziModal.min.js',

    ];


    public $depends = [

        'yii\web\YiiAsset',

        'yii\web\JqueryAsset',

    ];

    

    public function init()

    {

        parent::init();


        $this->setSourcePath(__DIR__ . '/assets');

        

    }


    /*

     * Sets the source path if empty

     * @param string $path the path to be set

     */

    protected function setSourcePath($path)

    {

        if (empty($this->sourcePath)) {

            $this->sourcePath = $path;

        }

    }

}



IziModalWidget.php




<?php


namespace marcelodeandrade\izimodal;


use yii;

use yii\web\View;


/**

 * A Yii2 wrapper widget for the iziModal, a modal plugin with jQuery.

 *

 * @see https://github.com/marcelodeandrade/yii2-izimodal-widget

 * @author Marcelo de Andrade <andrade.asmarcelo@gmail.com>

 * @since 1.0

 */

class IziModalWidget extends \yii\base\Widget

{

	/**

     * 

     * @var string name of the theme to use 

     */

    public $theme = null;//'tab';


    /**

     *

     * @var array options for widget

     */

    public $options = [];


    public $clientOptions = [];


    public function init()

    {

        parent::init();

        $this->registerAssets();


        if (!isset($this->options['id'])) {

            throw new InvalidConfigException("The 'id' option is required");

        }

    }


    public function run()

    {

    	$view = $this->getView();

    	$options = (isset($this->options)) ? json_encode($this->options) : json_encode( [] );

        $view->registerJs($js);

    }


    /**

     * Registers the needed assets

     */

    public function registerAssets()

    {

        $view = $this->getView();

        $id = $this->options['id'];


        iziModalAsset::register($view);


        $clientOptions = Json::encode(

            [

                $this->clientOptions ?: new JsExpression('{}')

            ]

        );


        $js = "$('#{$id}').iziModal({$clientOptions});";

        $view->registerJs($js);

    }

}



In my view, i try call:


use marcelodeandrade\izimodal\IziModalWidget;

/* @var $this yii\web\View */




echo IziModalWidget::widget([]);

And get error:


Class 'marcelodeandrade\izimodal\IziModalWidget' not found

What i am doing wrong?

You should add the namespace of marcelodeandrade in the file @vendor\yiisoft\extensions.php if it is not done by composer.

in extensions.php had the array:




  'marcelodeandrade/yii2-izimodal-widget' => 

  array (

    'name' => 'marcelodeandrade/yii2-izimodal-widget',

    'version' => '9999999-dev',

    'alias' => 

    array (

      '@marcelodeandrade/izimodal-widget' => $vendorDir . '/marcelodeandrade/yii2-izimodal-widget',

    ),

  ),



i think the alias is not correct, is that so?

@edit

i remove the suffix -widget from alias and works fine, but how to fix it?

ok, now that namespace ‘marcelodeandrade/izimodal-widget’ is pointing to $vendorDir . ‘/marcelodeandrade/yii2-izimodal-widget’ directory, you can use the namespace to use the files in the respective directory.

for eg:




        use marcelodeandrade\izimodal-widget\myClass;



will use the file $vendorDir . '/marcelodeandrade/yii2-izimodal-widget/myClass.php

Thanks