Love Fuel?    Donate

FuelPHP Forums

Ask your question about FuelPHP in the appropriate forum, or help others by answering their questions.
Run action only for HMVC requests
  • I made widget action in my modules. Can I run action_widget code only for HMVC requests from my view? And if user open url like example.com/news/widget he get 404 page?
  • This to me is exactly why HMVC as widgets is a bad idea. Why not just create a Widget class with a method for each widget or a more intelligent system that loads sub-classes and views?
  • Phil Sturgeon wrote on 03/06/11 4:08 pm:
    This to me is exactly why HMVC as widgets is a bad idea. Why not just create a Widget class with a method for each widget or a more intelligent system that loads sub-classes and views?
    May be I do it later. For now I need module based CMS. When module registered in CMS, system search for widget method and if its exists this widget appears in widgets panel.
  • Phil Sturgeon wrote on 03/06/11 4:08 pm:
    This to me is exactly why HMVC as widgets is a bad idea. Why not just create a Widget class with a method for each widget or a more intelligent system that loads sub-classes and views?
    I don't agree. The use of module controllers to produce a widget allows for a very flexible and modular design. What's wrong in this picture is mixing the different usage of modules. Either use them as widgets, in which case you use a front controller that drives your theme engine and fetches the widgets, and you don't allow any routing other than to your front controller. Or use them as routeable modules,but then drop the widgets idea. If you insist on mixing the two, get ready for some serious route filtering, or use a separate 404 controller that you can redirect to from your widget (calling show_404() generates a page, and can not be used in a widget!).
  • I assume a widget is a small piece of html code somewhere on the page, maybe the right column, like a listing of blog categories, or a listing of active users currently on the site. Is the widget action called from the view level or is it called by a controller? I'm assuming the controller gets the data from the model, renders it with a view template, and then assigns the output to a variable, which then appears in the specified location in the main page template? What if your app has multiple "main" sections like Blogs, Articles, Events, Users, etc., and you have 5 different widgets to display on your page... some depending on which section is active... i.e. if your main section is viewing a blog posting, and you want to display the widget that lists blog categories AND a diff widget for events, do you have to create calls to those 2 widget actions in your main blog controller? or would you recommend making a controller to handle the dispatching of widgets, and use some form of php switch statement to determine which widgets are generated based on the current url / route, etc? I read the section about ViewModels, but I did not understand it. Maybe you would use the ViewModel to call the widget actions, to get the data and format them with a widget template? i.e. the blog category widget would have it's own template file, and the same concept with the events widget... my terminology may be off...
  • I have widgets in modules, module controller get data from module model & generate output, which I print in common template. If user try to open url /blog/widget I redirect him to home page. Now I'm writing my own CMS and I want make widget plugins & sidebars like in wordpress.
  • what do you mean by wordpress plugin's and sidebars?
    it sounds like you've already made them.
    do you want to allow the site user (i.e. a cms author) to be able to control which widget/plug-in is displayed (on/off) or in which area of the template it is displayed?
  • Steve Wasiura wrote on 03/15/11 2:57 pm:
    what do you mean by wordpress plugin's and sidebars?
    it sounds like you've already made them.
    do you want to allow the site user (i.e. a cms author) to be able to control which widget/plug-in is displayed (on/off) or in which area of the template it is displayed?
    Yes, you are right;) For now I hardcoded widget calls in my common template, but I want to make interface to add/remove sidebars and activate/deactivate/uninstall widgets & plugins. Also I will make extention for Group & Acl package for use it with db for manage users rights for enabled plugins... When I do it I will open sources for my CMS and I will open plugins repository for it...
  • Make sure you can't route to it, and it won't be accessable from the URI.
  • Harro Verton wrote on 02/21/11 8:44 pm:
    Make sure you can't route to it, and it won't be accessable from the URI.
    This is a part of my module
    namespace Ideology;
    class Controller_Ideology extends \Frontend {
    
        ...
     
       public function action_widget()
        {
         $data = array();
    
      $data['pages']  = Model_Ideology::get_widget_data();
      
         $this->template = \View::factory('widget', $data);
        }
    }
    

    Can I deny route in module? Will be working this code when I deny route?
    echo \Request::factory('ideology/widget')->execute()->output();
    
  • Your module is not aware of the way it's called, a request is a request.
    If you don't want it routeable, you have to block it in the routes in the config file. You could try comparing Request::$main and Request::$active. If they're identical, you're in the main request. If not, you're in a subsequent request...
  • Vars Request::$main and Request::$active are protected and I can't view its value. I can check them by code:
    $r1 = \Request::active();
    $r2 = \Request::main();
    if ($r1->action == $r2->action && $r1->controller == $r2->controller)
    {
        \Request::show_404();
    }
    

    But if I show my widget in 404 page my app go to infinite loop and I get blank screen. Only that can I do with this code - redirect from /ideology/widget to /ideology or die() with 404 header;
    Harro Verton wrote on 02/24/11 9:19 pm:
    Your module is not aware of the way it's called, a request is a request.
    If you don't want it routeable, you have to block it in the routes in the config file. You could try comparing Request::$main and Request::$active. If they're identical, you're in the main request. If not, you're in a subsequent request...
  • You can do
    if (\Request::active() == \Request::main())
    {
        echo "This is the main request";
    }
    else
    {
        echo "This is an HMVC request";
    }
    

    If you use this in your controllers action or router method, you have to make sure your 404 controller doesn't require the same module methods, because that would be an HMVC request as well...
  • When I'm in action_index(), Request::active()->action will return an empty string. Is this intended behavior? edit:
    This only happens when I'm not directly accessing /index in the uri.
  • Yes, my 404 page print HMVC widgets in sidebar and I thought up to do:
    if (\Request::active() == \Request::main())
    {
        \Output::redirect('/');
    }
    else
    {
        $data = array();
       
        $data['pages']  = Model_Ideology::get_widget_data();
        $this->template = \View::factory('widget', $data);
    }
    
    Harro Verton wrote on 02/26/11 11:57 pm:
    You can do
    If you use this in your controllers action or router method, you have to make sure your 404 controller doesn't require the same module methods, because that would be an HMVC request as well...

Howdy, Stranger!

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

In this Discussion