<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <channel>
    <title>Forem: Marco Polichetti</title>
    <description>The latest articles on Forem by Marco Polichetti (@gremo).</description>
    <link>https://forem.com/gremo</link>
    <image>
      <url>https://media2.dev.to/dynamic/image/width=90,height=90,fit=cover,gravity=auto,format=auto/https:%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Fuser%2Fprofile_image%2F1106192%2F84918494-a626-4ef3-befc-3f2d9680a2f5.png</url>
      <title>Forem: Marco Polichetti</title>
      <link>https://forem.com/gremo</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://forem.com/feed/gremo"/>
    <language>en</language>
    <item>
      <title>API Platform up and running in 5 minutes 🚀</title>
      <dc:creator>Marco Polichetti</dc:creator>
      <pubDate>Fri, 07 Jul 2023 18:08:47 +0000</pubDate>
      <link>https://forem.com/gremo/api-platform-up-and-running-in-5-minutes-1ko2</link>
      <guid>https://forem.com/gremo/api-platform-up-and-running-in-5-minutes-1ko2</guid>
      <description>&lt;p&gt;&lt;a href="https://api-platform.com/"&gt;API Platform&lt;/a&gt; is a framework for API-first projects, built on top of Symfony components. Let's see how to create a minimal and lightweight starter project in just 5 minutes!&lt;/p&gt;

&lt;p&gt;The only requirements are &lt;a href="https://www.docker.com/products/docker-desktop"&gt;Docker Desktop&lt;/a&gt;, &lt;a href="https://code.visualstudio.com/"&gt;Visual Studio Code&lt;/a&gt; with the &lt;a href="https://marketplace.visualstudio.com/items?itemName=ms-vscode-remote.remote-containers"&gt;Dev Containers extension&lt;/a&gt; installed.&lt;/p&gt;

&lt;h2&gt;
  
  
  Setup
&lt;/h2&gt;

