React Admin
Keep on Learning!
If you liked what you've learned so far, dive in! Subscribe to get access to this tutorial plus video, code and script downloads.
With a Subscription, click any sentence in the script to jump to that part of the video!
Login Subscribe¡Vaya! ¡Cuidado! ¡Capítulo extra! Sabemos que nuestra API está completamente descrita mediante la especificación Open API. Incluso podemos verlo visitando /api/docs.json. Esto muestra todas nuestras rutas y sus campos. Obtiene esta deliciosa información leyendo nuestro código, PHPdoc y otras cosas. Y sabemos que esto se utiliza para alimentar la página de documentos Swagger UI. Nuestra API también se describe mediante JSON-LD e Hydra.
Y ambos tipos de documentación de la API pueden utilizarse para otras cosas.
Por ejemplo, busca "react admin" para encontrar un sistema de administración de código abierto basado en React. Esto es súper potente y genial... y existe desde hace mucho tiempo. Y la forma en que funciona es... increíble: lo apuntamos a nuestra documentación de la API y luego... ¡se construye solo! Creo que deberíamos probarlo.
Busca "api platform react admin" para encontrar la página de documentación de la API Platform que trata de esto. Tiene algo de información... pero lo que realmente buscamos está aquí. Haz clic en "Empezar". Esto nos guía a través de todos los detalles, incluyendo incluso la configuración CORS si tienes ese problema.
Así que... ¡hagámoslo!
Configuración de Webpack Encore
Si utilizas la distribución Docker de API Platform, esta área de administración viene preinstalada, pero también es bastante fácil añadirla manualmente. Ahora mismo, nuestra aplicación no tiene JavaScript, así que tenemos que arrancarlo todo. Busca tu terminal y ejecuta:
composer require encore
Esto instala WebpackEncoreBundle... y su receta nos da una configuración básica del frontend. Una vez hecho esto, instala los activos de Node con:
npm install
Bien, vuelve a los documentos. API Platform tiene su propio paquete Node que ayuda a integrarse con el administrador. Así que vamos a instalarlo. Copia la líneanpm install -también puedes usar yarn si quieres-, pégala en el terminal y añade un -D al final.
npm install @api-platform/admin -D
Ese -D no es super importante, pero yo suelo instalar mis activos como devDependencies.
Configuración UX React
Para que todo esto funcione, en última instancia, vamos a renderizar un único componente React en una página. Para ayudarte con eso, voy a instalar un paquete UX que es... realmente bueno renderizando componentes React. Es opcional, pero bueno.
Ejecuta:
composer require symfony/ux-react
Perfecto. Ahora, gira y busca "symfony ux react" para encontrar su documentación. Copia este código de instalación: tenemos que añadirlo a nuestro archivo app.js... aquí enassets/. Pégalo... y no necesitamos todos estos comentarios. También moveré este código debajo de las importaciones.
| // ... lines 1 - 12 | |
| import './bootstrap'; | |
| registerReactControllerComponents(require.context('./react/controllers', true, /\.(j|t)sx?$/)); |
¡Increíble! Esto básicamente dice que buscará en un directorio assets/react/controllers/y hará que cada componente React que haya dentro sea súper fácil de renderizar en Twig. Así que, vamos a crearlo: en assets/, añade dos nuevos directorios:react/controllers/. Y luego crea un nuevo archivo llamado ReactAdmin.jsx.
Para el contenido, vuelve a los documentos de la API Platform... y nos da casi exactamente lo que necesitamos. Copia esto... y pégalo dentro de nuestro nuevo archivo. Pero antes, no lo parece, pero gracias a la sintaxis JSX, estamos utilizando React, así que necesitamos un import React from 'react'.
Y... asegurémonos de que lo tenemos instalado:
npm install react -D
Pasando un Prop al Componente React
En segundo lugar, echa un vistazo al prop entrypoint. Esto es genial. Pasamos la URL a nuestra página de inicio de la API... y luego React admin se encarga del resto. Para nosotros, esta URL sería algo como https://localhost:8000/api. Pero... Prefiero no codificar "localhost" en mi JavaScript.
En lugar de eso, vamos a pasarlo como una propiedad. Para permitirlo, añade un argumento props... y luego di props.entrypoint.
| import { HydraAdmin } from "@api-platform/admin"; | |
| import React from 'react'; | |
| export default (props) => ( | |
| <HydraAdmin entrypoint={props.entrypoint} /> | |
| ); |
¿Cómo lo introducimos? Lo veremos en un minuto.
Activar React en Encore
Muy bien, veamos si el sistema llega a construirse. Enciéndelo:
npm run watch
Y... ¡error de sintaxis! Ve esta sintaxis .jsx y... ¡no sabe qué hacer con ella! Eso es porque aún no hemos activado React dentro de WebpackEncore. Pulsa Ctrl+C para detenerlo... luego gira y abre webpack.config.js. Busca un comentario que diga .enableReactPreset(). Ahí lo tienes. Descomenta eso.
| // ... lines 1 - 8 | |
| Encore | |
| // ... lines 10 - 64 | |
| // uncomment if you use React | |
| .enableReactPreset() | |
| // ... lines 67 - 77 |
Ahora, cuando volvamos a ejecutar
npm run watch
de nuevo... ¡seguirá sin funcionar! Pero nos da el comando que necesitamos para instalar el único paquete que falta para que React sea compatible Cópialo y ejecútalo:
npm install @babel/react-preset@^7.0.0 --save-dev
Y ahora cuando probemos
npm run watch
... ¡funciona! Es hora de renderizar ese componente React.
Renderizando el componente ReactAdmin
¿Cómo lo hacemos? Esta es la parte fácil. En src/Controller/, crea una nueva clase PHP llamada AdminController. Probablemente será el controlador más aburrido que jamás hayas creado. Haz que extienda AbstractController, y luego añade un public function llamado dashboard(), que devolverá un Response, aunque eso es opcional. Encima de esto, añade un Route() para /admin.
Todo lo que necesitamos dentro es return $this->render() y luego una plantilla: admin/dashboard.html.twig.
| // ... lines 1 - 2 | |
| namespace App\Controller; | |
| use Symfony\Bundle\FrameworkBundle\Controller\AbstractController; | |
| use Symfony\Component\HttpFoundation\Response; | |
| use Symfony\Component\Routing\Annotation\Route; | |
| class AdminController extends AbstractController | |
| { | |
| ('/admin') | |
| public function dashboard(): Response | |
| { | |
| return $this->render('admin/dashboard.html.twig'); | |
| } | |
| } |
¡Genial! Abajo, en el directorio templates/, crea ese directorio admin/... y dentro, un nuevo archivo llamado dashboard.html.twig. De nuevo, ésta es probablemente una de las plantillas más aburridas que harás nunca, al menos al principio. Amplíabase.html.twig y añade block body y endblock.
Ahora, ¿cómo renderizamos el componente React? Gracias a ese paquete UX React, es superfácil. Crea el elemento en el que debe renderizarse y añadereact_component() seguido del nombre del componente. Como el archivo se llamaReactAdmin.jsx en el directorio react/controllers/, su nombre será ReactAdmin.
| {% extends 'base.html.twig' %} | |
| {% block body %} | |
| <div {{ react_component('ReactAdmin', { | |
| // ... line 5 | |
| }) }}></div> | |
| {% endblock %} |
Y aquí es donde pasamos los accesorios. Recuerda: tenemos uno llamado entrypoint. Ah, pero déjame arreglar mi sangría... y recuerda añadir el </div>. No necesitamos nada dentro del div... porque ahí es donde aparecerá mágicamente el área de administración de React, como un conejo salido de una chistera.
Pasa el prop set entrypoint a la función normal path(). Ahora, sólo tenemos que averiguar el nombre de ruta que API Platform utiliza para la página de inicio de la API. Esta pestaña está ejecutando npm... así que abriré una nueva pestaña de terminal y la ejecutaré:
php bin/console debug:router
¡Woh! Demasiado grande. Así está mejor. Desplázate un poco hacia arriba y... aquí está. Queremos:api_entrypoint. Vuelve y pásalo.
| {% extends 'base.html.twig' %} | |
| {% block body %} | |
| <div {{ react_component('ReactAdmin', { | |
| entrypoint: path('api_entrypoint') | |
| }) }}></div> | |
| {% endblock %} |
¡Momento de la verdad! Busca tu navegador, cambia la dirección a /admin, y... ¡hola ReactAdmin! ¡Woh! Entre bastidores, eso hizo una petición a nuestro punto de entrada de la API, vio todos los diferentes recursos de la API que tenemos, ¡y creó este admin! Lo sé, ¿no es una locura?
No profundizaremos demasiado en esto, aunque puedes personalizarlo y casi seguro que necesitarás personalizarlo. No es perfecto: parece un poco confuso por nuestro dragonTreasures incrustado, pero ya es muy potente. ¡Incluso la validación funciona! Observa: cuando envío, lee la validación del lado del servidor devuelta por nuestra API y asigna cada error al campo correcto. Y los tesoros conocen nuestros filtros. ¡Todo está aquí!
Si esto te parece interesante, no dudes en seguir investigando.
¡Muy bien, equipo! ¡Lo habéis conseguido! Has superado el primer tutorial sobre la API Platform, que es fundamental para todo. Ahora entiendes cómo se serializan los recursos, cómo se relacionan los recursos con otros recursos, los IRI, etc. Todas estas cosas te van a servir para cualquier API que estés construyendo. En el próximo tutorial, hablaremos de usuarios, seguridad, validación personalizada, campos específicos de usuario y otras cosas extravagantes. Cuéntanos qué estás construyendo y, si tienes alguna pregunta, estamos a tu disposición en la sección de comentarios.
¡Muy bien, amigos! ¡Hasta la próxima!
28 Comments
If you are also running into this error:
removing 'doctrine/annotations' might fix it:
To fix this I changed the composer.json to include "minimum-stability": "beta". Then I did a
composer update.Hey FR,
At what point did you get that error? Did you download the course code, or you're following the course by yourself?
I did download the course code and coded along. The error popped up in this chapter or the end of the last chapter.
Hey @FR!
There definitely IS some weird stuff happening right now, as the ecosystem transitions away from annotation - e.g. https://github.com/symfony/symfony/issues/48792
But I can't get this to repeat using the code - I've tried doing
composer installon thefinishcode as well as acomposer update. We MIGHT have a dependency that needs upgrading, but I can't trigger it. @FR - do you see the error when you runcomposer instalor do you need to actually use an endpoint?Thanks!
Thank you for the course!
You're welcome!
Hey there!
First of all, thanks for your nice Videos!
Really having fun here :)
Except, when it comes to ReactAdmin...
I tried it two times now with setting up the whole api from scratch (had to downgrade doctrine/orm to 2.18, because apparently 3.00 cant handle ManyToMany Relationsships anymore), BUT
I always get a white screen!
Apparently, react does something, if I inspect my element, this is what it looks like:
This is my twig template:
My ReactAdmin:
The only difference was, that I was using @babel/preset-react: 7.0.0 instead of 7.18.6
I tried to update the version, npm install, but still: white screen.
I see a loading circle for a short moment and I can see, that the system made 3 Ajax Requests:
/api
/api/docs.jsonld
/api/contexts/Entrypoint
You got any idea, how I can get it to work?
Thanks and greetings
Maik
Hey @Maik-D!
Apologies for my epically slow reply!
Hmm. Let's look through piece-by-piece:
This tells me that you're outputting the right stuff. And the fact that you see the 3 AJAX requests is what really convinces me that React has, at least, started. But no errors and nothing else on the page? Tbh, I can't think of how the
HydraAdminwould obviously start (HydraAdmincode is what makes those Ajax calls)...but then it doesn't do anything or show an error. Do the responses in those 3 Ajax calls look correct? Can you see that they're all returning JSON?Cheers!
Hi Everybody :)
First, I would like to thank you for this superb course.
Everything went good for me, except the last step,
npm install @babel/react-preset@^7.0.0 --save-devDidn't work : '@babel/react-preset@7.0.0' is not in this registry,
so I ran :
npm install @babel/preset-react@^7.0.0 --save-devThen, when loading the admin page, I got several JS errors/warnings, here are the first two logs :
which I couldn't resolve
Any idea ? I am on Symfony 6.2.14 by the way.
Thanks in advance :)
Hey @Hamza-Y!
Yikes! That is a huge, ugly error. To be honest, I have no clue what's gong on :/. I'd recommend running
npm outdatedto, perhaps, see if any packages are out-of-date. This certainly feels like different packages of different versions not playing nicely with each other - but I'm guessing.Sorry I can't be more helfpul!
Sadly the Hydra Admin is actually not working in Symfony 7.0.1, i also tested out 6.4 but i had still no luck setting it up there :(
Starting the moment an Entity has been added to the Api Platform (which works on the PHP site well) the Hydra Admin is crashing after loading.
Hey @Sebastian
Forgive my ignorance but is the Hydra Admin a project of ApiPlatform? Perhaps you could create an issue on their GitHub repository
Cheers!
I think that the React Admin from api-platform/admin is based on HydraAdmin (correct me if im wrong). I tested both because i wanted to know if the base of it makes the problem or api-platform/admin itself :)
If someone wants a (temporary) solution, here is a way to fix it:
https://github.com/api-platform/admin/issues/522
Yes, I think you're right.
It's a weird error, I don't know what could be the issue, but thank you for sharing a workaround!
Cheers!
Hey, React Admin is so cool, thanks for introducing it in this amazing course!
I am adopting it now, and I found out that HMR is currently NOT supported for React out of the box. I had to do some research to make it work with Symfony + Webpack Encore + React, and I wanted to share it here to help others save time and efforts.
What you need is React Refresh Webpack Plugin: https://github.com/pmmmwh/react-refresh-webpack-plugin/. Here's how to set it up with Webpack Encore:
If you use Symfony local webserver with SSL, you also need to enable HTTPS for
webpack-dev-server, otherwise you'll see error in the browser's console about refresh plugin not being able to connect to websocket:NOTE: this is the setup without Stimulus Bridge.
Ah, this is awesome! Thanks for sharing this - seriously!!
I am quite sure I'll come here myself a lot of times in the future after googling "how to enable HMR for react"😂
Hi, i'm user a project with Symfony 6.3.1, all work fine, but in ReactAdmin i have this error on my console :
I found the problem, i added my 2nd ApiResource before the first on my DragonTreasure class. I know now we have to add ApiResource AFTER the first one
Thank you for sharing your solution. Cheers!
Congratulations. One more masterpiece. Keep up excellent work.
Yo! Thanks for feedback we really appreciate it!
Cheers and Happy coding!
Hello!
In my admin panel when I go to create a user it only requires the username but not the password and the email, also when I want to create or modify an object it always gives me the error 422 because one of my assertions jumps without me having touched that field.
Any ideas?
Hi @Fran!
Hmm.
Do you mean that it doesn't show your email / password fields? Or that it shows them, but they are not required?
What do you mean by "the assertion jumps"?
But, I can maybe give some hints :). If you haven't done it already,
passwordwill need a validation group to be added - https://symfonycasts.com/screencast/api-platform-security/validation-groups - so that it isn't always required. This may or may not be your issue, but I wanted to mention it :).Cheers!
Hello,
How can i fetch owner(data who owner is email from token) data if i use JWT token ?
Regards.
Mmx
Hey @Mepcuk!
So you have a JWT and that JWT contains the
emailof the user? Is that correct? We'll talk closer to this use-case (though not this exactly) in the next tutorial. But I would:A) Use the new
access_tokensystemB) In the "access token handler" class you'll create, you'll decode the JWT to get the email
C) Then, return
new UserBadge($email). As long as your have an "entity" user provider insecurity.yamlset up to query from theemailproperty... that's all you need.Let me know if that helps :).
Cheers!
Hey @weaverryan!
I think you did not understand my question - I have a Get operation -
It's work perfect and i can get information if i know ID :))) If not resricted.
But i want to make resourse where i can get all data (relations) related to this user (i login via JWT token)
I try to get Collection but it not worked
Security key did not described in API=platform docs and i don't know how to fetch collection assigned-related to user with this JWT token. You told that in security: possible to write user check, but how?
Hey @Mepcuk!
Haha, that happens to me a lot - apologies :)
Let's see... Question: how are you using
GetCollectionoperation? Are you using anApiFilterto filter by email - e.g./api/loans?email=foo@example.com? Or something else?When you use a
GetCollectionendpoint, the actual "object" is not, of course, a singleLoanPersonobject, but an array (or technically aCollection) orLoanPersonobjects. You tried usingcollection.getEmail()- but I don't quite understand yet what you were trying to do.You also said:
If I understand correctly, you would like to be able to make a
GET /api/loansand receive back only the loans "owned" by the currently-authenticated user. Is that correct? If so, solving this is less about security and more about filtering the data (from a security perspective, all users will have access to fetch their collection of loans, but they should only see their own loans). For this, I would use a "query extension" - https://symfonycasts.com/screencast/api-platform-security/query-extension - to automatically filter this. It doesn't matter that the user is inside of a JWT. It only matters that you fetch the currently-authenticated user, then modify the query based on that user inside the query extension.Let me know if I was closer this time :)
Cheers!
"Houston: no signs of life"
Start the conversation!