Kohana framework - Ajax implementation best practices

Shameer picture Shameer · Apr 6, 2011 · Viewed 7.5k times · Source

I am developing an application in Kohana framework. I would like to know the best practices in implementing ajax in kohana. So far I am using different controller for ajax. I think the important concerns will be minimizing the resources requirement and handling sessions.

Thanks in advance

Answer

Enrique picture Enrique · Apr 10, 2011

I'm using this:

In Controller_Template:

    public function before()
    {
        $this->auto_render = ! $this->request->is_ajax(); 
        if($this->auto_render === TRUE)
        {
            parent::before();
        }
    }

And inside my actions:

      if ($this->request->is_ajax())    
        {
            ...         
            $this->response->headers('Content-type','application/json; charset='.Kohana::$charset);
            $this->response->body($jsonEncoded);
        }