symfonyhttp-response-codessymfony-http-foundation

Shouldn't Response::isOk() return true for an HTTP status between 200 and 299?


I'm using Symfony to build REST APIs. I'm in a situation where I want to do some actions in the kernel.terminate event, but I want to make sure that the request was successfully processed before doing so.

For that, as I use REST conventions and should return an appropriate HTTP status code when an error occured, I want to check the response HTTP status. The method Symfony\Component\HttpFoundation\Response::isOk() seems appropriate and more readable than checking manually if the HTTP status is between 200 and 299, but it returns true only if the HTTP status equals 200. As I return a 201 status code when a resource is created, I can't use it for that.

Before opening an issue on Github, I was wondering if there is a reason for this method to not return true for other success HTTP statuses?

Thanks!


Solution

  • You can use the isSuccessful() method from the Response object:

    /**
     * Is response successful?
     *
     * @return bool
     *
     * @final since version 3.2
     */
    public function isSuccessful()
    {
        return $this->statusCode >= 200 && $this->statusCode < 300;
    }