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

2.2k Views Asked by At

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

3

There are 3 best solutions below

2
Ledif On BEST ANSWER

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

4
Sonrimos On

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

1
abram farouk On
<?php

namespace MVC\core;

class app{

    private $controller = 'home';
    private $method = 'index';
    private $params = [];

    public function __construct()
    {
        $this->url();
        $this->render();
    }


    private function url(){
        
        if(!empty($_SERVER['QUERY_STRING'])){
            $url =  explode("/",$_SERVER['QUERY_STRING']);

            // this controller
            $this->controller = isset($url[0]) ? $url[0]."controller" : "homecontroller"; 
            // this method 
            $this->method = isset($url[1]) ? $url[1] : "index";
            unset($url[0],$url[1]);
            // this params
            $this->params = array_values($url); 
        }else{
            $this->controller = 'homecontroller';
            $this->method = 'index';
        }
    }

    private function render(){ 
        $controller = "MVC\controllers\\".$this->controller;
         if(class_exists($controller)){
            
            $controller = new $controller;

            if(method_exists($controller,$this->method)){
                
                
                call_user_func_array([$controller,$this->method],$this->params);


            }else{
                echo 'method not exist';
            }
            
         }else{
             echo 'class nt exist';
         }   
    }


}