HTTP Error Codes as Constants

I need to throw a 404 and can do it like this:


throw new CHttpException(404, 'not found.');

but would prefer to use something like this:


throw new CHttpException(HTTPNotFound, 'not found.');

Is something like this already available in either Yii or PHP?

Not in the core, but you could very easily make your own class and steal the constants from Symfony.

protected/components/HttpStatus.php




<?php


class HttpStatus

{

    const CONTINUE = 100;

    const SWITCHING_PROTOCOLS = 101;

    const PROCESSING = 102;            // RFC2518

    const OK = 200;

    const CREATED = 201;

    const ACCEPTED = 202;

    const NON_AUTHORITATIVE_INFORMATION = 203;

    const NO_CONTENT = 204;

    const RESET_CONTENT = 205;

    const PARTIAL_CONTENT = 206;

    const MULTI_STATUS = 207;          // RFC4918

    const ALREADY_REPORTED = 208;      // RFC5842

    const IM_USED = 226;               // RFC3229

    const MULTIPLE_CHOICES = 300;

    const MOVED_PERMANENTLY = 301;

    const FOUND = 302;

    const SEE_OTHER = 303;

    const NOT_MODIFIED = 304;

    const USE_PROXY = 305;

    const RESERVED = 306;

    const TEMPORARY_REDIRECT = 307;

    const PERMANENTLY_REDIRECT = 308;  // RFC7238

    const BAD_REQUEST = 400;

    const UNAUTHORIZED = 401;

    const PAYMENT_REQUIRED = 402;

    const FORBIDDEN = 403;

    const NOT_FOUND = 404;

    const METHOD_NOT_ALLOWED = 405;

    const NOT_ACCEPTABLE = 406;

    const PROXY_AUTHENTICATION_REQUIRED = 407;

    const REQUEST_TIMEOUT = 408;

    const CONFLICT = 409;

    const GONE = 410;

    const LENGTH_REQUIRED = 411;

    const PRECONDITION_FAILED = 412;

    const REQUEST_ENTITY_TOO_LARGE = 413;

    const REQUEST_URI_TOO_LONG = 414;

    const UNSUPPORTED_MEDIA_TYPE = 415;

    const REQUESTED_RANGE_NOT_SATISFIABLE = 416;

    const EXPECTATION_FAILED = 417;

    const I_AM_A_TEAPOT = 418;                                               // RFC2324

    const UNPROCESSABLE_ENTITY = 422;                                        // RFC4918

    const LOCKED = 423;                                                      // RFC4918

    const FAILED_DEPENDENCY = 424;                                           // RFC4918

    const RESERVED_FOR_WEBDAV_ADVANCED_COLLECTIONS_EXPIRED_PROPOSAL = 425;   // RFC2817

    const UPGRADE_REQUIRED = 426;                                            // RFC2817

    const PRECONDITION_REQUIRED = 428;                                       // RFC6585

    const TOO_MANY_REQUESTS = 429;                                           // RFC6585

    const REQUEST_HEADER_FIELDS_TOO_LARGE = 431;                             // RFC6585

    const INTERNAL_SERVER_ERROR = 500;

    const NOT_IMPLEMENTED = 501;

    const BAD_GATEWAY = 502;

    const SERVICE_UNAVAILABLE = 503;

    const GATEWAY_TIMEOUT = 504;

    const VERSION_NOT_SUPPORTED = 505;

    const VARIANT_ALSO_NEGOTIATES_EXPERIMENTAL = 506;                        // RFC2295

    const INSUFFICIENT_STORAGE = 507;                                        // RFC4918

    const LOOP_DETECTED = 508;                                               // RFC5842

    const NOT_EXTENDED = 510;                                                // RFC2774

    const NETWORK_AUTHENTICATION_REQUIRED = 511;                             // RFC6585

}



Then use it in code as:




throw new CHttpException(HttpStatus::NOT_FOUND, 'Not found.');



or you could define a constant in your components/controller.php


<?php 


class Controller extends CController

{

	const HTTPNotFound = 404;

	// ...

}




// then in your controller you can do this


throw new CHttpException(self::HTTPNotFound, 'not found.');

Rather than copying constants I also prefer a centralizing class.

[size=2]

[/size]

[size=2]Notes:[/size]

[size=2]1. "CONTINUE" as a constant lits up as an error in my editor because it corresponds to a reserved word (not that there is a real conflict, but it is better to avoid them).[/size]

  1. If you prefer ‘self::CONSTANT’, you can also do:

class  MyController extends CController implements HttpStatus