Love Fuel?    Donate

FuelPHP Forums

Ask your question about FuelPHP in the appropriate forum, or help others by answering their questions.
calling module method shows wrong
  • I male two module named "main" and "honda" , when i call handa function from main module, it shows error "Class 'Honda\Main' not found" .

    // honda controller
    public static function called()
        {
            //do something
        }

    // main controller
    \Module::load('honda');
    \Honda\Main::called();

    and i use \Module::loaded() to check if loded , it shows "array('honda'=>'C:\web\fuelphp\fuel\app\modules\honda\', 'main'=>'C:\web\fuelphp\fuel\app\modules\main\')" . so modules are loaded .

    please give me a
    hand ,tks.
  • How is your "honda" controller defined? Based on how you call it, it should be

    namespace Honda;
    class Main {
        public static function called()
        {
            //do something
        }
    }

    which should be defined in C:\web\fuelphp\fuel\app\modules\honda\classes\main.php
  • these are classes and paths for my test.
    tks.

    //C:\web\fuelphp\fuel\app\modules\honda\classes\controller
    namespace Main;
    use \Main\Model\Api;

    class Controller_Main extends \Controller
    {
    //
    // ignore code ....
    //
    public function action_callanothermodule()
        {      
            \Module::load('honda');
            \Honda\Main::called();
        }
    }

    //C:\web\fuelphp\fuel\app\modules\honda\classes\controller
    namespace Honda;

    class Controller_Main extends \Controller
    {
    public static function called()
        {
            //do something
        }
    }
  • HarroHarro
    Accepted Answer
    Ah, you call \Honda\Main, but your controller is called Controller_Main. Two different class names, so that is not going to work as I wrote.

    So it should be \Honda\Controller_Main.

    But calling classes cross module isn't exactly best practice, and can lead to all kinds of issues. The most famous one is "call context". In your case, the "call context" is "\Main", even when you're in your Honda controller. Which means you will not be able to load views, config or lang files from that module, they will not be found.

    It is best practice to use an HMVC call to call a controller in another namespace.
  • thanks Harro Verton , i see.

    i have changed to hmvc Request :
    \Request::forge('honda/main/calledbyother', false)->execute();

    it can load context fully and work fine.

Howdy, Stranger!

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

In this Discussion