Accessing containers in routes.php

I want to pass the containers into the routes.php and I’m only able to get it to work with GLOBALS. I hate using GLOBALS and want to know if there is another way to make this work. I want to pass the containers into the __construct() method of my class. If it is possible to access the containers directly without having to pass them, that would be even better.

Here is my code:


use API\Organizations;

$GLOBALS['myMSName'] = '/organizations';
$GLOBALS['containers'] = $app->getContainer();

    function () {
        $this->post($GLOBALS['myMSName'] . '/create',
            function ($request,
                      $args) {
                $this->logger->info('route: ' . $GLOBALS['myMSName'] . '/create');
                $this->logger->debug("getUri / " . $request->getUri());

                $myOrganizations = new Organizations($GLOBALS['containers'],

                $result = $response->withJson($myOrganizations->create($request->getParsedBody()["orgname"],

                return $result;
    ->add(new Middleware($container));

You can just pass $this from within your route to $myOrganizations = new Organizations($this) as the route’s $this scope is bound to the container.

Thank you very much! Works great.