It's simple. Create a component in the components folder. If you use the Advanced template, the path to the folder will be common/components , if the Basic template, create the components folder in the project root.
Code like this:
<?php namespace [путь до папки components]; use [путь до модели]/Articles; class ArticleComponent extends \Yii\Base\Component { public function create($parametrOne, $parametrTwo) { $article = new Articles(); $article->parametr_one = $parametrOne; $article->parametr_two = $parametrTwo; return $article->save(); } }
Next, in the site config we connect our component in the 'components' section 'components'
'components' => [ ... 'article' => [ 'class' => '[namespace компонента]\ArticleComponent' ], ... ]
Now you can use it anywhere on your site. For example, like this:
Yii::$app->article->create('parametr1', 'parametr2');