There's a couple of issues I'm experiencing with the Rest Controller and Response classes.
I can't seem to get JSON to return using the new classes. I've tried
return Response::forge(array('number' => 2343), 200), but it returns null.
If I use the deprecated
$this->response object, it works.
This is similar to returning a view through the rest controller. I can't do what I would expect
return Response::forge(View::forge('welcome/newmessage'), 200),
but this still works: $this->response->body = View::forge('welcome/newmessage');
Am I doing something wrong?
Thanks in advance!!
Ok, thanks for the update!!
BTW (for anyone else looking), this doesn't work: $this->response(View::forge('welcome/newmessage'));
You'll need to set the response body if you want to return a view:
$this->response->body = View::forge('welcome/newmessage');
The rest controller is not supposed to return a view, it's supposed to return a data structure.
$this->response() requires an array as parameter, which will then be returned in the format requested (json, xml, whatever).
If you want to return views, use a normal controller (and return a Response object) or Controller_Template, and use $this->template to return the view...
I'm replacing a div with new html data via ajax. This is how I've always done it.
The documentation mentions that returning an html formatted response is allowed.
If I'm doing this incorrectly, programming-wise, please let me know.
Yes, but not as a view like a normal controller would.
With the rest controller, you make sure format detection detects 'html' as the desired return format (see the docs on how to do that), or set $this->format to 'html', and then use $this->response('your-html-here');
( correction on my previous post: the parameter for response() may also be something that can be cast to string, it doesn't have to be an array )
Ok, I had to set the format to make it work as I was expecting it to work ($this->format = 'html'). For whatever reason it still wasn't auto detecting the format even it was explicitly set in the ajax call, and hence giving me strange or failing responses.
Thanks!!!
The rest controller is not a normal controller, so it works and behaves differently.
The way to craft a response is documented, so check the docs. Hint: it uses the response() method.