N
N
Nik Gubin2016-10-26 01:32:05
PHP
Nik Gubin, 2016-10-26 01:32:05

How to organize class autoloading in Slim Framework?

Good night friends.
I decided to get acquainted with Slim and already picked up a project for training. I also work with Laravel, so namespace's have already become a habit. I installed the slim/slim-skeleton package first.
Slim has the ability to specify in routes which class and method to call. Fine.
But what about the autoloading of these very classes? I made some "crutch", for starters:

$container = $app->getContainer();

// Руками подключаю файл с нужным классом
require __DIR__ . '/Controllers/Controller.php';

// Помещаю в контейнер
$container['Controller'] = function ($container) {
  return new Src\Controllers\Controller();
};

But then he is a "crutch". I do not believe that no one before me has implemented a more elegant way. I did not find any documentation, maybe I was looking badly, so I will be glad for any link with useful information.
PS Installed Slim via composer.

Answer the question

In order to leave comments, you need to log in

6 answer(s)
Y
Yuri, 2016-10-26
@riky

in theory, autoload should be through composer.
see composer autoload

N
Nik Gubin, 2016-10-26
@gubin_niko

As usual, I asked and answered myself.
But I think it will be useful to everyone else who has the same question.
It is necessary to register in composer.json:

"autoload": {
    "psr-4": {
      "App\\": "src/App"
    }
  }

where "src/App" is the path to the directory with your classes.
After we update the composer and now the autoloader will successfully load the classes.
It is also very easy to write in routes:
and the autoloader will load the NameController and execute the index.

R
rinatoptimus, 2016-12-19
@2007-2017

jsfiddle.net/davidThomas/7F7Vn/1 - from here: stackoverflow.com/questions/20305594/circle-with-t...

D
Dmitry, 2016-12-19
@soledar10

https://jsfiddle.net/soledar10/khnkegjz/

P
Pshkll, 2016-12-19
@Pshkll

.box{
  margin-left: 10px;
  margin-top: 10px;
}

D
display: block, 2016-12-19
@qork

codepen.io/anon/pen/PbVRYG

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question