Love Fuel?    Donate

FuelPHP Forums

Ask your question about FuelPHP in the appropriate forum, or help others by answering their questions.
Module loading
  • Hi, i haven't had much time using latest version of FuelPHP, but here's what i noticed. In earlier versions of the framework you could use
    \Fuel::add_module('module_name', true);
    
    method to load desired module. Second argument in that method allowed me to load views that belongs to loaded module. Problem is, now there's no second argument option. Does it, mean that views being loaded automatically?
  • I have the same problem using Event class.
    How can I render a View calling a function with an Event::register() ?? I cannot use Request::factory() right? Example :
    <?php 
    
    namespace Bar; 
    
    class Controller_Bar &#123; 
    
    public function init() &#123; 
       \Event::register('test', 'Bar\Controller_Bar::hello_world');
    }
    
    public function hello_world() &#123; 
       return \View::factory('hello_world')->render(); 
    }
    
    }
    
  • That is a very good question. The context in which the cascading file finder works is based on the current request object. In case of events (which are callbacks), you haven't got a clue what the current context is, the event can be triggered from anywhere. In that case it's probably safest to hardcode the location:
    return \View::factory(strtolower(__NAMESPACE__).'::hello_world')->render();
    
  • Thank you for your support WanWizard,
    but your suggestion it doesn't work ... there's any other method to hardcode location?
    ( I didn't know that I could write "namespace::view_name" in factory function ) The "Event" class is very important ad powerful for a flexible application in my opinion ... and would be a shame if I cannot use a view render
  • What doesn't work? Any error messages? If not, can you describe what does happen? A \View::factory('module::viewname') loads the view explicitly from the module, no matter where you call it from. So if that doesn't work, you might have another issue...
  • It works!!!
    Sorry WanWizard but, for testing, I have tried to downgrade on RC 2.1, now with RC 3 is ok. Thank you!!!!
  • Yes, explicit module loading (the "module::") is only available as of RC3.
  • You can load views from a module from INSIDE the module (i.e. a module controller). If the loaded view doesn't exist in the module, but it does in app/views, it will be loaded from there. You can not by default load something from a module from OUTSIDE that module. If you want to do that, either specifically load it (as of today's develop branch):
    $view = View::factory("module::folder/view");
    

    or add the module to the current requests search path manually:
    Request::active()->add_path(APPPATH.'modules/module');
    
  • Ok, just to be clear. Let's say i have module "Foo" and module "Bar" Module Foo controller looks like this:
    <?php
    
    namespace Foo;
    
    class Controller_Foo {
    
      public function action_index() {
         \Fuel::add_module('bar');
         echo \Bar\Controller_Bar::hello_world();
      }
     
    }
    

    It loads hello_world method from Bar module, which looks like that:
    <?php
    
    namespace Bar;
    
    class Controller_Bar {
    
      public function hello_world() {
         return \View::factory('hello_world')->render();
      }
    
    }
    

    hello_world.php is in bar/views/ folder. If you try to use it, Fuel will throw an error message saying that the requested view could not be found: hello_world.
    How do i get around that? Thanks.
  • Do not call the other module controller directly. Instead, call it through a request object:
    $widget = Request::factory('bar/controller/method', false);
    
    in which case you need an action_method() in your controller. Or add the 'bar' module path to the current request:
    Request::active()->add_path(APPPATH.'modules'.DS.'bar');
    

    Alternatively, you can hardcode the module (not recommended):
    class Controller_Bar &#123;; 
        public function hello_world()
        &#123; 
            return \View::factory('bar::hello_world')->render(); 
        }
    }
    
  • Thanks a lot.

Howdy, Stranger!

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

In this Discussion