Love Fuel?    Donate

FuelPHP Forums

Ask your question about FuelPHP in the appropriate forum, or help others by answering their questions.
Tasks + modules
  • Hi fellow Fuelers, First time poster so please go easy on me. I have been using fuel for about 4 months now after migrating from Kohana (after migrating from CI) and am finding it absolutely awesome! Until now.. I recently started modularising my code in order to make things re-usable.
    I use Tasks for scheduling which works really well but have run into a problem when trying to call a model (from a module) from inside a task. Here is my example. This class exists inside a module located at APPPATH.'modules/rt'.
    The file is located at APPPATH.'modules/rt/classes/model/api.php'
    namespace RT;
    class Api
    {
        public static function doStuff() {}
    }
    

    I now have a task defined which is located at APPPATH.'tasks/scheduler.php'
    namespace Fuel\Tasks;
    use Cli;
    
    class Scheduler
    {
      public function run()
      {
        $output = \RT\Api::doStuff();
      }
    }
    

    The problem is that this produces a result of
    PHP Fatal error:  Class 'RT\Api' not found in /var/www/fuel/app/tasks/scheduler.php on line 40
    Error - Class 'RT\Api' not found in APPPATH/tasks/scheduler.php on line 40
    

    I have tried a number of variations including loading the module by hand from the scheduler class.
    \Fuel::add_module('rt');
    

    I know it is loading the module as changing the above line to something non-existent, produces an error of.
    Error - Trying to add a non-existent module "rt2" in COREPATH/classes/fuel.php on line 433
    

    I'm starting to wonder if this isn't an issue relating to using tasks while calling a model from a module. I know it works fine while calling the same routine from the main app directory and not as a module. Any help would be greatly appreciated. Thanks
  • The autoloader expects the Model to be named
    namespace RT;
    
    class Model_Api { }
    

    or
    namespace RT\Model;
    
    class Api { }
    

    So the 'Model' must be either in the namespace or the class name for the autoloader to know it is in classes/model. I find it very curious that the current situation does work in your application.
  • @WanWizard, Thanks for your help!! I have actually converted over to using Model_ prefixing and it seems to have cleared up all my problems. The part I was missing was the RT\Model; in the namespace declaration (namespacing is very new to me) which would have fixed everything with the method I was using.

Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

In this Discussion