Love Fuel?    Donate

FuelPHP Forums

Ask your question about FuelPHP in the appropriate forum, or help others by answering their questions.
backbone.js with fuelphp
  • I am using backbone.js for my frontend. It's getting a little bit tricky to work it with fuel php Input class, here is why Backbone.js uses json to serialize its models back to the backend.
    By default it will post the string as application/json and can be acquired through $HTTP_RAW_POST_DATA = 'json serialized string' but using Backbone.emulateJSON = true, the model saved will be under $_POST = 'json serialized string' as application/x-www-form-urlencoded My current solution is to have a "before" function in my REST api and use json_decode on the posted model. My questions are:
    1. Is it possible to use the default behavior using $HTTP_RAW_POST_DATA with input and somehow deserialize it
    2. how do I use the validation class? Can I pass an array/object to be validated instead of reading automatically from POST?
  • Hello, I'm not having answer for quesiotn #1. but for the second, yes you can, example: http://scrp.at/_- Good luck
  • I ran into this same issue just recently and overcame by just using a rest controller with different variations of the index method (get, post, put, delete) in combination with using Input::json instead of Input::?. Thought I would add it to the forums for anyone else who has had an issue with this.
  • shohyuken Setiawan wrote on Friday 29th of July 2011:
    1. Is it possible to use the default behavior using $HTTP_RAW_POST_DATA with input and somehow deserialize it

    No, you can't do this without extending the rest controller. I've done this myself as well to make it all a bit more intuitive. Just extend the rest controller
    abstract class Controller_Rest extends Fuel\Core\Controller_Rest
    

    Add it to your bootstrap
    Autoloader::add_classes(array(
     'Controller_Rest'  => APPPATH . 'classes/extend/controller/rest.php'
    ));
    

    Copy the Router function over from the fuel rest controller and modify the response part:
    if ( $data = file_get_contents('php://input'); AND $data = json_decode($data))
      {
       array_push($arguments,$data);
      }
    
      // If method is not available, set status code to 404
      if (method_exists($this, $controller_method))
      {
       $response = call_user_func_array(array($this, $controller_method), $arguments);
       $this->response($response);
      }
      else
      {
       $this->response->status = 404;
       return;
      }
    
    This will add the raw post input as the last argument to the method being called (in case you are using GET parameters). Note that the above is my own code which also includes a "fix" that allows me to just return the resulting data in my REST services rather than having to call $this->response(). you might want to remove this, based on your preference.[/code] EDIT: Careful to fix the encoding errors in the above code, these forums have some serious bugs on them. Which is not helping sell the framework as a stable platform :(
  • is this a open-source project? really like to see a backbone.s example.
    shohyuken wrote on Friday 29th of July 2011:
    I am using backbone.js for my frontend. It's getting a little bit tricky to work it with fuel php Input class, here is why Backbone.js uses json to serialize its models back to the backend.
    By default it will post the string as application/json and can be acquired through $HTTP_RAW_POST_DATA = 'json serialized string' but using Backbone.emulateJSON = true, the model saved will be under $_POST = 'json serialized string' as application/x-www-form-urlencoded My current solution is to have a "before" function in my REST api and use json_decode on the posted model. My questions are:
    1. Is it possible to use the default behavior using $HTTP_RAW_POST_DATA with input and somehow deserialize it
    2. how do I use the validation class? Can I pass an array/object to be validated instead of reading automatically from POST?
  • thanks you two, the combination of them actually works. :)

Howdy, Stranger!

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

In this Discussion