Love Fuel?    Donate

FuelPHP Forums

Ask your question about FuelPHP in the appropriate forum, or help others by answering their questions.
How should my ajax controller return JSON object
  • I tried:
    $this->response->body = json_encode($data);
    
    but I get warning: controller should return a string or a Response object, support for the $controller->response object is deprecated.
    Should I simply use
    echo json_encode($data);
    
    or is there something else I should use?
  • Heres how the main parts of my ajax controller work, I override the router to check for a csrf token that should be coming with the input vars, if its not there nothing gets routed to a method.
    class Controller_Ajax extends Controller_Rest {
    
        public $params;
        public $response;
        private $user_id;
        private $token;
        private $data;
        private $success;
        private $message;
        private $output;
    
        public function before($data = null) {
    
            parent::before();
    
            if (!Sentry::check()) {
                die('not logged in');
            }
    
            $this->user_id = Session::get('user_id');
    
            $this->params = json_decode(stripslashes(Input::post('params')));
            $this->token = Input::post('csrf_token');
    
            $this->success = false;
            $this->message = null;
            $this->output = null;
            $this->code = 200;
        }
    
        public function router($resource, array $arguments) {
    
            $pattern = '/\.(' . implode('|', array_keys($this->_supported_formats)) . ')$/';
    
            // Check if a file extension is used
            if (preg_match($pattern, $resource, $matches)) {
                // Remove the extension from arguments too
                $resource = preg_replace($pattern, '', $resource);
    
                $this->format = $matches[1];
            } else {
                // Which format should the data be returned in?
                $this->format = $this->_detect_format();
            }
    
            if (Security::check_token($this->token) == false) {
                $this->message = 'page has expired, please refresh and try again';
                return;
            }
            // If they call user, go to $this->post_user();
            $controller_method = strtolower(\Input::method()) . '_' . $resource;
    
            // If method is not available, set status code to 404
            if (method_exists($this, $controller_method)) {
                if (call_user_func_array(array($this, $controller_method), $arguments) == true) {
                    $this->success = true;
                }
            } else {
                $this->message = "unknown method $controller_method";
                $this->response->status = 404;
                return;
            }
        }
    
      
        public function after($response) {
            $this->response(array('success' => $this->success, 'message' => $this->message, 'output' => $this->output), $this->code);
            return parent::after($response);
        }
    
    
  • Ideally, your controller should extend Controller_Rest. Which will take care of all json business for you, internally you just work with arrays.
  • Quick helper function: function push_json($data=array(),$response=200)
    {
    $content_type = array('Content-type'=>'application/json'); if($data)
    return new \Response(json_encode($data),$response,$content_type);
    }

Howdy, Stranger!

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

In this Discussion