{"id":13828186,"url":"https://github.com/gdebrauwer/laravel-hateoas","last_synced_at":"2025-10-04T06:45:09.113Z","repository":{"id":35064981,"uuid":"200690759","full_name":"gdebrauwer/laravel-hateoas","owner":"gdebrauwer","description":"Expose the authorization logic of your REST API using HATEOAS links","archived":false,"fork":false,"pushed_at":"2024-12-12T18:56:21.000Z","size":159,"stargazers_count":167,"open_issues_count":4,"forks_count":12,"subscribers_count":3,"default_branch":"master","last_synced_at":"2025-05-09T22:18:38.294Z","etag":null,"topics":["api","hateoas","laravel","resources","rest-api"],"latest_commit_sha":null,"homepage":"","language":"PHP","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/gdebrauwer.png","metadata":{"files":{"readme":"README.md","changelog":"CHANGELOG.md","contributing":"CONTRIBUTING.md","funding":null,"license":"LICENSE.md","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null}},"created_at":"2019-08-05T16:24:12.000Z","updated_at":"2024-12-15T21:35:31.000Z","dependencies_parsed_at":"2024-03-20T18:45:54.316Z","dependency_job_id":"c5d363c7-a510-427a-8d18-a750be31f0c8","html_url":"https://github.com/gdebrauwer/laravel-hateoas","commit_stats":{"total_commits":125,"total_committers":5,"mean_commits":25.0,"dds":0.384,"last_synced_commit":"1ba743cc22b619833ec9aca80aeeda04dd0d40b5"},"previous_names":[],"tags_count":19,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/gdebrauwer%2Flaravel-hateoas","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/gdebrauwer%2Flaravel-hateoas/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/gdebrauwer%2Flaravel-hateoas/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/gdebrauwer%2Flaravel-hateoas/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/gdebrauwer","download_url":"https://codeload.github.com/gdebrauwer/laravel-hateoas/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":254414477,"owners_count":22067270,"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":["api","hateoas","laravel","resources","rest-api"],"created_at":"2024-08-04T09:02:35.749Z","updated_at":"2025-10-04T06:45:04.077Z","avatar_url":"https://github.com/gdebrauwer.png","language":"PHP","funding_links":[],"categories":["PHP"],"sub_categories":[],"readme":"# Laravel HATEOAS\n\n[![Latest Version on Packagist](https://img.shields.io/packagist/v/gdebrauwer/laravel-hateoas.svg?style=flat-square)](https://packagist.org/packages/gdebrauwer/laravel-hateoas)\n[![Total Downloads](https://img.shields.io/packagist/dt/gdebrauwer/laravel-hateoas.svg?style=flat-square)](https://packagist.org/packages/gdebrauwer/laravel-hateoas)\n\n[HATEOAS](https://en.wikipedia.org/wiki/HATEOAS) allows you to expose the authorization logic of your REST API.\nThis package makes it easy to add HATEOAS links to your Laravel API resources.\n\nEach resource has its HATEOAS links, and only the accessible links per resource are returned.\nIf a link is not available on a resource, then the clients of your API can disable functionality linked to that HATEOAS link.\n\nBy default an array of links, in the following format, will be added to the JSON of a Laravel API resource:\n\n```json\n{\n    \"data\": [\n        {\n            \"id\": 1,\n            \"text\": \"Hello world!\",\n            \"_links\": [\n                {\n                    \"rel\": \"self\",\n                    \"type\": \"GET\",\n                    \"href\": \"http://localhost/message/1\"\n                },\n                {\n                    \"rel\": \"delete\",\n                    \"type\": \"DELETE\",\n                    \"href\": \"http://localhost/message/1\"\n                }\n            ]\n        }\n    ]\n}\n```\n\n\n## Installation\n\nYou can install the package via composer:\n\n```bash\ncomposer require gdebrauwer/laravel-hateoas\n```\n\n## Usage\n\nYou can create a new HATEOAS class for a model using the following artisan command:\n\n```bash\nphp artisan make:hateoas MessageHateoas --model=Message\n```\n\nIn the created class you can define public methods that will be used to generate the links. A method should either return a link or `null`.\n\n```php\nclass MessageHateoas\n{\n    use CreatesLinks;\n\n    public function self(Message $message) : ?Link\n    {\n        if (! auth()-\u003euser()-\u003ecan('view', $message)) {\n            return;\n        }\n\n        return $this-\u003elink('message.show', ['message' =\u003e $message]);\n    }\n\n    public function delete(Message $message) : ?Link\n    {\n        if (! auth()-\u003euser()-\u003ecan('delete', $message)) {\n            return $this-\u003elink('message.archive', ['message' =\u003e $message]);\n        }\n\n        return $this-\u003elink('message.destroy', ['message' =\u003e $message]);\n    }\n}\n```\n\nTo add the links to an API resource, you have to add the `HasLinks` trait and use the `$this-\u003elinks()` method. The HATEOAS class will be automatically discovered.\n\n```php\nclass MessageResource extends JsonResource\n{\n    use HasLinks;\n\n    public function toArray($request) : array\n    {\n        return [\n            'id' =\u003e $this-\u003eid,\n            'text' =\u003e $this-\u003etext,\n            '_links' =\u003e $this-\u003elinks(),\n        ];\n    }\n}\n```\n\n## Customization\n\n#### Formatting\n\nYou can customize the JSON links formatting by providing a formatter class that implements the `Formatter` interface to the `formatLinksUsing` method.\nIf the code to format the links is pretty small or you don't want to create a separate formatter class for it, you also have the option to provide a formatting callback function to the `formatLinksUsing` method.\n\n```php\nuse GDebrauwer\\Hateoas\\Hateoas;\nuse GDebrauwer\\Hateoas\\LinkCollection;\n\n// Provide your own Formatter class ...\nHateoas::formatLinksUsing(CustomFormatter::class);\n\n// ... Or provide a callback\nHateoas::formatLinksUsing(function (LinkCollection $links) {\n    // return array based on links\n});\n```\n\n#### HATEOAS class discovery\n\nBy default, the HATEOAS classes of models will be auto-discovered. Specifically, the HATEOAS classes must be in a Hateoas directory below the directory that contains the models.\nIf you would like to provide your own HATEOAS class discovery logic, you can register a custom callback:\n\n```php\nuse GDebrauwer\\Hateoas\\Hateoas;\n\nHateoas::guessHateoasClassNameUsing(function (string $class) {\n    // return a HATEOAS class name\n});\n```\n\n## Testing\n\n```bash\ncomposer test\n```\n\n## Linting\n\n```bash\ncomposer lint\n```\n\n## Changelog\n\nPlease see [CHANGELOG](CHANGELOG.md) for more information what has changed recently.\n\n## Contributing\n\nPlease see [CONTRIBUTING](CONTRIBUTING.md) for details.\n\n## Credits\n\n- [Günther Debrauwer](https://github.com/gdebrauwer)\n- [All Contributors](../../contributors)\n\n## License\n\nThe MIT License (MIT). Please see [License File](LICENSE.md) for more information.\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fgdebrauwer%2Flaravel-hateoas","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fgdebrauwer%2Flaravel-hateoas","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fgdebrauwer%2Flaravel-hateoas/lists"}