Problem with PSR-4 and Poser autoload

Asked

Viewed 249 times

0

I’m having a problem with Poser’s autoload, the structure and code are all right, only he’s not finding the class.

I’ve already updated the autoloads by bash and nothing... anyone knows what it might be?

{
    "name": "celestino/ecommerce",
    "description": "PHP 7 Ecommerce",
    "type": "project",
    "authors": [
        {
            "name": "Lucas Celestino",
            "email": "[email protected]"
        }
    ],
    "minimum-stability": "dev",
    "require": {
        "phpmailer/phpmailer":"5.2.*",
        "rain/raintpl":"3.0.0",
        "slim/slim":"2.0"
    },
    "autoload":{
        "psr-4": {
            "Cap\\":"vendor\\cap\\php-classes\\src"
        }
    }
}

This is my index.php where I’m calling the class:

<?php
require_once("vendor\autoload.php");

$app = new \Slim\Slim();

$app->get('/', function ()
{
    $sql = new Cap\DB\Sql();
    $sql->teste();
});


$app->run();


?>

And that’s my class I’m calling.

Folder structure: Ecommerce vendor cap php-classes src DB

<?php

namespace DB;

class Sql
{
    public function teste()
    {
        echo "Funcionando";
    }
}

?>

1 answer

0

The first part of his namespace needs to be the same as defined in Composer.json. Starting from this first part, PSR-4 follows its directory structure. Beware of upper and lower case letters, this is different for some operating systems and may cause class problems not found.

Exchange the namespace of its class to:

<?php

namespace Cap\DB;

class Sql
{
    public function teste()
    {
        echo "Funcionando";
    }
}

The path where your dependencies are saved can also be corrected. Use the conventional bar to separate the spaces. Even in Windows the bar will work.

About your code being inside the vendor, I don’t think it’s a good idea to save code inside code that isn’t managed by Composer. Usually this directory is not versioned and you can forget to convert your code.

"autoload":{
    "psr-4": {
        "Cap\\": "php-classes/cap/src/"
    }
}

Browser other questions tagged

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