Tasks

Sometimes, we will want background processes, periodic tasks, or maintenance tasks to occur. This is where FuelPHP tasks come in handy. They can be run via the command-line tool, or set up as a periodic task on Windows, or cron job on Mac OSX and *nix. They can call upon modules and other classes just like controllers can.

Tasks should be placed in the fuel/app/tasks folder and, by default, only a run() method needs to be defined within the class. If you need other methods, these can be added in the usual way for PHP classes.

The tasks are called using the Oil refine command. FuelPHP comes with an example task called robots and will be included in the fuel/app/tasks folder.

To call the main method of the robot task, you can run the following command:

$ php oil refine robot

The run() method in the robot task has a variable defined, allowing you to pass a string via the oil refine command. Typing the following command line will change the message in the task:

$ php oil refine robots "Kill all mice"

If you look at the robots.php file, you will notice a second method called protect()—this can be called using the following code:

$ php oil r robots:protect

Tasks in FuelPHP are very useful for periodic actions and are very similar to controllers, making them straightforward to write. They also have the advantage of having the same access to the core FuelPHP functionality.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
52.15.160.43