![]() |
Problem with CLI script routing or execution - Printable Version +- CodeIgniter Forums (https://forum.codeigniter.com) +-- Forum: CodeIgniter 4 (https://forum.codeigniter.com/forumdisplay.php?fid=28) +--- Forum: CodeIgniter 4 Support (https://forum.codeigniter.com/forumdisplay.php?fid=30) +--- Thread: Problem with CLI script routing or execution (/showthread.php?tid=92774) |
Problem with CLI script routing or execution - massimiliano1.mancini - 04-22-2025 Hi All, I'm trying to add some cron scheduled jobs to my site. I've started with a simple controller: PHP Code: <?php When I call it in my public directory Code: php index.php CLI/cronjob message no output is showed. The index.php and routing are executed for sure, infact if I make some mistakes on porpouse, they are showed. For example, this is the output of the following command Code: PS C:\develop\horizon\public> php index.php CLI/cronjob mAssage Any help is appreciated. RE: Problem with CLI script output - warcooft - 04-22-2025 First, you have to define a route in app/Config/Routes.php PHP Code: // example then run Code: php index.php cronjob codeigniter Also, your method is cliMessage not cliMAssage See https://codeigniter.com/user_guide/cli/cli_controllers.html#let-s-try-it-hello-world Problem with CLI script routing or execution - massimiliano1.mancini - 04-22-2025 Thank you for the reply. (04-22-2025, 07:10 AM)warcooft Wrote: First, you have to define a route in app/Config/Routes.php I'm using Auto Route Improved, and according to the official guide, defining explicit routes like this isn’t necessary in my case: Quote:Define a Route (04-22-2025, 07:10 AM)warcooft Wrote: then run As you noticed, I intentionally used cliMAssage in the second example just to show that the framework is responding correctly. In the actual test, I use cliMessage as expected. I also tried using CLI library commands like CLI::print, but haven't had success with that either. EDIT: I've conducted further investigation and found that the cliMessage method in the Cronjob controller is not being called at all. I ran a debug session and set a breakpoint on the output line within the cliMessage method, and I confirmed that this point is never reached. For this reason, I'm updating the topic title, as the missing output is merely a consequence of a prior issue: the method doesn't appear to be invoked correctly in the first place. EDIT2: Solved! The problem was that I forgot to update the Config/Filters.php file in order to exclude the cli/cronjob/message path from mandatory authentication Code: 'auth' => ['except' => [ After this fix the method is corrected invoked. I leave this post as it could be useful for other users. Thank you RE: Problem with CLI script routing or execution - gosocial2 - 04-23-2025 AFAIK, creating controllers and routes are not really necessary unless you want to expose some kind of RESTful service. In my case I developed a bunch of 'Command's in App\Commands that can be run by both spark and any CRON job. It just works: PHP Code: <?php |