2017 © Pedro PelĂĄez
 

library teapot

PHP HTTP Response Status library

image

shrikeh/teapot

PHP HTTP Response Status library

  • Tuesday, September 26, 2017
  • by shrikeh
  • Repository
  • 13 Watchers
  • 265 Stars
  • 159,112 Installations
  • PHP
  • 21 Dependents
  • 1 Suggesters
  • 15 Forks
  • 2 Open issues
  • 16 Versions
  • 11 % Grown

The README.md

Teapot

![latest_stable_version_img] ![license_img] ![twitter_img], (*1)

This is a very simple library that aims to aid verbosity in any Web-based application by defining clearly the HTTP 1.1 response codes as constants. It includes two main components: an interface, which contains the constants, and an exception specifically for HTTP., (*2)

Usage

Using the StatusCodes interface

Assuming for a moment a PHPUnit test on a cURL client response:, (*3)

<?php

/**
 * @dataProvider someUrlProvider
 */
public function testResponseIsOK($url)
{
    $client = new Client($url);
    $response = $client->get();

    $this->assertSame(200, $response->getStatusCode());
}

This becomes:, (*4)

<?php

use Teapot\StatusCode;
...
$this->assertSame(StatusCode::OK, $response->getStatusCode());

While this is a trivial example, the additional verbosity of the code is clearer with other HTTP status codes:, (*5)

<?php

use Teapot\StatusCode;

$code = $response->getStatusCode();

$this->assertNotEquals(StatusCode::NOT_FOUND, $code);
$this->assertNotEquals(StatusCode::FORBIDDEN, $code);
$this->assertNotEquals(StatusCode::MOVED_PERMANENTLY, $code);
$this->assertSame(StatusCode::CREATED, $code);

As StatusCode is an interface without any methods, you can directly implement it if you prefer:, (*6)

<?php

use Teapot\StatusCode;

class FooController implements StatusCode
{
    public function badAction()
    {
        if ($this->request->getMethod() == 'POST') {
            throw new \Exception('Bad!', self::METHOD_NOT_ALLOWED);
        }
    }
}

This may be beneficial in an abstract class, so that child classes don't need to explicitly use the interface., (*7)

There are various "helper" interfaces within the library, such as WebDAV and Http. Additionally, the various status codes are split into the RFCs that defined them: the Http helper interface extends RFCs 2616, 2324, and 2774, for example. This allows you very granular control of what status codes you want to allow within your application., (*8)

All constants have doc blocks that use the official W3C and IETF draft specification descriptions of the status code, to aid IDEs and for reference., (*9)

Using the HttpException

The HttpException is very straightforward. It simply is a named exception to aid verbosity:, (*10)

<?php

use Teapot\HttpException;
use Teapot\StatusCode;

throw new HttpException(
    'Sorry this page does not exist!',
    StatusCode::NOT_FOUND
);

The exception itself uses the StatusCode interface, allowing you to avoid manually and explicitly importing it if you prefer:, (*11)

<?php

use Teapot\HttpException;

throw new HttpException(
    'Sorry this page does not exist!',
    HttpException::NOT_FOUND
);

Installation

Run the following command., (*12)

composer require shrikeh/teapot

Coding Standards

The entire library is intended to be PSR-1, PSR-2 and PSR-4 compliant., (*13)

Get in touch

If you have any suggestions, feel free to email me at barney+teapot@shrikeh.net or ping me on Twitter with @shrikeh., (*14)

The Versions

26/09 2017
19/02 2017
03/01 2017
16/11 2016
27/06 2016

v2.1

2.1.0.0 https://shrikeh.github.io/teapot/

PHP HTTP Response Status library

  Sources   Download

MIT

The Requires

 

The Development Requires

by Barney Hanlon

http

29/04 2016

dev-feature/add-branch-alias-for-stable

dev-feature/add-branch-alias-for-stable https://shrikeh.github.io/teapot/

PHP HTTP Response Status library

  Sources   Download

MIT

The Requires

 

The Development Requires

by Barney Hanlon

http

22/04 2016

dev-stable

dev-stable https://shrikeh.github.io/teapot/

PHP HTTP Response Status library

  Sources   Download

MIT

The Requires

 

The Development Requires

by Barney Hanlon

http

22/04 2016
19/04 2016

dev-feature/RFC72X

dev-feature/RFC72X https://shrikeh.github.io/teapot/

PHP HTTP Response Status library

  Sources   Download

MIT

The Requires

 

The Development Requires

by Barney Hanlon

http

17/08 2015

v1.0.2

1.0.2.0 http://shrikeh.github.com/teapot

PHP HTTP Response Status library

  Sources   Download

MIT

The Requires

 

by Barney Hanlon

http

02/04 2013

0.1

0.1.0.0 http://shrikeh.github.com/teapot

PHP HTTP Response Status library

  Sources   Download

MIT

The Requires

  • php >=5.3.3

 

by Barney Hanlon

http