Command line interface
Cherrycake apps can run as command line applications that are invoked from an operating system prompt like the Linux shell.
To let your app attend requests from the command line, you set up an Action just like any other, except this time you use the ActionCli class when mapping it, like this:
And in your method, you use the ResponseCli class instead of the usual ResponseTextHtml or ResponseTextPlain:
Executing CLI Actions
If you remember how we created the index.php
file in the Getting started guide, you'll remember that the method we called to make Cherrycake starting working on the received request actions was Engine:attendWebRequest. When creating an app that works in the command line, the method to use is Engine:attendCliRequest instead, like this:
Apps that both attend web requests and CLI actions
Sometimes you'll want your app to attend web requests like a normal web application, but also attend some CLI actions that you'll use to perform maintenance work, run batch processes or similar tasks that are triggered by an admin from the command line, and not by a client using a browser.
A common solution is to create a cli.php
file additionally to the index.php
file. This cli.php will look more or less equal to your existing index.php
, but it will call the Engine:attendCliRequest instead of Engine:attendWebRequest method.
The Cherrycake Skeleton repository provides a cli.php
file where you'll see this solution at work.
Running an app from the command line
To run a Cherrycake app from the command line in Linux, you use the PHP cli executable to run the cli.php
file (or whatever name you choose for your main .php
file), and pass the Action name as the first parameter.
Following our example above, to run helloWorldCli
Action, we would call Cherrycake from the Linux command line like this:
Passing parameters to CLI actions
Just like regular Actions can receive GET and POST parameters, CLI actions can receive command line parameters. To map an ActionCli that receives parameters, you pass the parameters
array when creating the Request object just like you already did in the Accept GET or POST parameters of the Actions Guide, except this time you use the REQUEST_PARAMETER_TYPE_CLI
parameter type instead of REQUEST_PARAMETER_TYPE_GET
or REQUEST_PARAMETER_TYPE_POST
, like this:
And you receive the parameters just like you do with GET or POST:
Now, to call a CLI action that accepts parameters from the command line, you use the regular UNIX parameters syntax after the action name, like this:
Last updated