Steps to Create component:
components
in your project root folderMyComponent.php
namespace app\components;
use Yii;
use yii\base\Component;
use yii\base\InvalidConfigException;
class MyComponent extends Component
{
public function demo()
{
return "welcome";
}
}
config/web.php
filecomponents' => [
'mycomponent' => [
'class' => 'app\components\MyComponent',
],
]
Now you can use your component method:
namespace app\controllers;
use Yii;
class DemoController extends \yii\web\Controller
{
public function actionTest()
{
echo Yii::$app->mycomponent->demo();
}
}