Love Fuel?    Donate

FuelPHP Forums

Ask your question about FuelPHP in the appropriate forum, or help others by answering their questions.
[FuelPHP 1.7.1] REST-Controller
  • I've got 2 issues with the 1.7.1-REST controller:

    In 1.7 I returned 
    return Response::forge($this->response($data))->send();

    but in 1.7.1 I have to return without ->send(); otherwise I get double data in the result, this was not in the changelog?

    In /fuel/core/classes/format.php, function to_json():
    The json_encode looks for the config variable 
        'format.json.encode.options'

    But in the config file, /fuel/core/config/format.php
    it's set as  'format.json.encode.option'

    Were these 2 changes intended?
  • You should NEVER send a response from a controller, as the front controller (your index.php) is in charge of sending reponses back. This has always been the case.

    send() will echo the response out, and does not return anything. So you're action is returning Response::forge(null); Since this returns an empty response object, and no after processing happens when a response object is returned, I don't see what could cause double data, the response passed back to the front controller is empty.

    The second issue is a typo that sneaked in, it should be options, not option. This has been fixed in 1.8/develop, and will be part of the next release.
  • HarroHarro
    Accepted Answer
    Ah, wait, missed a bracket.

    You are injecting a response in a response, and then sending the outer response. What is the point of that? $this->response is already a response object?

    Let's look at your statement in detail.

    $this->response($data)

    This will set your data on $this->response, which the the REST controllers internal Response object.

    Response::forge($this->response($data));

    This will wrap that response object ($this->response() returns $this->response) in a new Response object.

    return Response::forge($this->response($data))->send();

    This will echo out the representation of that response, and then return whatever send() is returning, which is null (send() doesn't have a return value).

    So, at this point, your output has been send out due to your forced send() call.

    Next, the REST controllers after() method kicks in, and that receives your return value. Which is NULL. So it will assume your action has set $this->response, and will return that. Since you have filled that with your data, the front controller will send that out again.

    I have tested this in 1.7 using the following action:

        public function action_test()
        {
            $this->format = 'json';
            $data = array('this', 'is', 'a', 'test');
            return Response::forge($this->response($data))->send();
        }

    and I get the exact same double response back


    ["this","is","a","test"]["this","is","a","test"]

    when I use the browser to check the returned result.
  • Just checked against 1.6 too, and that version also returns the same.
  • Even though, when using your example from the docs:

    class Controller_Testrest extends Controller_Rest
    {

    public function get_list()
    {
    return $this->response(array(
    'foo' => Input::get('foo'),
    'baz' => array(
    1, 50, 219
    ),
    'empty' => null
    ));
    }
    }

    I get a empty page when going to /testrest/list.json 
    This is on a 1.7 installation.
  • HarroHarro
    Accepted Answer
    You must have a local issue, if I do that here, I get
    {"foo":null,"baz":[1,50,219],"empty":null}
    ( on 1.7.1, I don't have a 1.7 locally anymore )

    I also tried it on 1.6/master, and that gives the same output here.

  • I've tested on 1.6.1, 1.7, 1.7.1 with downloads from the site:


    Note: these installs are just a unzip from the downloads from the website, with composer self-updated and updated for 1.6.1 and 1.7
  • Please ignore my comment above, it's monday and apparantly I'm asleep, forgot the /list.json at the end, and all seems to work.

    Now I have to find out what the local issue is..
  • HarroHarro
    Accepted Answer
    lol, these things happen. ;-)
  • I'm a bit amazed by this issue still,

    I've copied all files from /core and /packages and /vendor over my installation (git didn't detect any changes), deleted most config files in /app/config, and I still get a white page as output.

    There are no logs generated.

    In what files can it be changed aswell? Because the core etc is copied from a fresh checkout..
  • A complete checkout made it seem that index.php was corrupted, works now!


Howdy, Stranger!

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

In this Discussion