spatie / laravel-resource-links
Add links to Laravel API resources
Fund package maintenance!
spatie.be/open-source/support-us
Installs: 38 647
Dependents: 1
Suggesters: 0
Security: 0
Stars: 240
Watchers: 9
Forks: 10
Open Issues: 1
Requires
- php: ^7.2|^8.0
- illuminate/database: ~5.8.0|^6.0|^7.0|^8.0
- illuminate/routing: ~5.8.0|^6.0|^7.0|^8.0
Requires (Dev)
- orchestra/testbench: ~3.8.0|^4.0|^5.0|^6.0
- phpunit/phpunit: ^8.0|^9.0
This package is auto-updated.
Last update: 2021-04-14 14:34:28 UTC
README
This package is abandoned: Laravel resource links two years later
Add links to Laravel API resources
Let's say you have a UsersController
with index
, show
, create
, edit
, store
, update
and delete
methods and an UserResource
. Wouldn't it be nice if you had the URL's to these methods immediately in your UserResource
without having to construct them from scratch?
This package will add these links to your resource based upon a controller or actions you define. Let's look at an example of a resource.
class UserResource extends JsonResource { use Spatie\ResourceLinks\HasLinks; use Spatie\ResourceLinks\HasMeta; public function toArray($request): array { return [ 'id' => $this->id, 'name' => $this->name, 'links' => $this->links(UsersController::class), ]; } public static function meta() { return [ 'links' => self::collectionLinks(UsersController::class), ]; } }
Now when creating a UserResource
collection, you will have all the links from the UserController
available:
{ "data":[ { "id":1, "name": "Ruben Van Assche", "links": { "show": "https://laravel.app/users/1", "edit": "https://laravel.app/users/1/edit", "update": "https://laravel.app/users/1", "delete": "https://laravel.app/users/1" } } ], "meta": { "links": { "index": "https://laravel.app/users", "create": "https://laravel.app/users/create", "store": "https://laravel.app/users" } } }
Support us
We invest a lot of resources into creating best in class open source packages. You can support us by buying one of our paid products.
We highly appreciate you sending us a postcard from your hometown, mentioning which of our package(s) you are using. You'll find our address on our contact page. We publish all received postcards on our virtual postcard wall.
Why include links in your resources?
Let's say you're building a single-page application or an application built with Inertia, then you have a PHP application running at the backend and a Javascript application at the front. These applications communicate with each other via an api but what if the frontend wants to route a user to another page?
Since routes are defined in the backend, the frontend has no idea where it has to route the user to. We could just write the url's in the javascript code but what if a route is changed? So why not pass these routes from the backend to the frontend? You could just manually write down all these routes, or let this package do that job for you.
Setting up resource links
We have a dedicated docs site for this package.
Testing
composer test
Changelog
Please see CHANGELOG for more information on what has changed recently.
Contributing
Please see CONTRIBUTING for details.
Security
If you discover any security related issues, please email freek@spatie.be instead of using the issue tracker.
Credits
License
The MIT License (MIT). Please see License File for more information.