I had a base_controller wich extended the Template_controller, and used the before() function to do an initial setup for the $template (such as load a common header and footer).
Once I've changed the base_controller to extend the Hybrid_controller, I'm getting errors as $this->template seems not to be a valid object in REST calls.
So I want to know how conditionally initialize the $template object based on the proper request type.
Thanks in advance.
The template is meant to make it easier to produce webpages from browser requests.
In general, this is not what you want, you want to return json, xml, or perhaps a snippet of HTML. So the REST controller doesn't support templates.
The Hybrid base controller allows you to have a normal template controller (for your browser requests), and include some REST API method so you don't have to make a separate controller for that. This is mainly for controllers in an ajax environment, where one method produces the webpage, and ajax requests from that page call the controllers REST methods to update or fetch extra data.
Hi Harro, thanks for replaying.
I think I understand whats the hybrid controller logic. Perhaps I didn't made myself clear.
I've overrided the before() function in my hybrid controller. There I have code like this:
$this->template->header = View::forge ( etc...
All goes fine when using normal calls (say, any action_ function in the controller) , but when trying to access the rest methods (like post_anything function) I have errors fired in the controller::before function, as $this->template seems not to be initialized.
This sounds logic, since, as you said, there's no need for a template in a json response, but I still have to deal with both types of request in the same before() function.
I've solved it testing the $template variable like this:
if(!is_object($this->template)) return false;
And placing all related template initializacion after that.
What I wanted to know if there was a more elegant way to know if the controller is acting as a Rest or a Template one.
Sounds like probably there must be some property in the Request object that could tell me this.
Thanks again