|
1 | 1 | # Getting Started
|
2 | 2 |
|
3 |
| -## Installation |
| 3 | +## API Platform Symfony variant |
4 | 4 |
|
5 |
| -If you use the [API Platform Symfony variant](../symfony/), API Platform Admin is already installed, you can skip this installation guide. |
| 5 | +If you use the [API Platform Symfony variant](../symfony/), good news, API Platform Admin is already installed! 🎉 |
6 | 6 |
|
7 |
| -Otherwise, follow this guide. |
| 7 | +You can access it by visiting `/admin` on your API Platform application. |
8 | 8 |
|
9 |
| -If you don't have an existing React Application, create one using [Create React App](https://create-react-app.dev/): |
| 9 | +When running locally, you can also click on the "Admin" button of the welcome page at https://localhost. |
10 | 10 |
|
11 |
| -```console |
12 |
| -npm init react-app my-admin |
| 11 | + |
| 12 | + |
| 13 | +Here is what it looks like with a simple API exposing a `Greetings` resource: |
| 14 | + |
| 15 | + |
| 16 | + |
| 17 | +## Manual Installation |
| 18 | + |
| 19 | +If you did not use the Symfony variant of API Platform and need to install API Platform Admin manually, follow this guide. |
| 20 | + |
| 21 | +First, let's scaffold a React Admin Application by using the [Create React Admin](https://marmelab.com/react-admin/CreateReactAdmin.html) tool: |
| 22 | + |
| 23 | +```bash |
| 24 | +npx create-react-admin@latest my-admin |
13 | 25 | cd my-admin
|
14 | 26 | ```
|
15 | 27 |
|
16 | 28 | Then, install the `@api-platform/admin` library:
|
17 | 29 |
|
18 |
| -```console |
| 30 | +```bash |
19 | 31 | npm install @api-platform/admin
|
20 | 32 | ```
|
21 | 33 |
|
22 |
| -## Creating the Admin |
| 34 | +Now you can use either: |
23 | 35 |
|
24 |
| -To initialize API Platform Admin, register it in your application. |
25 |
| -For instance, if you used Create React App, replace the content of `src/App.js` by: |
| 36 | +- [`<HydraAdmin>`](#using-hydraadmin) to connect your app to an API exposing a Hydra documentation |
| 37 | +- [`<OpenApiAdmin>`](#using-openapiadmin) to connect your app to an API exposing an OpenAPI documentation |
26 | 38 |
|
27 |
| -```javascript |
28 |
| -import { HydraAdmin } from '@api-platform/admin'; |
| 39 | +## Using `HydraAdmin` |
| 40 | + |
| 41 | +You can use the [`<HydraAdmin>`](./components.md#hydraadmin) component exported by `@api-platform/admin` to connect your app to an API exposing a Hydra documentation. |
| 42 | + |
| 43 | +If you used Create React Admin, you can replace the content of `src/App.tsx` by: |
| 44 | + |
| 45 | +```tsx |
| 46 | +import { HydraAdmin } from "@api-platform/admin"; |
29 | 47 |
|
30 | 48 | // Replace with your own API entrypoint
|
31 | 49 | // For instance if https://example.com/api/books is the path to the collection of book resources, then the entrypoint is https://example.com/api
|
32 |
| -export default () => <HydraAdmin entrypoint="https://demo.api-platform.com" />; |
| 50 | +export const App = () => <HydraAdmin entrypoint="https://localhost" />; |
| 51 | +``` |
| 52 | + |
| 53 | +**Tip:** if you don't want to hardcode the API URL, you can [use an environment variable](https://vite.dev/guide/env-and-mode). |
| 54 | + |
| 55 | +Your new administration interface is ready! `HydraAdmin` will automatically fetch the Hydra documentation of your API and generate CRUD pages for all the resources it exposes. |
| 56 | + |
| 57 | +Type `npm run dev` to try it! |
| 58 | + |
| 59 | + |
| 60 | + |
| 61 | +**Tip:** There are more props you can pass to the `HydraAdmin` component to customize the dataProvider or the connection to Mercure. Check the [API documentation](./components.md#hydraadmin) for more information. |
| 62 | + |
| 63 | +**Tip:** You may also need to configure your API to set the correct CORS headers. Refer to the [Configuring CORS](#configuring-cors) section below to learn more. |
| 64 | + |
| 65 | +## Using `OpenApiAdmin` |
| 66 | + |
| 67 | +You can use the [`<OpenApiAdmin>`](./components.md#openapiadmin) component exported by `@api-platform/admin` to connect your app to an API exposing an OpenAPI documentation. |
| 68 | + |
| 69 | +If you used Create React Admin, you can replace the content of `src/App.tsx` by: |
| 70 | + |
| 71 | +```tsx |
| 72 | +import { OpenApiAdmin } from "@api-platform/admin"; |
| 73 | + |
| 74 | +// Replace with your own API entrypoint |
| 75 | +export const App = () => ( |
| 76 | + <OpenApiAdmin |
| 77 | + entrypoint="https://localhost" |
| 78 | + docEntrypoint="https://localhost/docs.jsonopenapi" |
| 79 | + /> |
| 80 | +); |
33 | 81 | ```
|
34 | 82 |
|
| 83 | +**Tip:** if you don't want to hardcode the API URL, you can [use an environment variable](https://vite.dev/guide/env-and-mode). |
| 84 | + |
| 85 | +Your new administration interface is ready! `OpenApiAdmin` will automatically fetch the Hydra documentation of your API and generate CRUD pages for all the resources it exposes. |
| 86 | + |
| 87 | +Type `npm run dev` to try it! |
| 88 | + |
| 89 | + |
| 90 | + |
| 91 | +**Tip:** There are more props you can pass to the `OpenApiAdmin` component to customize the dataProvider or the connection to Mercure. Check the [API documentation](./components.md#openapiadmin) for more information. |
| 92 | + |
| 93 | +**Tip:** You may also need to configure your API to set the correct CORS headers. Refer to the [Configuring CORS](#configuring-cors) section below to learn more. |
| 94 | + |
| 95 | +## Configuring CORS |
| 96 | + |
35 | 97 | Be sure to make your API send proper [CORS HTTP headers](https://developer.mozilla.org/en-US/docs/Web/HTTP/CORS) to allow
|
36 | 98 | the admin's domain to access it.
|
37 | 99 |
|
@@ -61,6 +123,4 @@ Clear the cache to apply this change:
|
61 | 123 | bin/console cache:clear --env=prod
|
62 | 124 | ```
|
63 | 125 |
|
64 |
| -Your new administration interface is ready! Type `npm start` to try it! |
65 |
| - |
66 |
| -Note: if you don't want to hardcode the API URL, you can [use an environment variable](https://create-react-app.dev/docs/adding-custom-environment-variables). |
| 126 | +**Next step:** Learn how to add more features to your generated Admin by [Customizing the Schema](./schema-org.md). |
0 commit comments