Love Fuel?    Donate

FuelPHP Forums

Ask your question about FuelPHP in the appropriate forum, or help others by answering their questions.
When module A calls module b, why is A's view loaded and not B's?
  • Lets say module A calls module B like so:

    $view = \moduleB\Controller_Admin_ModuleB::getFields();

    Now module B wants to load its own view, but module A's is loaded instead. Is that by design or am I doing something wrong?

    Using ver 1.4
  • HarroHarro
    Accepted Answer
    Yes, and yes. ;-)

    The key concept here is "request context". Everything that is loaded in Fuel, like config, language files, views, are loaded from their context, which determines what search paths are used to look for a file.

    If you request a controller in a module A, the context of that request is "module A". Which means files are loaded from: the module, any loaded package, the app, the core", in this order.

    When you call a class in another module, the context doesn't change. Nothing in Fuel is aware of the fact that your in another module, and there is no way to determine that you are. This explains why you still load resources from "module A".

    Second part of your question: you should not call cross-module (or from app to a module for that matter). That will create a tight coupling between the two, which is always a bad idea. It has other downsides too, one of which you have now experienced.

    The correct way to call a controller in another module is to do an HMVC call:

    $fields = \Request::forge('moduleb/admin/moduleb/getfields', false)->execute()->response()->body;

    This will create a new request context, this time for "moduleb", and all works as you would like it to.

    The response body of the request contains whatever your method returns.

Howdy, Stranger!

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

In this Discussion