Using slim 4 app in CLI mode

Hi,

Did someone succeeded to run slim 4 on CLI ?

I need to setup cronjobs for my app to run on the cli, but I am unable to use do it,

I am using the SLIM 4 squeleton app from github.

thank you

Hi :slight_smile:

Slim is wired as an HTTP app, it doesn’t grab CLI arguments, only server request ones… I think.

But you could easily manually grab the arguments you need from your CLI request and create a fake PSR-7 Request with those arguments to inject on $app->run().

Never tried but seems possible.

Thx for the quick answer Pedropereira :slight_smile:

Do you have a way or an example to do that ? I can’t figure it out :frowning:

thx

Hi @sidbenac

Like @pedropereira said, you could invoke the routes via the console. To achive this, you could create a mocked request object and invoke the $app->handle($request) method. The downside is that the console output is not optimized for the CLI. Passing and mapping console input parameters is also not so easy.

You could implement a symfony/console command for your Slim application. I have prepared an example for Slim 4 here:

1 Like