Welcome Guest, Not a member yet? Register   Sign In
Problem with CLI script routing or execution
#1

(This post was last modified: 04-22-2025, 10:09 AM by massimiliano1.mancini.)

Hi All,
I'm trying to add some cron scheduled jobs to my site. I've started with a simple controller:

PHP Code:
<?php

namespace App\Controllers\CLI;

use 
CodeIgniter\Controller;

class 
Cronjob extends Controller
{
    public function cliMessage($to 'World')
    {
        return "Hello {$to}!" PHP_EOL;
    }


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
ERROR: 404

Controller or its method is not found: \App\Controllers\CLI\Cronjob::cliMAssage

Any help is appreciated.
Reply
#2

First, you have to define a route in app/Config/Routes.php 

PHP Code:
// example
$routes->cli('cronjob/(:segment)''Cronjob::cliMessage/$1'); 

then run

Code:
php index.php cronjob codeigniter
// output: Hello codeigniter!

Also, your method is cliMessage not cliMAssage

See https://codeigniter.com/user_guide/cli/c...ello-world
@xxxx[{::::::::::::::::::::::::::::::::>
Reply
#3

(This post was last modified: 04-22-2025, 10:32 AM by massimiliano1.mancini.)

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 

PHP Code:
// example
$routes->cli('cronjob/(:segment)''Cronjob::cliMessage/$1'); 

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
If you use Auto Routing, skip this.

(04-22-2025, 07:10 AM)warcooft Wrote: then run

Code:
php index.php cronjob codeigniter
// output: Hello codeigniter!

Also, your method is cliMessage not cliMAssage

See https://codeigniter.com/user_guide/cli/c...ello-world

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' => [
                '/',
                'user/login',
                'cli/cronjob/message']]

After this fix the method is corrected invoked.
I leave this post as it could be useful for other users.

Thank you
Reply
#4

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

namespace App\Commands;

use 
CodeIgniter\CLI\BaseCommand;
use 
CodeIgniter\CLI\CLI;
use 
App\Models\PageModel;

class 
AggregateBlogCategories extends BaseCommand
{
    protected $group      'Blog';
    protected $name      'blog:aggregate-categories';
    // the rest of the code 

CodeIgniter Wizard (CRUD code generator for Mac) instantly scaffolds Bootstrap-based web applications with an administrative interface (admin templates include Bootstrap5)

Reply




Theme © iAndrew 2016 - Forum software by © MyBB