PHP code example of 4d47 / http-resource

1. Go to this page and download the library: Download 4d47/http-resource library. Choose the download type require.

2. Extract the ZIP file and open the index.php.

3. Add this code to the index.php.
    
        
<?php
require_once('vendor/autoload.php');

/* Start to develop here. Best regards https://php-download.com/ */

    

4d47 / http-resource example snippets


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);
    }
}

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

<html>
<title><?= $this->title 

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