I have a controller called Buy and it has a live method. How to install a cronjob if using codeigniter? I use codeigniter 4,and l use cpanel.
php - q /home/kuslon/kuslon/app/Controllers/Services/Buy.php
I have a controller called Buy and it has a live method. How to install a cronjob if using codeigniter? I use codeigniter 4,and l use cpanel.
php - q /home/kuslon/kuslon/app/Controllers/Services/Buy.php
2
Answers
the Main thing You must do is check if request is from command Line and not from A browser in your controller using this is_cli() function .
then you can set the cron job in Cpanel .
good luck
STEPS
app/Config/Routes.php
. I.e:Where:
cli/ship-product/(:segment)
– Represents your route. The(:segment)
is optional depending on if your Controller method requires an argument or not.Buy
– Represents your Controller.ship
– Represents your Controller method.$1
– Represents the optional(:segment)
to be forwarded to the first Controller method’s argument if in case it requires one. You may omit it if your Controller method doesn’t require any arguments.Notice the use of CLI-only routing with the help of
->cli(...)
.Where:
-q
– Represents quiet-mode. Suppress HTTP header output./home/kuslon/kuslon/public/index.php
– Represents the absolute path to your project’sindex.php
file. This assumes that the firstkuslon
in the path represents your Cpanel username and the secondkuslon
represents your project root folder.cli
– Represents the first section of the command-line route you defined earlier.ship-product
– Represents the second section of the command-line route."ed053cb1-29a4-42f2-a17e-3109fa4d80fe"
– Represents the third optional section of the command-line route that may be required by your Controller method.Resource: Running via the Command Line
Addendum:
If in case all your routes pass through an Authentication filter, you may want to exclude these command-line based routes in
app/Config/Filters.php
. I.e: As you may notice below, I’ve excluded all routes starting withcli/*
from theauthfilter
.