Love Fuel?    Donate

FuelPHP Forums

Ask your question about FuelPHP in the appropriate forum, or help others by answering their questions.
Render a module view from outside the module?
  • Is there a way to render a module view from outside the module? Can't seem to find a way to do this.
  • In the docs there's a section for HMVC calls... that's what you're looking for.
    $widget = \Request::factory('module/controller/action/param', false)->execute();
    
  • Or if you only mean to use the view from a controller outside of the module... use
    \Fuel::add_module('modulename');
    

    That will add the module view folder into the load path and enable you to use the views. (That's the easier way)... Or you could simply add the module path to the paths array
    \Fuel::add_path(APPPATH.'modules/modulename');
    
  • Hm.. I tried that and it called the module fine, but when trying to load a view, it was looking in the app/views folder instead of the app/modules/{module}/views folder. I'll take another shot at it though... edit: I tried doing via your 2nd post, i'll have to give your first post a try
  • I'm not 100% sure but there's a way to prepend the path to the paths array instead of at the end. Check the docs for the fuel class... Add_path method... You want it to search the module path first. Sorry I'm in my phone so I can't look it all up.
  • Ah. That's it. Set the second argument of Fuel::add_path to true.
  • \Fuel::add_path(APPPATH.'modules/modulename', true);
    
    $view \View::factory('path/to/view');
    

    That'll work for you. If what you mean to do is to ONLY use the view...
  • Note that the preferred way if doing this is to use a request, and use a module controller to render the view. This way you won't create any dependencies between modules, or app and module.
    $view = Request::factory('modulename/controller/method')->execute();
    

    And then
    namespace Modulename;
    
    class Controller_Controller {
    
        public function method()
        {
            return \View::factory('path/to/view');
        }
    }
    
  • I was using the way that nurdsrecueme suggested to start with but it didn't seem right somehow so I changed it to the way I thought it should be done and low and behold it turns out to be the recommended way. I even surprise myself sometimes :) Slowly getting my head around Fuel. The more I use it the more obvious and simple things become :)
  • @WanWizard - yea, I finally got around to getting it to work that way, thanks for the input @Phil F - I'm right there with ya, slowly getting my head around Fuel as well @nurdsrecueme - Thanks for all the help :)
  • // Load a view from APPPATH/modules/test/views/some/view.php
    $view = \View::factory('test::some/view');
    
    
  • @nerdsrescueme your add_path() thing is correct, I use that in a project currently. Sometimes, if it's a one-off (or a module that I don't want to check views for every time) then it's easier to use the :: to get a view from a module.

Howdy, Stranger!

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

In this Discussion