Using Yii2 advanced I need to unload a little code in the Controller.

I want to create a third-party class and use it. I made a folder in common and wrote a class in it, specified a namespace , and connected it ( use ) in the controller. But it gives an error - such a class does not exist!

Class, just to unload the code in the controller, that is, in several controllers some work is done (the same), I want to make a class to call functions from it - this will significantly reduce the amount of code, and make it more rational!

In commom, I made the classes directory, and in it the UploadWorker class.

In this class I specify: namespace common/classes;

And where I create this class: use common/classes/UploadWorker ;

An error says about a non-existent class along this path!

How to properly implement such a task? Where to create? And how to connect the class?

  • If it says that the class does not exist, then the class does not exist - check the path again. - Roman Grinyov
  • @RomanGrinyov, thank you cap - Maybe_V

1 answer 1

In commom made a classes directory

not in commo m , but in commo n .

And the problem is in the wrong namespace delimiter. The delimiter is not '/', but '\'.

Ie not

 use common/classes/UploadWorker 

but

 use common\classes\UploadWorker 

And for such tasks it is better to use treit. I advise you to meet them.