Codeigniter HMVC from CLI: not entering controller
Asked Answered
I

2

7

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.

Imbroglio answered 1/4, 2016 at 15:59 Comment(7)
I would think because you have HMVC in application controllers and not in a module application/modules/your_module_name/controllers/Cron.phpMarlanamarlane
@wolfgang1983: What's the CLI syntax for Codeigniter if I had the controller placed in the module? I've tried without success. Anyways, controllers outside modules, in their original location, should still work using the HMVC extension, as it happens when running through the web server.Rosmunda
The one commenting above is me, with my personal account (not the one I use at work)Imbroglio
class Cron extends MX_Controller. Based on your description of MY_Session, should that be MX_Controller or MY_Controller?Cortex
@bishop: I don't understand your question. The HMVC module extends the CI structure by adding sort of an additional layer, represented with the MX_Controller. All the controllers extend from it, through an intermediate controller called MY_Controller controller.Imbroglio
Well, MX_Controller looked like a typo, based on MY_Controller. (An "X" being used where a "Y" was expected.) However, it sounds like it's a deliberate inheritance chain.Cortex
@Cortex MX_Controller is the base controller in the HMVC extension. This controller Cron inherits directly from MX_Controller because it doesn't need the functionality relying under MY_Controller.Imbroglio
C
2

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

Caisson answered 4/5, 2016 at 6:42 Comment(0)
D
1

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.

Desberg answered 21/10, 2019 at 8:12 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.