PHPackages                             4d47/http-resource - PHPackages - PHPackages  [Skip to content](#main-content)[PHPackages](/)[Directory](/)[Categories](/categories)[Trending](/trending)[Leaderboard](/leaderboard)[Changelog](/changelog)[Analyze](/analyze)[Collections](/collections)[Log in](/login)[Sign up](/register)

1. [Directory](/)
2. /
3. [HTTP &amp; Networking](/categories/http)
4. /
5. 4d47/http-resource

ActiveLibrary[HTTP &amp; Networking](/categories/http)

4d47/http-resource
==================

http resource

v3.0.0(11y ago)2186[1 issues](https://github.com/4d47/php-http-resource/issues)1MITPHPPHP &gt;= 5.3.0

Since Mar 11Pushed 11y ago1 watchersCompare

[ Source](https://github.com/4d47/php-http-resource)[ Packagist](https://packagist.org/packages/4d47/http-resource)[ RSS](/packages/4d47-http-resource/feed)WikiDiscussions master Synced 2mo ago

READMEChangelog (5)Dependencies (2)Versions (10)Used By (1)

\\Http\\Resource
================

[](#httpresource)

> **The PHP paradox**: PHP is a web framework. Any attempt at using PHP will result in building a web framework.

Install
-------

[](#install)

```
composer require 4d47/http-resource:3.*
```

Usage
-----

[](#usage)

```
namespace App;

# First you define a 'class' of URLs.
# That's a set of documents that share the same structure of data and operations.
# Instances of the class will represents a specific URL.

class Product extends \Http\Resource {

    # The `$path` static variable holds the URL pattern that this resource
    # match.  When matched, the instance will have properties assigned with
    # the pattern variables.  Parenthesis can be used for optional variables,
    # colon denote a variable and a star matches anything. eg: `/foo((/:bar)/*)`

    public static $path = '/products/:name';

    # Then you implement HTTP methods, GET, POST, PUT, etc
    # to update the instance resource representation.
    # Server errors (5xx), client errors (4xx) and redirects (3xx) are sent by throwing
    # [http exceptions](http://github.com/4d47/php-http-exceptions).

    public function get() {
        if ($this->name == 'bazam')
            throw new \Http\NotFound();
        $this->price = 12;
    }

    # Implement any other methods you like
    public function __toString() {
        return sprintf("%s, %d$$", ucfirst($this->name), $this->price);
    }
}
```

Default `render` use scripts located in the `views` directory and named after the class name. Eg. `views/app/product.php`. The instance properties are `extract` before being included. `$this` reference the resource itself, it can be used to assign properties or call helpers methods. `link` is used to reference back resource path.

```
