Love Fuel?    Donate

FuelPHP Forums

Ask your question about FuelPHP in the appropriate forum, or help others by answering their questions.
Why can't I interrupt the REST controllers response before after() is called
  • Before I had a custom router that would call a function and return a response based off of a boolean value but i'd rather just use an unmodified rest controller. Basically if something goes wrong in a method I want to be able to send a failed response with a custom method i've named fail

    public function fail($const = null)
          {
                $message = defined($const) === true ? $const : self::API_ERROR;

                return $this->response(array('success' => false, 'message' => $message), 200);
          }

    and then a method would implement it like this

    public function get_test()
          {
               //do some logic
                if(!Input::get('id')){
                      $this->fail(self::ID);
                }
                return;
          }

    but the after() method runs regardless which I want to be used if everything went ok
     public function after($response)
          {
                $message = isset($this->message) ? $this->message : 'success';
                return $this->response(array('success' => true, 'message' => $message, 'data' => $this->output), 200);
          }
  • Your get_test() method needs to return the response from $this->fail().

    After() checks if the return value is an instance of Response and if not it will create one (in this case with a null value, since that is what you return now).

Howdy, Stranger!

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

In this Discussion