Search code examples
phpyii2

In Yii2 how can I use a class in the root folder?


I would like to use my class rootfolder/assets/AppAsset.php from my backend class rootfolder/backend/views/layouts/view.php however I can't seem to reference it.

I cannot do \assets\AppAsset, ..\..\assets\AppAssets

Is there a way I can use this class that is inside the root folder from my backend app?

My project stucture is

RootFolder/
    backend/
    assets/AppAsset

Solution

  • I fixed this by putting this in my PSR-4 in composer.json

    "autoload-dev": {
            "psr-4": {
                "myAlias\\": "./",
            }
        },
    

    Then inside of my root file I can do use myAlias\assets\AppAsset; and I can do the same in the backend and it gets both fine without having to do any if statements on the namespace.