Love Fuel?    Donate

FuelPHP Forums

Ask your question about FuelPHP in the appropriate forum, or help others by answering their questions.
HMVC Request Problem
  • Hi i have some problem with fuel HMVC Request behaviour.
    Problem is with passing data to HMVC controller method.
    Here is practiclly same request constructions


    \Request::forge('tags/admin/dashboard/save', false)->execute(array(array('tags' => $sorted_tags['new'])))->response()->body()
    \Request::forge('authors/admin/dashboard/save', false)->execute(array(array('authors' => $sorted_authors['new'])))->response()->body()
    $sorted_tags['new'] is simple array like [1,2,3,5]

    and second array have same construction but when passing data in first request method recive simple array but in  second request recive [authors => [1,2,3,4,5]] not [1,2,3,4,5]

    Can somebody tell me why is that because it's not  normal behaviour. I don't what to make hack in save method to pass data correctly because it should make same data pass every time.
  • HarroHarro
    Accepted Answer
    How are both save() methods defined? How do you retrieve the parameters inside the save() methods?

    The parameter of execute() is an array, so you're effectively passing

    array('authors' => $sorted_authors['new'])

    this value is assigned to the first argument of the save() method, so if that is defined as

    public function action_save($data)
    {
    }

    then $data would contain exactly that, and you should use $data['authors'] in your save() method to access the data passed.

    The same is true for your other request, so if the results are different, you'll have to play the game of find the differences in both save() methods, and how you process the data passed. If it is identical, the data passed must be different.

    If identical too, we have magic at work, the execute() method doesn't do anything with the array other then pass it along to the action method called.
  • public function action_save($authors) {
    // $authors output array( 'authors' => array(1,2,3,4))
    }

    public function action_save($tags) {
    //$tags output array(1,2,3,4)
    }

    The problem is why in same request construction data is passing different in first case  and different in  second?
  • It is not the execute method that does this.

    That will:
    - accept the argument passed
    - merge it with any arguments from the route (which are not there in these calls, the route flag is false)
    - pass it on to the method called

    From a technical point of view, the "author" example is the correct output.

    I have created this test controller (using 1.8/develop):

    public function action_index()
    {
        $sorted_tags['new'] = [1,2,3,4,5];
        $sorted_authors['new'] = [1,2,3,4,5];

        $x = \Request::forge('test/save1', false)->execute(array(array('tags' => $sorted_tags['new'])))->response()->body();
        $y = \Request::forge('test/save2', false)->execute(array(array('authors' => $sorted_authors['new'])))->response()->body();
    }

    public function action_save1($authors) {
        var_dump($authors);
    }

    public function action_save2($tags) {
        var_dump($tags);
    }

    the result of calling the index action is as I predicted:

    array (size=1)
      'tags' =>
        array (size=5)
          0 => int 1
          1 => int 2
          2 => int 3
          3 => int 4
          4 => int 5

    array (size=1)
      'authors' =>
        array (size=5)
          0 => int 1
          1 => int 2
          2 => int 3
          3 => int 4
          4 => int 5

     so there is something wrong at your end somewhere.
  • Everythig is clear but still it's so strange that in my case same Request construction passing same data differently. I'm using 1.7 version in this project. I'll make more test maybe something popup in test that I missed or I'll find some bug. Thanks for unswear.
  • I just ran the same test on the 1.7 codebase, with the same result.

Howdy, Stranger!

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

In this Discussion