Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Codeigniter HMVC from CLI: not entering controller

I'm trying to run code from the CLI just as said in the CI documentation, but for some reason, maybe because of using the HMVC extension, it won't enter the specified controller.

I have found no additional documentation about CLI execution in the HMVC extension site.

Does anyone know how to deal with this?

The code here:

/**
 * application/controllers/Cron.php
 * This is just a wrapper controller to use an HMVC module using the basic CLI syntax for CI
 *
 */
class Cron extends MX_Controller
{

    public function generatepdfs($start_date = null, $end_date = null)
    {       
            echo 'Not reached'; exit;

            $this->load->module('facturation/documentscontroller');

            $this->documentoscontroller->generatepdfs($start_date, $end_date);
    }
}

No matter if I run this from the document root:

php index.php cron generatepdfs

or this:

/usr/bin/php -f /home/user/public_html/index.php cron generatepdfs

It will not display 'Not reached'.

It does if I run it from the browser.

These are some of the errors output to the console:

Unable to locate the specified class: Session.php

I have an overriden Session library MY_Session in application/libraries/session/MY_Session.php that works perfectly in a browser. If I move this file one directory upwards this error won't display again in the console, but it won't work in the browser. It is autoloaded by the way.

A workaround for this I don't like at all: copy the same file in both locations.

Next error:

PHP Fatal error: Class 'CI_Preferencias' not found in /home/mysite/public_html/system/core/Common.php on line 196

I have a custom library called Preferencias, which is also autoloaded, in application/libraries/Preferencias.php

Again, this works perfectly in the browser. However, from CLI it seems to search for a CI_Preferencias library, which doesn't exist. If I rename it to CI_Preferencias, and autoload "CI_Preferencias", CI will search for a class called CI_CI_Preferencias from CLI.

I'm not sure if I'm missing something, or there's an actual bug in CI, or the HMVC module is messing with it.

Could anyone help me?? This is getting me crazy since I must use CLI actions scheduled from cronjobs and I feel completely lost.

like image 795
luis.ap.uyen Avatar asked Apr 01 '16 15:04

luis.ap.uyen


2 Answers

If you have added subclass_prefix in config file as " $config['subclass_prefix'] = 'MY_'; " . Try MY_Preferencias.php instead of Preferencias.php

If you did not used $config['subclass_prefix'] = 'MY_'; and calling session library as $this->load->library('session'); it is taking CIs native session library instead of your custom MY_Session.php

Here is the link

like image 140
Satya Avatar answered Nov 11 '22 14:11

Satya


I think you should add constructor

class Cron extends MX_Controller
{

    public function __construct() {
       parent::__construct();
       // Any conditions/triggers while constructor loads.
    }

    public function generatepdfs($start_date = null, $end_date = null)
    {       
            echo 'Not reached'; exit;

            $this->load->module('facturation/documentscontroller');

            $this->documentoscontroller->generatepdfs($start_date, $end_date);
    }
}

I've not tested the code. Just give it a try.

like image 1
Silambarasan R.D Avatar answered Nov 11 '22 13:11

Silambarasan R.D