Slim is … well … very slim. As such it is basically just a controller. You’re right, the db inclusion in that controller action isn’t a great place to put it.
But being slim, you can add your own Model/ORM, just pick the one you like to use. For examples of more full MVC style setups, have a look at the Slim-bookshelf example app as well as the First Application Walkthrough which provide more complete examples.
Well, your example does not scale very well because it slows down your application. All you need to do is create an instance on demand. Simply use the anonymous functions (closure) as “factory” to load only the objects you really need. Example:
$container[Bookshelf\Action\ListAuthorsAction::class] = function (Container $container) {
// This here is the factory
return new Bookshelf\Action\ListAuthorsAction($container->get('view'), $container->get('db'));
};
Edit:
I dont understand this. Do i need to include all my classes here?
Not really, if you make use auf the default behavior of slim. Just add a route in your routes.php like this:
And make sure the Action class has a __invoke() method. e.g.
namespace App\Action;
use Slim\Container;
use Slim\Http\Request;
use Slim\Http\Response;
class HomeIndexAction
{
public function __construct(Container $container)
{
// TODO: Implement __construct() method.
}
public function __invoke(Request $request, Response $response)
{
// TODO: Implement __invoke() method.
$response->getBody()->write('Hello World');
return $response;
}
}
Then you don’t need a container entry for the routes. However, if you do not want to pass the entire container into the action, you should use callbacks (first example) and define all dependencies explicitly in the constructor of the action class.
Is it actually loading all the php files or just getting reference for them?