Autoload does not find PSR-0 class

Asked

Viewed 78 times

1

I made an application using Poser with autload, configured the file Composer.js and set the namespace correctly, but when executing the code one of the classes is not found generating the error below.

Fatal Error error: Class 'SON Init Bootstrap' not found in C: xampp htdocs pj App init.php on line 7

Composer.js

{
    "name": "son/mvc",
    "require":{
        "php": ">=7.2"
    },

    "minimum-stability": "alpha",
    "authors": [
        {
            "name": "Evandro Ignacio",
            "email": "[email protected]"
        }
    ],

    "autoload": {
        "psr-0": {
         "SON" : "vendor/",
         "App" : ""
        }
    }, 
    "config": {
        "bin-dir":"bin"
    }

}

autoload_static.php

<?php

// autoload_static.php @generated by Composer

namespace Composer\Autoload;

class ComposerStaticInit25747fe376eb11119c2042d9135ae04d
{
    public static $prefixesPsr0 = array (
        'S' => 
        array (
            'SON\\' => 
            array (
                0 => 'C:\\xampp\\htdocs\\pj\\vendor',
            ),
        ),
        'A' => 
        array (
            'App\\' => 
            array (
                0 => __DIR__ . '/../..' . '/',
            ),
        ),
    );

    public static function getInitializer(ClassLoader $loader)
    {
        return \Closure::bind(function () use ($loader) {
            $loader->prefixesPsr0 = ComposerStaticInit25747fe376eb11119c2042d9135ae04d::$prefixesPsr0;

        }, null, ClassLoader::class);
    }
}

Calling for

<?php

namespace App;

use SON\Init\Bootstrap;

class Init extends Bootstrap{ // o erro acontece aqui

    protected function initRoutes(){
        $ar['home'] = array ('route' =>'/','controller'=>'index', 'action'=>'index');
        $ar['pedidos'] = array('route' =>'/pedidos', 'controller' => 'index', 'action'=> 'pedidos');
        $this->setRoutes($ar);
    }    
}

2 answers

0


The autoload PSR-0, although it works, is outdated. Use the autoload PSR-4. The installation syntax is a little different:

Create the file composer.json with the following content:

{
    "autoload": {
        "psr-4": {
            "SON\\" : "vendor/",
            "App\\" : "App"
        }
    }
}

Open the command terminal/prompt and run the command: composer install and wait:

Rodando a instalação do composer

The structure of your project will look like this:

App/                (suas classes - chamada no php: use App\*)
public_html/        (arquivos públicos como index.php, *.css, *.js, *.png, etc)
vendor              (autoload.php - chamada no php: use SON\*)
vendor/Init         (Bootstrap - chamada no php: use SON\Init\*)
vendor/composer     (autoload requireds)

Now just request the autoloader on your index.php:

<?php
require_once('../vendor/autoload.php');

Note: I do not recommend you edit any of the autoload files. Everything should work the way it comes in the installation. If it doesn’t work, you did something wrong and shouldn’t do a "Gambi" in the autoload files.

-1

The autoload syntax is wrong try to use this:

"autoload": {
  "psr-4": {
   "SON\\" : "vendor/",
   "App\\" : ""
  }  
},

Alias, it is not recommended to use psr-0 as it has been discontinued. It is best to use psr-4.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.