RESTPresenter is a powerful, lightweight package designed to streamline Laravel API development. It integrates seamlessly with Laravel API Resources and Spatie's Data objects, making API creation and management effortless.
With RESTPresenter, you can:
RESTPresenter enhances your Laravel projects with powerful tools for efficient and secure API development.
To get started with RESTPresenter, you need to meet the following requirements:
Install the package via composer:
composer require xtend-packages/rest-presenter
Manage your RESTPresenter resources directly from Filament with our dedicated plugin. This integration allows you to generate user tokens, manage your API resources, and more.
To install the RESTPresenter Filament plugin, run:
php artisan rest-presenter:filament --install
This command will prompt you to auto-commit changes to your Git repository. If you choose not to commit, you can manually commit the changes yourself. The Sanctum StarterKit is automatically installed during this process. For more details, see Sanctum StarterKit.
To generate your API collection, run:
php artisan rest-presenter:generate-api-collection
By default, this command generates a Postman collection. If you prefer Insomnia, switch by setting the following in your .env
file:
REST_PRESENTER_EXPORT_PROVIDER=insomnia
For a full list of configuration options, see Configuration.
To uninstall the RESTPresenter Filament plugin, run:
php artisan rest-presenter:filament --uninstall
This command will prompt you to auto-commit and revert changes to your Git repository. If you choose not to commit, you can manually commit the changes yourself.
The new RESTPresenter panel serves as a dashboard, offering a comprehensive overview and management interface for your API collection.
You can access the RESTPresenter panel by navigating to /rest-presenter
in your browser. the path is configurable in the .env
file see Configuration for more details.
RESTPresenter panel link has now conveniently been added to the user menu for all your filament panels.
Features include:
Coming Soon: Test Coverage and Reports, just one of many features in active development.
By default, all endpoints are publicly available without Sanctum middleware, protected by a security API key which you can update via REST_PRESENTER_AUTH_API_KEY
in your .env
file.
You can make any endpoint authenticated by updating the isAuthenticated
property in the resource controller. This will automatically add the Sanctum middleware to the endpoint.
<?php class CustomerResourceController extends ResourceController{ protected static string $model = Customer::class; public static bool $isAuthenticated = true; // ... rest of the resource controller}
The following directories with generated files will be created:
app/Api/StarterKits/Sanctum
(allows you to override the default Sanctum actions)app/Api/StarterKits/Filament
(contains all auto-generated resources for Filament)resources/rest-presenter/postman
(generated Postman collection)resources/rest-presenter/insomnia
(generated Insomnia collection)resources/rest-presenter/types/
(generated TypeScript DTOs)tests/StarterKits
(generated tests)Tests are generated for each resource in the Filament test suite.
Warning: To prevent overriding your database, update
phpunit.xml
with the following:
<env name="DB_CONNECTION" value="sqlite"/><env name="DB_DATABASE" value=":memory:"/>
Initial Test Expectations: We anticipate that your initial tests will fail. This is a standard part of the testing process and is one of the reasons we provide a comprehensive test suite.
Identifying Issues: The test suite aids in identifying any missing relationships or properties in your factories. It serves as a diagnostic tool to highlight areas that may need adjustment or improvement.
Field Matching and Types: One specific aspect of the tests is to verify that fields match and return the expected data type. For instance, if a field is supposed to contain an integer but is instead a string, the test will fail. This aspect helps ensure data consistency and integrity.
Adjustments: You're encouraged to make adjustments as needed based on the test results. This could involve modifying either the data object or the factory to align with the expected types and specifications.
Check out our Roadmap for upcoming features and improvements. Feel free to open an issue for suggestions or feature requests. Join us on Discord to start a discussion and stay updated on the latest news.
RESTPresenter is more than just a CRUD generator. It offers:
Presenters allow you to transform data before it's sent to the client, enabling modifications without altering API resources. This is particularly useful for transforming data for specific endpoints.
To use a presenter, add the header property X-REST-PRESENTER: PresenterName
to your request. RESTPresenter will automatically apply the presenter to the data before sending it to the client. Presenters work with collections, single resources, and nested resources, allowing for data transformation at any response level.
The RESTPresenter package includes a Filament Starter Kit, providing a robust foundation for your Filament projects:
We are developing a full Filament Kit with additional features for comprehensive CRUD generation in your Filament project. This advanced solution is intended for those who require more robust functionality and will be released under a sponsorship model. Once we reach our sponsorship milestones, the full Filament Kit will be open-sourced.
The full kit will save developers time by providing advanced solutions out of the box. However, the Starter Kit still allows for full CRUD implementation, though it may require more time and effort. If you’re interested in the advanced Filament Kit, reach out to us on Discord to express your interest and support its development through sponsorship.
RESTPresenter seamlessly integrates into any Laravel application, allowing you to generate API resources effortlessly from your selected models.
Customize RESTPresenter for your project with our setup command:
php artisan rest-presenter:setup
We recommend installing the Sanctum starter kit, so this has been pre-selected for you. Note Filament now has a dedicated command to install the RESTPresenter plugin so has been removed from the setup command.
To generate a new resource, use the following command:
php artisan rest-presenter:make-resource
This command will guide you through creating a new resource. Prompts will allow you to automatically generate presenters, filters, data, and set up your resource ready to use. All model relationships and fields are automatically detected throughout the prompt process. Additionally, we provide a custom option for most prompts to generate without auto-detection.
We no longer publish the configuration by default. This is to provide better support for future updates and to prevent conflicts with your existing configuration. If you need to publish the configuration, you can do so with vendor:publish
however we do not recommend this approach.
Instead we have made sure that you can override any configuration directly in your .env
file. This allows you to customize the package to your needs without the need to publish the configuration.
Here is a list of all available configuration options including their default values:
# RESTPresenter Generator ConfigurationREST_PRESENTER_GENERATOR_PATH=app/ApiREST_PRESENTER_GENERATOR_NAMESPACE=App\ApiREST_PRESENTER_GENERATOR_TS_TYPES_PATH=rest-presenter/typesREST_PRESENTER_GENERATOR_TS_TYPES_KEYWORD=interfaceREST_PRESENTER_GENERATOR_TS_TYPES_TRAILING_SEMICOLON=trueREST_PRESENTER_GENERATOR_TEST_PATH=tests/Feature/Api/v1REST_PRESENTER_GENERATOR_TEST_NAMESPACE=Tests\Feature\Api\v1 # RESTPresenter API ConfigurationREST_PRESENTER_API_PREFIX=apiREST_PRESENTER_API_VERSION=v1REST_PRESENTER_API_NAME=APIREST_PRESENTER_API_DEBUG=trueREST_PRESENTER_API_PRESENTER_HEADER=X-REST-PRESENTER # RESTPresenter Auth ConfigurationREST_PRESENTER_AUTH_API_KEY=rest-presenter-secret-keyREST_PRESENTER_AUTH_API_TOKEN_NAME=rest-presenter-api-tokenREST_PRESENTER_AUTH_API_KEY_HEADER=X-REST-PRESENTER-API-KEYREST_PRESENTER_AUTH_ENABLE_API_KEY=trueREST_PRESENTER_AUTH_REGISTER_DATA_NAME="required|string|max:255"REST_PRESENTER_AUTH_REGISTER_DATA_EMAIL="required|string|email|max:255|unique:users,email"REST_PRESENTER_AUTH_REGISTER_DATA_PASSWORD="required|string|min:8|max:255|confirmed"REST_PRESENTER_AUTH_LOGIN_DATA_EMAIL="required|string|email|max:255"REST_PRESENTER_AUTH_LOGIN_DATA_PASSWORD="required|string|min:8"REST_PRESENTER_AUTH_LOGOUT_REVOKE_ALL_TOKENS=falseREST_PRESENTER_AUTH_RATE_LIMIT_MAX_ATTEMPTS=5 # RESTPresenter Export ConfigurationREST_PRESENTER_EXPORT_PROVIDER=postman # RESTPresenter Export Insomnia ConfigurationREST_PRESENTER_EXPORT_INSOMNIA_WORKSPACE_NAME="${APP_NAME} (RESTPresenter)"REST_PRESENTER_EXPORT_INSOMNIA_WORKSPACE_DESCRIPTION="${APP_NAME} RESTPresenter Workspace"REST_PRESENTER_EXPORT_INSOMNIA_ENVIRONMENT_NAME="${APP_NAME} (RESTPresenter)"REST_PRESENTER_EXPORT_INSOMNIA_ENVIRONMENT_BASE_URL="${APP_URL}"REST_PRESENTER_EXPORT_INSOMNIA_ENVIRONMENT_VERSION=v1 # RESTPresenter Export Postman ConfigurationREST_PRESENTER_EXPORT_POSTMAN_INFO_NAME="${APP_NAME} (RESTPresenter)"REST_PRESENTER_EXPORT_POSTMAN_INFO_SCHEMA="https://schema.getpostman.com/json/collection/v2.1.0/collection.json"REST_PRESENTER_EXPORT_POSTMAN_AUTH_METHOD=bearerREST_PRESENTER_EXPORT_POSTMAN_AUTH_TOKEN=YOUR_API_TOKEN # RESTPresenter Resource ConfigurationREST_PRESENTER_RESOURCES_USER_PROFILE=\XtendPackages\RESTPresenter\Resources\Users\Presenters\ProfileREST_PRESENTER_RESOURCES_USER_USER=\XtendPackages\RESTPresenter\Resources\Users\Presenters\User # RESTPresenter Panel ConfigurationREST_PRESENTER_PANEL_BRAND_NAME=RESTPresenterREST_PRESENTER_PANEL_BRAND_LOGO=trueREST_PRESENTER_PANEL_PATH=rest-presenterREST_PRESENTER_PANEL_MAX_WIDTH=7xlREST_PRESENTER_PANEL_TOP_NAVIGATION=false
Please see CHANGELOG this is automatically generated from commits which follow the conventional commits standards and updated with each release.
Please see our CONTRIBUTING guide if you are thinking of contributing to this package.
RESTPresenter is open-source software licensed under the MIT License