Home > Mobile >  Error with autoloading. Class does not comply with psr-4 autoloading standard. Skipping
Error with autoloading. Class does not comply with psr-4 autoloading standard. Skipping

Time:08-19

This Error happens when update to composer 2 from composer 1

Class moduleNema\twigExtensions\NemaTwigExtension located in ./modules/moduleNema/twigExtensions/NemaTwigExtensions.php does not comply with psr-4 autoloading standard. Skipping.

Folders:

    modules
      -> modulesNema
         -> controllers

         -> services
            -> Dark.php
            -> Rent.php

         -> twigExtensions
            -> NemaTwigExtensions.php

         -> Module.php

      -> Module.php

NemaTwigExtensions.php:

<?php
namespace moduleNema\twigExtensions;

use moduleNema\services\Dark;
use moduleNema\services\Rent;

class NemaTwigExtension extends \Twig_Extension implements \Twig_Extension_GlobalsInterface
{
    public function getGlobals()
    {
        return array(
            'nema' => array(
                'dark' => new Dark(),
                'rent' => new Rent()
            )
        );
    }
}

Module.php

<?PHP

   

    namespace moduleNema;

       use moduleNema\twigExtensions\NemaTwigExtension as NemaTwigExtension;

       use Craft;
 

    use craft\services\Elements;


 

    use yii\base\Event;


 class Module extends \yii\base\Module
  {
      public function init()
      {
          parent::init();

          Event::on(Elements::class, Elements::EVENT_AFTER_SAVE_ELEMENT, function( Event 
           $event ) {
        
           });
        
            if ( Craft::$app->request->getIsSiteRequest() ) {
               // Add in our Twig extension
               $extension = new NemaTwigExtension();
               Craft::$app->view->registerTwigExtension($extension);
            }        
         }
      }

on composer:

"autoload": {
        "psr-4": {
          "modules\\": "modules/",
          "moduleNema\\": "modules/moduleNema"
        },
      },

I try all kinds of stuff, and still can solve my error

CodePudding user response:

Just like Yivi answered

This problem may happen for many reasons, you should check the Path Case, File name and Class name or Namespace differences and Nested namespaces and missing declaration

CodePudding user response:

I rename all folders like this:

modules

      -> ModulesNema
         -> Controllers

         -> Services
            -> Dark.php
            -> Rent.php

         -> TwigExtensions
            -> NemaTwigExtensions.php

         -> Module.php

      -> Module.php

Then change all namespace like

use Modules\ModuleNema\TwigExtensions\NemaTwigExtensions;
use Modules\ModuleNema\Services\Dark;
use Modules\ModuleNema\Services\Rent;

on composer:

"autoload": {
        "psr-4": {
          "Modules\\": "modules/",
          "ModuleNema\\": "modules/ModuleNema"
        },
      },

and run

composer dump-autoload -o

now all it's working with composer 2

  • Related