Love Fuel?    Donate

FuelPHP Forums

Ask your question about FuelPHP in the appropriate forum, or help others by answering their questions.
Best way to handle parameters for dual-use (HMVC and normal) controller
  • I have an API module that handles REST requests made by Backbone on the front end, and I'm starting to set up some bootstrapping for models and collections so I don't have to make an AJAX request right when the page loads. HMVC is perfect for this, but I'm running into some issues when I have a controller that takes a whole bunch of parameters in the query string, and how to mimic that in an HMVC request.

    What's the best way to either set bogus variables so they show up in \Input::get(), or create a dual-use system of getting parameters which will return either named parameters passed in the request's execute() method, or parameters from the corresponding \Input method? Passing them in the execute() method is OK but then they are passed as method arguments rather than named parameters.
  • HarroHarro
    Accepted Answer
    That is currently not possible, due to the v1.x architecture, in which most "data containers" are global. So there is only one Input class, that holds the global data. Fixing this is one of the major design goals for Fuel v2.

    You can work around by calling:

    $request = \Request::forge('my\hmvc\request\here');
    $request->named_params = array_merge($request->named_params, $my_new_get_data);
    $response = $request->execute();

    and in your controllers before() method:

    $this->params = \Request::is_hmvc() ? $this->request->params() : \Input::param();

    This way your input data is in $this->params, no matter what the source of the request is.
  • Perfect, thanks. So in 2.0 the Input class will have per-request instances?
  • Correct. Same for other data containers too, like Config and Lang.
  • Awesome.
  • Here's the result if anyone is interested:


    namespace API;

    abstract class Controller_HMVCRest extends \Controller_Rest {
    protected $_params = array();

    public function before() {
    $this->_params = \Request::is_hmvc() ? $this->request->params() : \Input::param();

    parent::before();
    }

    public function param($name, $default = null) {
    return empty($this->_params[$name]) ? $default : $this->_params[$name];
    }
    }

Howdy, Stranger!

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

In this Discussion