jupitern / slim3-skeleton

Slim3 skeleton (http + cli) with some add-ons out of the box
44 stars 12 forks source link

cli classes have no access to $app #4

Closed pradeshjm closed 7 years ago

pradeshjm commented 7 years ago

The current method of creating CLI classes does not inject $app.

Having access to $app may be useful to access the environment or settings.

pradeshjm commented 7 years ago

I was able to get around this by adding a static route in ../config/routes/console.php

It is important to add the static routes BEFORE the variable route.


$app->get('/TraderFeed/getFeed', function (Request $request, Response $response, $args) use ($app, $argv) {
             // .. my code here    
});

// automatic console command resolver
$app->get('/{command}/{method}', function (Request $request, Response $response, $args) use ($app, $argv) {

    $params = [];
       <snip>
          ... variable route code here..
       </snip
    return $app->resolveRoute("\\App\\Console", $argv[0], $argv[1], $params);
});
pradeshjm commented 7 years ago

Re-opening. The docs should point this out.

jupitern commented 7 years ago

hi, I'm not quite sure i understand your question.

the App object is a singleton so you can get the app object anywhere using $app = \Lib\Framework\App::instance();

In my opinion the ideal is to have service providers or middleware to inject dependencies in the container and then add then have it automatically resolved in the (controller / console class) constructor or method but if you can also do \Lib\Framework\App::instance()->resolve('varName') to get something from the container anywhere in your code