Command Line
The command line access features built-in Alloy make creating and using cron scripts and background tasks very easy to implement and use on any computer, operating system, or platform that PHP is available on.
Running Scripts
Alloy uses all the same URL routes, dispatcher and other framework components as the entry point for CLI scripts, which makes running and using them very easy and straight forward:
This approach to running CLI scripts means that your CLI scripts use the same Controller action methods you are used to for responding to direct HTTP requests, and are easy to find and modify along with your normal workflow.
Running Scripts With Parameters
Adding special parameters or flags for your script to use is similarly easy. Parameters take the form of a typical URL in an HTTP GET request:
The URL parameters are parsed into key/value pairs that are set on the \Alloy\Request object that is passed into Controller methods so they can be used in your script.
Creating Scripts
Since CLI scripts use the same URL router and dispatch cycle as direct HTTP requests and internal HMVC sub-requests, they also use the same structure - Controllers. In the examples above, the corresponding script created to handle the request might look something like this:
As you may note, the main difference between a Controller method for a regular HTTP request and a CLI request is that in a CLI request, it is safe (and usually even desired) to immediately echo output directly from within the controller action to the console, because there is no browser output buffering or single complete response that is necessary over HTTP. This is useful for showing the progress of a running task or background process.