Slim 3 autoloader

I am new to slim framework and can't figure out how to use autoloader to autoload my classes.

I created app/models/myclass.php, but, of course, when I try to use it, I get a class that is not found. I'm not sure if this is the right way to autoload classes or names that I should use. Should I do this through a .json composer? I browse the web for several hours without any solid answer.

UPDATE

I managed to do this:

  • added model to: app / src / Model / Client.php
  • added namespace App\Model;to Client.php
  • Added the following to depedencies.php:
$container['App\Model\Client'] = function ($c) {
    return new App\Model\Client();
};

and routes.php:

$app->get('/client/ping/{id}',  function ($request, $response, $args)  {
    $container = $this->getContainer();
    $client=$container['App\Model\Client']; //instantiates a new Client
    ...
    ...
}
+4
source share
1 answer

Composer, composer.json:

{
    "require": {
        "slim/slim": "^3.9"
    },
    "autoload": {
        "psr-4": {
            "My\\Namespace\\": "src/"
        }
    }
}
// index.php
require 'vendor/autoload.php';

$app = new \Slim\App();
$myClass = new \My\Namespace\MyClass();

composer update .

+9

Source: https://habr.com/ru/post/1598575/


All Articles