Use dependencies in classes without injecting it?


hi there.

since about 1 year i’m coding on a RESTful api with Slim 3. until now i finished about 145 routes, and i’m
looking forward to get all 322 routes done (within the next year(s)). more to come…

my controllers are all setup with injection of the whole $container.
each controller initiates the needed classes with injection of only the needed objects/settings/data.

until now i used logging only for access, error and output logging within middleware, but i’m missing the logger in all my classes.
now i like to implement the logger, for debugging purposes, to the upcoming live-system (i.e.: for monitoring of database and soap requests)

i think there are 3 ways to do:

  1. update all __constructs to include the logger object (much work)
  2. add middleware to PDO and SOAP (not nice, and not flexible)
  3. somehow use a $Global object for the logger?

is there some solution like i mention at no3? should i avoid going this way?
i’d prefer to access all callables from dependencies.php, and the data of settings.php, in all classes.

i’ve never done OOP before i started with slim3.
so i might miss something, and my thoughts might be wrong?
(i did read the PSR, MVC and RESTful conventions. and i’m trying to follow them)

your help will be apprecated!


I believe this article DI Factories for Slim Controllers should address many of your questions.


thank you for the link. i tread the article and can tell you, that i already knew anything which is written there.

my whole container get’s passed to my controllers, because
"Slim will automatically inject the container into the controller’s constructor if you don’t create a DI factory for it."

example code:

    public function __construct(Container $container)

    $db = $container['Api\Database'];
    $soap = $container['soap'];
    $validators = $container['validators'];

    // renderer
    $this->view = $container['view'];
    $this->twig = $container['twig'];
    // initiate corresponding models
    $this->vhosts = new Vhosts($soap, $validators);
    .... many more new instances here, because there are many dependencies between my models.

    most of them are just using $soap and $validators. some of them also need data from settings.php.
    $this->apache = new Vhosts\Apache($db, $validators, $container['settings']['apacheValues']);

i’m thinking about changing all instanzes to something like
$this->vhosts = new Vhosts($container, $this);


Hi , I am new for Slim Framework . I want to redirect the page after validation and successfully login, How to manage route file. Could you please give codes in route.php to make redirection to page main.php after successfully validation of login.phtml using validation.php. Thank you in advance.

My structure is:
assests – DB.php
— validation.php
templete— login.phtml
---- main.phtml
src-- route
– system


After validation and login, you can redirect like this:

return $response->withRedirect($this->router->pathFor('main'));

Which would require a route named main in your routes file, perhaps something like this

$app->get('/', '\MainController:index')->setName('main');


Thank you Tim ,

I have still a problem , how can I redirect to same page if validation failed. I am using a main.php which have two sections for login and for signup. I have used pagination for moving 1 to 2 or 2 to 1 section. In this case how can I redirect to same page if validation failed. I have used like this for log in :

$app->post(’/login’, function ($request, $response, $args) {
// Sample log message
$this->logger->info(“Slim-Skeleton ‘/’ route”);

// TODO:verify login information

// login validatuon page
include_once ‘lib/loginValidation.php’;

if ($validation){

  return $this->renderer->render($response, 'main.phtml', $args);


// Render index view
//return $this->renderer->render($response, ‘login.php’, $args);

return $this->renderer->render($response, 'index.phtml', $args);


Thank you