Answer the question
In order to leave comments, you need to log in
How to style an arbitrary class in Yii2?
Hello!
There is a class that works with the API of a third-party resource. For example, uploading images. Where is the best place to move this class, following the MVC structure? How to issue it?
ps The class is written in-house, not distributed as a composer package.
Answer the question
In order to leave comments, you need to log in
MVC is an architecture that is built using a model-view-controller bundle. But that doesn't mean that any class you want to create has to be either a view, or a controller, or a model. No, look at the purpose of your class - in Yii2 there are helpers that are simply created in the helpers folder (you can create your own - frontend/helpers, why not?). And they are called something like this: ArrayHelper::getValue($array, 'foo.bar.name');
Perhaps your class is better designed as a service - then read about the service locator . Read the documentation, see how Yii2 itself works and draw solutions from it.
Where exactly to create classes? Decide for yourself. If you work with an advanced template, you can create a components folder in common and file your classes there. Don't forget to specify namespace common\components for them; and then you can connect them like this:
use common\components\MyClassName;
//...
$api = new MyClassName($someConfig);
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question