skip to Main Content

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 assetsAppAsset, ....assetsAppAssets

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

2

Answers


  1. Chosen as BEST ANSWER

    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 myAliasassetsAppAsset; and I can do the same in the backend and it gets both fine without having to do any if statements on the namespace.


  2. According to the docs your class definition must be PSR-4 compliant. Therefore you must define the right namespace in your class definition. For example like this:

    namespace appassets;
    
    class AppAssets
    {
    
    }
    

    Then you could reference the class by using

    use appassetsAppAsset
    
    AppAsset::register($this);
    
    ...
    

    When using the Basic Project Template, you may put your classes under the top-level namespace app so that they can be autoloaded by Yii without the need of defining a new alias. This is because @app is a predefined alias, and a class name like appcomponentsMyClass can be resolved into the class file AppBasePath/components/MyClass.php, according to the algorithm just described.

    In the Advanced Project Template, each tier has its own root alias. For example, the front-end tier has a root alias @frontend, while the back-end tier root alias is @backend. As a result, you may put the front-end classes under the namespace frontend while the back-end classes are under backend. This will allow these classes to be autoloaded by the Yii autoloader.

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search