&lt;p&gt;Download &lt;a href="https://github.com/gremo/symfony-sail/releases/latest/download/symfony-sail-p.zip"&gt;Symfony Sail&lt;/a&gt; (disclaimer, I'm the author 😊) which provides a ready-to-go developement environment for Symfony.&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;Extract the zip in a directory &lt;code&gt;api-platform-playground&lt;/code&gt;
&lt;/li&gt;
&lt;li&gt;Create an empty &lt;code&gt;.env.local&lt;/code&gt; in that folder&lt;/li&gt;
&lt;li&gt;Start Visual Studio Code, type F1 and choose &lt;em&gt;Dev Containers: Reopen in Container&lt;/em&gt;
&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;From now on, every command must be run inside Visual Studio Code built-in terminal, that is &lt;strong&gt;inside the container&lt;/strong&gt; itself.&lt;/p&gt;

&lt;p&gt;Let's download and install &lt;strong&gt;Symfony skeleton&lt;/strong&gt;:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;curl &lt;span class="nt"&gt;-O&lt;/span&gt; https://raw.githubusercontent.com/symfony/skeleton/6.2/composer.json
composer &lt;span class="nb"&gt;install&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;We need to add two &lt;strong&gt;required libraries&lt;/strong&gt; in order to be able to navigate API Platform documentation:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;composer require symfony/asset symfony/twig-pack
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;And finally the &lt;strong&gt;API Platform&lt;/strong&gt; package:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;composer require api-platform/core
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Your API documentation is available at &lt;a href="http://localhost/api/docs"&gt;http://localhost/api/docs&lt;/a&gt; 🎉🎉🎉&lt;/p&gt;

&lt;h2&gt;
  
  
  Your first resource
&lt;/h2&gt;

&lt;p&gt;Time to add the first resource! We want to &lt;strong&gt;validate&lt;/strong&gt; the input data and &lt;strong&gt;persist&lt;/strong&gt; our resource. And being lazy, we take the advantage of &lt;a href="https://symfony.com/bundles/SymfonyMakerBundle/current/index.html"&gt;MakerBundle&lt;/a&gt; also:&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;&lt;strong&gt;Do not&lt;/strong&gt; execute the Docker recipe as we already have Docker configuration!&lt;br&gt;
&lt;/p&gt;
&lt;/blockquote&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;composer require symfony/validator symfony/orm-pack symfony/maker-bundle
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Wait a moment, what about the database? Thanks to &lt;a href="https://github.com/gremo/symfony-sail"&gt;Symfony Sail&lt;/a&gt;, we already have a running database instance as &lt;code&gt;db:3306&lt;/code&gt;. Let's set the &lt;code&gt;DATABASE_URL&lt;/code&gt; in the &lt;code&gt;.env.local&lt;/code&gt; file:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;DATABASE_URL="mysql://root:@db:3306/db_name?serverVersion=10.11.2-MariaDB&amp;amp;charset=utf8mb4"
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Now we can finally generate our first resource named &lt;code&gt;BlogPost&lt;/code&gt;:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;php bin/console make:entity BlogPost &lt;span class="nt"&gt;--api-resource&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Using the wizard, add a required &lt;code&gt;title&lt;/code&gt; property of &lt;code&gt;64&lt;/code&gt; character length, then modify &lt;code&gt;App\Entity\BlogPost&lt;/code&gt; as follows:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight php"&gt;&lt;code&gt;&lt;span class="cp"&gt;&amp;lt;?php&lt;/span&gt;

&lt;span class="kn"&gt;namespace&lt;/span&gt; &lt;span class="nn"&gt;App\Entity&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

&lt;span class="c1"&gt;// ...&lt;/span&gt;
&lt;span class="kn"&gt;use&lt;/span&gt; &lt;span class="nc"&gt;Symfony\Component\Validator\Constraints&lt;/span&gt; &lt;span class="k"&gt;as&lt;/span&gt; &lt;span class="nc"&gt;Assert&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

&lt;span class="c1"&gt;// ...&lt;/span&gt;
&lt;span class="kd"&gt;class&lt;/span&gt; &lt;span class="nc"&gt;BlogPost&lt;/span&gt;
&lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="c1"&gt;// ...&lt;/span&gt;

    &lt;span class="c1"&gt;// ...&lt;/span&gt;
    &lt;span class="c1"&gt;#[Assert\NotBlank]&lt;/span&gt;
    &lt;span class="c1"&gt;#[Assert\Length(max: 64)]&lt;/span&gt;
    &lt;span class="k"&gt;private&lt;/span&gt; &lt;span class="kt"&gt;?string&lt;/span&gt; &lt;span class="nv"&gt;$title&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="kc"&gt;null&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Almost done! Generate a migration and execute it:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;php bin/console doctrine:migrations:diff
php bin/console doctrine:migrations:migrate &lt;span class="nt"&gt;-n&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Finish! 😉&lt;/p&gt;

&lt;p&gt;Test a simple POST request using the API Platform page at &lt;a href="http://localhost/api/docs"&gt;http://localhost/api/docs&lt;/a&gt;, with a simple payload like &lt;code&gt;{ title: "My first blog post!" }&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Bonus&lt;/strong&gt;: static analysis with PHPStan and formatting upon saving using PHP_CodeSniffer? 🤓&lt;/p&gt;

&lt;p&gt;Easy:&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;&lt;strong&gt;Do&lt;/strong&gt; install the recipes of these libraries.&lt;br&gt;
&lt;/p&gt;
&lt;/blockquote&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;composer require &lt;span class="nt"&gt;--dev&lt;/span&gt; phpstan/phpstan squizlabs/php_codesniffer
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



</description>
      <category>symfony</category>
      <category>php</category>
      <category>api</category>
      <category>tutorial</category>
    </item>
    <item>
      <title>I've created a new easy to use Synfony Docker enviroment</title>
      <dc:creator>Marco Polichetti</dc:creator>
      <pubDate>Thu, 22 Jun 2023 07:27:55 +0000</pubDate>
      <link>https://forem.com/gremo/ive-created-a-new-easy-to-use-synfony-docker-enviroment-3bi1</link>
      <guid>https://forem.com/gremo/ive-created-a-new-easy-to-use-synfony-docker-enviroment-3bi1</guid>
      <description>&lt;p&gt;I have been working for months in my spare time to learn Docker, the Linux shell, and the Github workflows.&lt;/p&gt;

&lt;p&gt;Project: &lt;a href="https://github.com/gremo/symfony-sail"&gt;https://github.com/gremo/symfony-sail&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Symfony Sail aims to be a &lt;strong&gt;complete&lt;/strong&gt; development and production environment that is &lt;strong&gt;easy to use&lt;/strong&gt; (just extract a zip file) and &lt;strong&gt;highly configurable&lt;/strong&gt;.&lt;/p&gt;

&lt;p&gt;I hope the community can contribute ❤️ to the project, improve it, and make the code more elegant because, I must admit, it's a bit rough in some parts.&lt;/p&gt;

</description>
      <category>docker</category>
      <category>symfony</category>
      <category>php</category>
    </item>
  </channel>
</rss>
