Love Fuel?    Donate

FuelPHP Forums

Ask your question about FuelPHP in the appropriate forum, or help others by answering their questions.
One class instance in every hmvc request.
  • To display all errors / messages to user I made Reports class: http://scrp.at/aMR
    Everything was fine when I was using MVC, when I extended my application to use HMVC Fuel seemed to use separated instances of my class in every module and at 'the top' controller too.
    This destroyed my whole idea of this class. Is there any way to have only one instance of this class or I will must use Session to set some messages?
  • All the methods in your class are defined as static, so there is no concept of an instance.
    Class::method is exactly the same, it doesn't matter from where you call it. So your issue must be elsewhere, but without more explanation on how you use this class, I can't help.
  • I made a similar class called Notifications. I haven't tried it in a HMVC context yet but you're more than welcome to try. http://scrp.at/aM7
  • The problem is when I use Reports::add() inside hmvc (child) request and then try Reports::get() in main request I get an empty array.
    When I use it in mvc everything is good.
    Whole idea was that there is one Reports class per request (main request) so it can hold all reports and then output it. @Johnny Freeman
    Thanks for that code. I wanted not to use Session for that but I thing I'll have to.
  • Is it empty, or do you get an error? A class exists within the PHP runtime environment, and static classes really only exists once. But pay attention to the namespace, if your class is defined in app/classes, use \Reports::add() to call it, especially from a module, which lives in a different namespace from your app classes. The entire FuelPHP core is based on this static call principe, it is not a PHP nor a FuelPHP problem.
  • The problem was I modified classes/views.php in parser package to add 'reports' var (few more too - they work well) to every view
    $data' = \Reports::get();
    print_r($data) added in classes/views.php outputted valid array with reports, $reports in 'the top' view was outputting empty Array.
    I removed my modifications and added to 'the top' controller:
    $view->set('reports', Reports::get());
    That solved the problem. But still I don't know why 'reports' wasn't available in 'the top' view (reports are not deleted after Reports::get) I've got few more variables added in classes/views.php and I don't have such problems with them.
  • You have to understand how the framework works in relation to views. FuelPHP uses lazy rendering, which means View objects aren't rendered until they are cast to string (which happens for example when you echo it). If you have a nested construction, with page views and partial views which may even contain other partials, your code may not execute when you expend it (i.e. when you forge the view), but much later. p.s. it is a very bad idea to modify core code. If you have to alter core code, add a class to your app (in this case app/classes/view.php) that extends the class you want to modify. After that, define this class in your app bootstrap. FuelPHP will automatically and transparently load your extension (in case of a package, make sure the package is loaded).
  • I knew about lazy views but I misunderstood it a little. Thanks for explanation.
    I want to extend View class, I use parser so I guess I must do something like:
    class View extends Parser\View
    in APPPATCH/classes/view.php
    I did so (edited bootstrap) and every Twig code inside view is not being parsed just outputted as {{some_var}}. I also tried:
    class View extends \Fuel\Core\View
    But it doesn't work neither (Fuel\Core\FuelException [ Error ]: The requested view could not be found).
  • You need this in your bootstrap:
    // Add classes you want to override here
    Autoloader::add_classes(array(
     'View' => APPPATH.'classes/view.php',
    ));
    

    And
    class View extends Parser\View {
    }
    

    Which works fine here. Maybe some issue with your code?
  • I just forget parent::_init() in my view class... what a shame...
    All good now.

Howdy, Stranger!

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

In this Discussion