Maker Bundle: ¡Generemos algo de código!
Me quito el sombrero por haber superado casi por completo el primer tutorial de Symfony. Has dado un gran paso hacia la construcción de lo que quieras en la web. Para celebrarlo, quiero jugar con MakerBundle: La impresionante herramienta de Symfony para la generación de código.
Composer require vs require-dev
Vamos a instalarlo:
composer require symfony/maker-bundle --dev
Aún no hemos visto la bandera --dev, pero no es tan importante. Muévete y abre composer.json. Gracias a la bandera, en lugar de que symfony/maker-bundle vaya bajo la clave require, se ha añadido aquí abajo, bajo require-dev.
| { | |
| // ... lines 2 - 84 | |
| "require-dev": { | |
| // ... line 86 | |
| "symfony/maker-bundle": "^1.52", | |
| // ... lines 88 - 89 | |
| } | |
| } |
Por defecto, cuando ejecutes composer install, descargará todo lo que esté bajo requirey require-dev. Pero require-dev está pensado para paquetes que no necesitan estar disponibles en producción: paquetes que sólo necesitas cuando desarrollas localmente. Esto se debe a que, cuando despliegues, si quieres, puedes decirle a Composer:
¡Eh! Instala sólo los paquetes de mi clave
require: no instales las cosas derequire-dev.
Eso puede darte un pequeño aumento de rendimiento en producción. Pero, en general, no es gran cosa.
Los comandos Maker
Acabamos de instalar un bundle. ¿Recuerdas lo principal que nos proporcionan los bundles? Exacto: servicios. Esta vez, los servicios que nos ha proporcionado MakerBundle son servicios que proporcionan nuevos comandos de consola. Redoble de tambores, por favor. Ejecuta:
php bin/console
O, en realidad, empezaré a ejecutar symfony console, que es lo mismo. ¡Gracias al nuevo bundle, tenemos un montón de comandos que empiezan por make! Comandos para generar un sistema de seguridad, hacer un controlador, generar entidades de doctrina para hablar con la base de datos, formularios, oyentes, un formulario de registro.... ¡muchas, muchas cosas!
Generar un comando de consola
Utilicemos uno de éstos para crear nuestro propio comando de consola personalizado. Ejecuta:
symfony console make:command
Esto nos preguntará interactivamente por nuestro comando. Llamémoslo: app:ship-report. ¡Listo!
Esto ha creado exactamente un archivo: src/Command/ShipReportCommand.php. ¡Vamos a comprobarlo!
| // ... lines 1 - 2 | |
| namespace App\Command; | |
| use Symfony\Component\Console\Attribute\AsCommand; | |
| use Symfony\Component\Console\Command\Command; | |
| use Symfony\Component\Console\Input\InputArgument; | |
| use Symfony\Component\Console\Input\InputInterface; | |
| use Symfony\Component\Console\Input\InputOption; | |
| use Symfony\Component\Console\Output\OutputInterface; | |
| use Symfony\Component\Console\Style\SymfonyStyle; | |
| ( | |
| name: 'app:ship-report', | |
| description: 'Add a short description for your command', | |
| ) | |
| class ShipReportCommand extends Command | |
| { | |
| public function __construct() | |
| { | |
| parent::__construct(); | |
| } | |
| protected function configure(): void | |
| { | |
| $this | |
| ->addArgument('arg1', InputArgument::OPTIONAL, 'Argument description') | |
| ->addOption('option1', null, InputOption::VALUE_NONE, 'Option description') | |
| ; | |
| } | |
| protected function execute(InputInterface $input, OutputInterface $output): int | |
| { | |
| $io = new SymfonyStyle($input, $output); | |
| $arg1 = $input->getArgument('arg1'); | |
| if ($arg1) { | |
| $io->note(sprintf('You passed an argument: %s', $arg1)); | |
| } | |
| if ($input->getOption('option1')) { | |
| // ... | |
| } | |
| $io->success('You have a new command! Now make it your own! Pass --help to see your options.'); | |
| return Command::SUCCESS; | |
| } | |
| } |
¡Genial! Esta es una clase normal - es un servicio, por cierto - pero con un atributo encima: #[AsCommand]. Esto le dice a Symfony:
¡Eh! ¿Ves este servicio? No es sólo un servicio: Me gustaría que lo incluyeras en la lista de comandos de la consola.
El atributo incluye el nombre del comando y una descripción. Además, la propia clase tiene un método configure() en el que podemos añadir argumentos y opciones. Pero la parte principal es que, cuando alguien llame a este comando, Symfony llamará a execute().
Esta variable $io es genial. Nos permite mostrar cosas -como $this->note()o $this->success() - con diferentes estilos. Y aunque no lo veamos aquí, también podemos hacer preguntas al usuario de forma interactiva.
¿Y lo mejor? Con sólo crear esta clase, ¡ya está lista para usar! Pruébala:
symfony console app:ship-report
¡Qué guay! El mensaje de aquí abajo procede del mensaje de éxito de la parte inferior del comando. Y gracias a configure(), tenemos un argumento llamadoarg1. Los argumentos son cadenas que pasamos después del comando, como:
symfony console app:ship-report ryan
Dice
Has pasado un argumento: ryan
... que viene de este lugar del comando.
Construir una barra de progreso
Hay muchas cosas divertidas que puedes hacer con los comandos... y quiero jugar con una de ellas. Uno de los superpoderes del objeto $io es crear barras de progreso animadas.
Imagina que estamos construyendo un informe sobre un barco... y requiere algunas consultas pesadas. Así que queremos mostrar una barra de progreso en la pantalla. Para ello, decimos $io->progressStart()y le pasamos el número de filas de datos que estemos recorriendo y manejando. Imaginemos que estamos haciendo un bucle sobre 100 filas de datos para este informe.
En lugar de hacer un bucle sobre datos reales, crea un bucle falso con for. ¡Incluso voy a incluir la variable $i en el medio! Dentro, para hacer avanzar la barra de progreso, di $io->advance(). Entonces, aquí es donde haríamos nuestra consulta pesada o trabajo pesado. Finge eso con un usleep(10000) para crear una breve pausa.
Después del bucle, termina con $io->progressFinish().
| // ... lines 1 - 16 | |
| class ShipReportCommand extends Command | |
| { | |
| // ... lines 19 - 31 | |
| protected function execute(InputInterface $input, OutputInterface $output): int | |
| { | |
| // ... lines 34 - 44 | |
| $io->progressStart(100); | |
| for ($i = 0; $i < 100; ++$i) { | |
| $io->progressAdvance(); | |
| usleep(10000); | |
| } | |
| $io->progressFinish(); | |
| // ... lines 51 - 54 | |
| } | |
| } |
Ya está Gira y pruébalo:
symfony console app:ship-report ryan
Qué guay.
Y... ¡eso es todo, gente! Choca esos cinco contigo mismo... o, mejor, ¡sorprende a un compañero de trabajo con un choca esos cinco saltarín! Después, celébralo con una merecida cerveza, un té, un paseo por la manzana o un partido de frisbee con tu perro. Porque... ¡lo has conseguido! Has dado el primer gran paso para ser peligroso con Symfony. Entonces, vuelve y prueba estas cosas: juega con ellas, construye un blog, crea unas cuantas páginas estáticas, lo que sea. Eso marcará una gran diferencia.
Y si alguna vez tienes alguna pregunta, miramos atentamente la sección de comentarios debajo de cada vídeo y respondemos a todo. Además, ¡sigue adelante! En el próximo tutorial, vamos a ponernos aún más peligrosos profundizando en la configuración y los servicios de Symfony: los sistemas que dirigen todo lo que harás en Symfony.
Muy bien, amigos, ¡hasta la próxima!
37 Comments
This whole course was incredibly clear and helpful.
Two thumbs up!
Thanks @Sam35!
The course is amazing! Thank you so much 🥰
We're glad you liked it!
I am an enterprise developer with very little front-end knowledge, and following this course was super easy and very enlightening. It was presented in a fun way (live long and prosper ;-) and when I was stuck (once), Ryan responded very quickly. I am impressed and considering spending money to access the rest of the courses.
As a developer who is mostly doing ERP code work and interacting with a web developer team that manages our company web site written in Drupal (which is written in Symfony), this will help me communicate better with the off-shore dev team and also understand their responses. Hopefully I can also make some small code changes, but at the very least I may be able to understand some Drupal code better.
Finally I want to add, that I am pretty much a one man show at the company next to the CEO who also does some ERP code work. I am taking on many tasks that go far and beyond his capabilities by doing lots of research. We have two projects at the company that I intend to turn into Symfony projects. Both will heavily depend on database interaction with our ERP system. This should be fun!
So far I am very impressed with how easy coding in Symfony is, but now I need to take the leap to accessing database entities which is the meat and potato of my overall work.
Thank you, Ryan, for being a great presenter!
Hey Kay,
Thank you for such kind words about this course! You made our day ❤️
You will find the Doctrine (DB entities) course in the Symfony 7 track too: https://symfonycasts.com/tracks/symfony . However, the next one would be better to watch: https://symfonycasts.com/screencast/symfony-fundamentals/ - that will be the correct course order to follow :)
Cheers!
This course is exceptionally well designed. Thank you...
You're welcome :]
Nice sessions. There is a lot with Symfony that was unknown to me. The Stimulus and turbo are killers.
it is really dangerous now. See you in the next session. Thank you!
Hey @Abhi
In that case, I bet you'll love this other course https://symfonycasts.com/screencast/last-stack
Cheers!
I started with HTML, CSS, and JS two years ago. I've been using PHP and SQL for four months now. Now I discovered Symfony, and it's fantastic, just like this tutorial! I laughed a few times. I loved the phrase "Super Nice." Super Nice! Thanks!
I'm happy to hear it! I'll assure Ryan reads this :]
Really great work on this course and thanks for providing it for free. I think I'm hooked on Symfony now.
Hey Marshall,
Thank you for this feedback and your interest in SymfonyCasts tutorials! We're super happy to hear you liked this free course, and hearing that you're hooked on Symfony is 🖤
Happy coding with Symfony!
Cheers!
I love this series. When I first started learning Symfony, I was so confused. Now, I have a clear image of Symfony, and I am ready to dive deep into it
Hey mr_kava,
We're happy to hear you love it ❤️ Stay tuned for more cool tutorials :)
Cheers!
Great tutorial, thanks! Learned a lot, and yeah Symfony is cool
You're welcome!
Thanks for the course, this is great stuff
Hey Leed,
Thank you for your feedback!
Cheers!
Awesome! loved this first course! thank-you for putting it together!
Hey Richard,
Thank you for the kind words about this course! We're happy to hear you liked it! :)
Cheers!
A very busy week to successfully complete this course during it. Thank you very much!!!
Hey Erick,
You made it, well done! 👏
Cheers!
It is so nice and joyful to see Ryan back as the instructor in this course. Very handy content, expect to see many other new courses like that. Thanks to all SF team!
Hey @iclaborda!
Unfortunately, I'm not back as an instructor (this course was from before all the craziness), but I am slowly working more with Kevin (and our other behind-the-scenes helpers) to keep up the same quality and fun. And hopefully, to accelerate our content-creation pace! A lot of cool work happening!
Cheers!
Hey @iclaborda ,
Thank you for your feedback! ❤️ Stay tuned for more courses ;)
Cheers!
Great, Thanks a lot
Excellent, I've learned a lot and enjoyed it very much
Hey @tomoguchi ,
Thank you for your feedback! We're delighted to hear you enjoyed it!
Cheers!
Beautiful and very useful course. Thank you so much!
Hey Jared,
Thank you for your feedback! We're thrilled to hear you liked this course and that it was useful for you :)
Stay tuned for more content ;)
Cheers!
Hello,
Do you have any course for integration symfony+react +typescript together?
Hey @Mahmut-A
We have a tutorial about Symfony + React 16. React is integrated through Symfony Encore, which uses Webpack under the hoods. Ryan does not use Typescript in that tutorial but it should be easy to support it by configuring Webpack.
Tutorial link: https://symfonycasts.com/screencast/reactjs
Cheers!
thank you. I will try to follow below link also.
https://symfony.com/bundles/ux-react/current/index.html#usage
That was so good!
Thanks!
Hey Saidazim,
We're glad to hear this video was useful for you!
Cheers!
"Houston: no signs of life"
Start the conversation!