how to autoload custom class in yii2

I have some custom classes used for a third party payment gateway that I need to integrate with my Yii2 advanced project. I have done a bunch of research but everyone seems to have a different way of doing this.

I would like to be able to reference these third party classes anywhere in my application with the "use namespace" directive. I added my classes to the path "/vendor/erec/moneris/". They all have a namespace of "erec\moneris". I added the alias in my config file like so…


    

'aliases' => [

    '@erec\moneris' => '@vendor/erec/moneris',

],



When I try to include one of these classes with "use erec\moneris\ClassName;" directive I get the "class not found" exception.

If I change the namespace to "moneris" and set the alias like…


    

'aliases' => [

    '@moneris' => '@vendor/erec/moneris',

],



it works. However, I want my namespace to be “erec\moneris” not just “moneris” because I will have other namespaces under the “erec” parent. My problem is that I can’t get the namespace inheritance to work. I’ve only been able to get a single namespace without the any “\” in it to work.

Does anyone know how I can achieve this?

Have you tried with double \\ ?




'aliases' => [

    '@erec\\moneris' => '@vendor/erec/moneris',

],



Thanks Fabrizio, that lead me onto the right path. I actually had to use a forward slash not a backslash. Silly thing to get hung up on but that did the trick.

For anyone else that might come across this, I declared the namespace in my classes as "namespace erec\moneris". In the config file I had to specify the alias like this…




'aliases' => [

    '@erec/moneris' => '@vendor/erec/moneris',

],



I can now reference these classes from anywhere with the following directive… "use erec\moneris\ClassName"

Thanks :)