Love Fuel?    Donate

FuelPHP Forums

Ask your question about FuelPHP in the appropriate forum, or help others by answering their questions.
How to remove attached string from HMVC response body.
  • 1. I have a package "skills" that I'm putting together for my portfolio website.

    /* returns Model\Skills */
    \Skills\Skills::getAll()

    2. Then I have a REST Controller that calls the "skills" package.

    \Controller\Rest\Skills::get_main()

    3. Next I have an application controller that dupms off the request to a presenter.

    \Controller\Skills\Index => \Presenter\Skills\Page

    4. Inside the skills presenter.

    \Presenter\Skills\Page::view()

    public function view()
    {
    /* put stuff in main content section of the template*/
    $this->skills = Request::forge('/rest/skills/main')->execute()->response()->body();
    }



    5. Finally in the view, I get an error due to a string attached to the body of the request.

    <pre>
    <!--?= print_r($skills); ?-->
    </pre>




    The requested REST method returned an array or object:<br><br>[
    {
    "1": {
    "id": "1",
    "name": "Art \u0026 Design.",
    "slug": "design",
    "url": "\/design",
    "category_id": null,
    "description": "Design is a process that begins with your message and ends with a your creation.",
    "parent_id": null,
    "deleted_at": null,
    "created_at": "1454400961",
    "updated_at": null
    },
    "2": {
    ...
    }
    }
    ]1

  • here is a link for the GitHub code snippet in question.

  • REST controllers do not have presenters or views? A REST controller should return a JSON response?
  • I know rest controllers do not have corresponding views, I am making a hmvc request call from a presenter to a rest api. Then from the presenter I was hoping to pass on the results to its view. I successfully get the DB results back from the rest api, however the body has an a attached string right before the data; "The requested REST method returned an array or object:<br><br>."

    That string is preventing the view from being able to parse the data correctly.
  • UPDATE: If I call the Skills package method ($this->skills->getAll( true )) directly from the controller or from the presenter classes I get a regular model object and everything works well; and I able to pass the info to the view. However, if I call the same Skills package method ($this->skills->getAll( true )) through a rest controller from another controller I get the weird string ("The requested REST method returned an array or object:<br><br>.") attached to the data results and that messes up the bindings in the view.
  • HarroHarro
    Accepted Answer
    Next time just give the complete story, so I don't have to guess as to the cause... ;-)

    The error message is generated by the REST controller, and caused because it can't process the data being passed to return: https://github.com/fuel/core/blob/1.8/develop/classes/controller/rest.php#L215

    Problems:
    - there is no format set, so the REST controller doesn't do format conversion, and
    - the object you pass doesn't have a toString method that converts the object at runtime

    So, It can't handle DB objects, it either needs an array, or a string (if format is set to html).
  • So if a created a toStiring method in my Skills object would that help?
    Thanks for the response, sorry for the confusion at first.
  • It's probably better you give it a toArray() method, so you can pass an array back, and set the format for the REST controller to json. The controller will then take care of the conversion and the correct result.

Howdy, Stranger!

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

In this Discussion