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:


            
php app/www/index.php -u "/events/importTheGoogle"

          

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:


            
php app/www/index.php -u "/events/importTheGoogle?start=500&limit=100"

          

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:


            
namespace Module\Events; class Controller extends \Alloy\Module\ControllerAbstract { public function importTheGoogleAction(\Alloy\Request $request) { // Prevent non-CLI Access if(!$request->isCli()) { return false; } // Defaults for optional params $start = (int) $request->get('start', 0); $limit = (int) $request->get('limit', 1000); echo "Downloading TheGoogle!!!1\n"; echo "=================================\n\n"; echo file_get_contents("http://google.com?q=events&start=" + $start + "&limit=" + $limit); } }

          
The whole Alloy environment is loaded and ready to go for your CLI scripts, just like any other web request. This means you don't have to create your own new structure or special bootstrap file, and you still have access to all the things you would in a normal HTTP controller - models, view templates, helpers, library files, etc.

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.

Related Links