Basics
MoonShine allows you to switch the admin panel to API mode.
To do this, simply add Accept: application/json to the request header, after which CRUD operations will return json responses.
We also provide tools that allow you to switch authentication to JWT tokens, as well as generate OpenApi specifications and documentation based on resources.
Available routes:
- DELETE /admin/resource/{resourceUri}/crud - Mass removal (ids[])
- GET /admin/resource/{resourceUri}/crud - Listing
- POST /admin/resource/{resourceUri}/crud - Create
- PUT /admin/resource/{resourceUri}/crud/{resourceItem} - Edit
- DELETE /admin/resource/{resourceUri}/crud/{resourceItem} - Delete
When fully using MoonShine in API mode, don't forget to disable session middleware in the MoonShine configuration.
Also, take a look at the SDUI section.
JWT
MoonShine also provides a simple way to switch the admin panel to API mode and interact via tokens.
Installation:
composer require moonshine/jwtcomposer require moonshine/jwt
Next, publish the config:
php artisan vendor:publish --provider="MoonShine\JWT\Providers\JWTServiceProvider"php artisan vendor:publish --provider="MoonShine\JWT\Providers\JWTServiceProvider"
Then add the secret key in base64 to .env:
JWT_SECRET=YOUR_BASE64_SECRET_HEREJWT_SECRET=YOUR_BASE64_SECRET_HERE
Next, change the set of middleware in the system and add authPipeline and authMiddleware:
use MoonShine\JWT\JWTAuthPipe;use MoonShine\JWT\Http\Middleware\AuthenticateApi;return ['middleware' => [// empty],'auth' => [// ...'middleware' => AuthenticateApi::class,'pipelines' => [JWTAuthPipe::class],]// ...];
use MoonShine\JWT\JWTAuthPipe;use MoonShine\JWT\Http\Middleware\AuthenticateApi;return ['middleware' => [// empty],'auth' => [// ...'middleware' => AuthenticateApi::class,'pipelines' => [JWTAuthPipe::class],]// ...];
All set! Upon successful authentication, you will receive a token, which can subsequently be used in the header Authorization: Bearer <token>.
OpenApi Generator
Installation:
composer require moonshine/oagcomposer require moonshine/oag
Next, publish the config:
php artisan vendor:publish --provider="MoonShine\OAG\Providers\OAGServiceProvider"php artisan vendor:publish --provider="MoonShine\OAG\Providers\OAGServiceProvider"
The configuration is already set up; in special cases, you can override specific settings:
return [// Documentation title'title' => 'Docs',// Location path for the specification'path' => realpath(resource_path('oag.yaml')),// Route to retrieve data for documentation'route' => 'oag.json',// View for documentation'view' => 'oag::docs',];return [// Documentation title'title' => 'Docs',// Location path for the specification'path' => realpath(resource_path('oag.yaml')),// Route to retrieve data for documentation'route' => 'oag.json',// View for documentation'view' => 'oag::docs',];
Next, based on the declared and configured resources in the system, a specification will be generated:
php artisan oag:generatephp artisan oag:generate
The specification files are by default located in the resources directory:
resources/oag.yaml,resources/oag.json.
Documentation is available at the address /docs.