{"id":24991460,"url":"https://github.com/4d47/php-http-resource","last_synced_at":"2025-03-29T13:14:50.354Z","repository":{"id":13013555,"uuid":"15692996","full_name":"4d47/php-http-resource","owner":"4d47","description":null,"archived":false,"fork":false,"pushed_at":"2015-03-07T14:57:36.000Z","size":452,"stargazers_count":2,"open_issues_count":1,"forks_count":0,"subscribers_count":2,"default_branch":"master","last_synced_at":"2025-03-06T08:58:14.826Z","etag":null,"topics":[],"latest_commit_sha":null,"homepage":null,"language":"PHP","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":null,"status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/4d47.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":null,"code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null}},"created_at":"2014-01-07T02:25:43.000Z","updated_at":"2022-08-10T14:03:56.000Z","dependencies_parsed_at":"2022-08-23T12:31:02.793Z","dependency_job_id":null,"html_url":"https://github.com/4d47/php-http-resource","commit_stats":null,"previous_names":[],"tags_count":6,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/4d47%2Fphp-http-resource","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/4d47%2Fphp-http-resource/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/4d47%2Fphp-http-resource/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/4d47%2Fphp-http-resource/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/4d47","download_url":"https://codeload.github.com/4d47/php-http-resource/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":246187238,"owners_count":20737462,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":[],"created_at":"2025-02-04T13:49:07.903Z","updated_at":"2025-03-29T13:14:50.336Z","avatar_url":"https://github.com/4d47.png","language":"PHP","funding_links":[],"categories":[],"sub_categories":[],"readme":"# \\Http\\Resource\n\n\u003e **The PHP paradox**: PHP is a web framework. Any attempt at using PHP will result in building a web framework.\n\n## Install\n\n```bash\ncomposer require 4d47/http-resource:3.*\n```\n\n## Usage\n\n\n```php\nnamespace App;\n\n# First you define a 'class' of URLs.\n# That's a set of documents that share the same structure of data and operations.\n# Instances of the class will represents a specific URL.\n\nclass Product extends \\Http\\Resource {\n\n    # The `$path` static variable holds the URL pattern that this resource\n    # match.  When matched, the instance will have properties assigned with\n    # the pattern variables.  Parenthesis can be used for optional variables,\n    # colon denote a variable and a star matches anything. eg: `/foo((/:bar)/*)`\n\n    public static $path = '/products/:name';\n\n    # Then you implement HTTP methods, GET, POST, PUT, etc\n    # to update the instance resource representation.\n    # Server errors (5xx), client errors (4xx) and redirects (3xx) are sent by throwing\n    # [http exceptions](http://github.com/4d47/php-http-exceptions).\n\n    public function get() {\n        if ($this-\u003ename == 'bazam')\n            throw new \\Http\\NotFound();\n        $this-\u003eprice = 12;\n    }\n\n    # Implement any other methods you like\n    public function __toString() {\n        return sprintf(\"%s, %d$$\", ucfirst($this-\u003ename), $this-\u003eprice);\n    }\n}\n```\n    \n\nDefault `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. \n\n```php\n\u003ca href=\"\u003c?= \\App\\Product::link($name) ?\u003e\"\u003e\n    \u003c?= $this ?\u003e\n\u003c/a\u003e\n```\n\nIf there is a file named `layout.php` in the views subpath, the first one will be used. \nThe `$content` variable will hold the result of the first view and `$this` will also be available.\nEg. using `views/layout.php`.\n\n```php\n\u003chtml\u003e\n\u003ctitle\u003e\u003c?= $this-\u003etitle ?\u003e\u003c/title\u003e\n\u003cbody\u003e\u003c?= $content ?\u003e\u003c/body\u003e\n\u003c/html\u003e\n```\n    \nFinally you bootstrap everything in your `index.php` by handling \nthe list of your resources.\n\n```php\n\\Http\\Resource::handle(['App\\Product']);\n```\n\nSee [4d47/php-start](https://github.com/4d47/php-start/) for a basic layout of the code.\n\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2F4d47%2Fphp-http-resource","html_url":"https://awesome.ecosyste.ms/projects/github.com%2F4d47%2Fphp-http-resource","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2F4d47%2Fphp-http-resource/lists"}