2017 © Pedro Peláez
 

library http-resource

http resource

image

4d47/http-resource

http resource

  • Saturday, March 7, 2015
  • by 4d47
  • Repository
  • 1 Watchers
  • 2 Stars
  • 139 Installations
  • PHP
  • 1 Dependents
  • 0 Suggesters
  • 0 Forks
  • 1 Open issues
  • 10 Versions
  • 1 % Grown

The README.md

\Http\Resource

The PHP paradox: PHP is a web framework. Any attempt at using PHP will result in building a web framework., (*1)

Install

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

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., (*2)

<a href="<?= \App\Product::link($name) ?>">
    <?= $this ?>
</a>

If there is a file named layout.php in the views subpath, the first one will be used. The $content variable will hold the result of the first view and $this will also be available. Eg. using views/layout.php., (*3)

<html>
<title><?= $this->title ?></title>
<body><?= $content ?></body>
</html>

Finally you bootstrap everything in your index.php by handling the list of your resources., (*4)

\Http\Resource::handle(['App\Product']);

See 4d47/php-start for a basic layout of the code., (*5)

The Versions

07/03 2015

4.x-dev

4.9999999.9999999.9999999-dev

http resource

  Sources   Download

MIT

The Requires

 

The Development Requires

05/09 2014

3.x-dev

3.9999999.9999999.9999999-dev

http resource

  Sources   Download

MIT

The Requires

 

The Development Requires

05/09 2014

dev-master

9999999-dev

http resource

  Sources   Download

MIT

The Requires

 

The Development Requires

05/09 2014

v3.0.0

3.0.0.0

http resource

  Sources   Download

MIT

The Requires

 

The Development Requires

30/06 2014

v2.1.0

2.1.0.0

http resource

  Sources   Download

MIT

The Requires

 

The Development Requires

25/06 2014

v2.0.0

2.0.0.0

http resource

  Sources   Download

MIT

The Requires

 

The Development Requires

24/06 2014

2.x-dev

2.9999999.9999999.9999999-dev

http resource

  Sources   Download

MIT

The Requires

 

The Development Requires

12/03 2014

v1.2

1.2.0.0

http resource

  Sources   Download

MIT

The Requires

 

The Development Requires

11/03 2014

v1.1

1.1.0.0

http resource

  Sources   Download

MIT

The Requires

 

The Development Requires

11/03 2014

v1.0

1.0.0.0

  Sources   Download

MIT

The Requires

 

The Development Requires