Love Fuel?    Donate

FuelPHP Forums

Ask your question about FuelPHP in the appropriate forum, or help others by answering their questions.
ViewModels from base controller in different module
  • In one of my modules I extend a controller in app/classes/controller. In the base controller I load a ViewModel. It doesn't seem like this is possible, since it always assumes the controller loading the ViewModel is in the active module's namespace, so it can't find the ViewModel. From ViewModel::forge:
    $namespace = \Request::active() ? ucfirst(\Request::active()->module) : '';
    $class = $namespace.'\\View_'.ucfirst(str_replace(array('/', DS), '_', $viewmodel));
    
    The ViewModel docs says something about using ViewModels from other namespaces but it doesn't seem to work http://docs.fuelphp.com/general/viewmodels.html
    Using ViewModels from other namespaces or not View_ prefixed If you want to use these, you have to use the full classname with the forge(), including the namespace.

    This doesn't appear to be the case, since the class is always prefixed with the active module namespace. Is there something I am missing?
  • Jelmer Schreuder wrote on Friday 13th of July 2012:
    This is how it actually searches for the class: https://github.com/fuel/core/blob/1.3/develop/classes/viewmodel.php#L33

    Yes, the code I posted above is from that function. The problem is that it uses Request::active()->module as the namespace, but the ViewModel I'm trying to load is not in the active module's namespace. If I do as the documentation says and specify the full class name and namespace, it will still try to use Request::active()->module as the namespace
  • It makes 2 attempts, the first one to the current Module's namespace and the second one to global at which point your code should just work.
  • Ah yeah I missed that :( It was actually the View it wasn't finding not the ViewModel. Setting $_view fixed it Thanks for the help

Howdy, Stranger!

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

In this Discussion