<?xml version="1.0" encoding="UTF-8"?><rss xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:atom="http://www.w3.org/2005/Atom" version="2.0" xmlns:cc="http://cyber.law.harvard.edu/rss/creativeCommonsRssModule.html">
    <channel>
        <title><![CDATA[Stories by Antoine Lamé on Medium]]></title>
        <description><![CDATA[Stories by Antoine Lamé on Medium]]></description>
        <link>https://medium.com/@antoine.lame?source=rss-bb104292599f------2</link>
        <image>
            <url>https://cdn-images-1.medium.com/fit/c/150/150/1*V92RpEQ6CkEzgOJoXvGdRA.jpeg</url>
            <title>Stories by Antoine Lamé on Medium</title>
            <link>https://medium.com/@antoine.lame?source=rss-bb104292599f------2</link>
        </image>
        <generator>Medium</generator>
        <lastBuildDate>Thu, 19 Nov 2026 17:26:10 GMT</lastBuildDate>
        <atom:link href="https://medium.com/@antoine.lame/feed" rel="self" type="application/rss+xml"/>
        <webMaster><![CDATA[yourfriends@medium.com]]></webMaster>
        <atom:link href="http://medium.superfeedr.com/" rel="hub"/>
        <item>
            <title><![CDATA[How to generate OpenAPI specs for Laravel?]]></title>
            <link>https://medium.com/@antoine.lame/how-to-generate-openapi-specs-for-laravel-e0f402da0c3e?source=rss-bb104292599f------2</link>
            <guid isPermaLink="false">https://medium.com/p/e0f402da0c3e</guid>
            <category><![CDATA[laradoc]]></category>
            <category><![CDATA[openapi-generator]]></category>
            <category><![CDATA[laravel]]></category>
            <category><![CDATA[api-documentation]]></category>
            <category><![CDATA[open-api]]></category>
            <dc:creator><![CDATA[Antoine Lamé]]></dc:creator>
            <pubDate>Wed, 29 Mar 2023 14:05:35 GMT</pubDate>
            <atom:updated>2023-03-29T14:05:35.056Z</atom:updated>
            <content:encoded><![CDATA[<figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*c3odY_pDaxCcNHtwqghe7w.png" /><figcaption>uselaradoc.com</figcaption></figure><p>As a Laravel user, I’m a huge fan of the framework’s incredible developer experience. It lets me create APIs super fast and with minimal effort. However, this often results in spending almost an equal amount of time maintaining the API documentation.</p><p>Writing and updating API documentation can be a real headache. That’s why I created <a href="http://uselaradoc.com/">Laradoc</a> — a platform that automatically generates the OpenAPI specifications of Laravel projects without requiring any PHP annotation.</p><p>Since launching Laradoc a few weeks ago, it has received 384 sign-ups, and over 200 API documentations have been created. This helped me to get feedback and fix many issues. As the beta phase is over, I’m excited to open registrations for everyone and show you how it can help you with your projects.</p><h3>Writing API documentation is fastidious</h3><h4>Postman</h4><p>In 2019, I worked at <a href="https://aspireapp.com/">Aspire</a>. We were using Postman to document our APIs. However, being a young company with a rapidly evolving product, we lacked the resources to keep the collections up to date.</p><h4>Slate</h4><p>In 2020, I founded my consulting company, worked on many APIs, and documented them with <a href="https://github.com/slatedocs/slate">Slate</a>. I enjoyed the rendering and markdown’s flexibility. However, updating a response schema soon became a nightmare, as even a single change required editing dozens of examples.</p><h4>Scrible</h4><p>In 2022, I co-founded Telemonica, a healthcare company that provides patient data centralization. We generated OpenAPI specs with <a href="https://scribe.knuckles.wtf/laravel/">Scrible</a>. While it worked well, it required adding PHP annotations. Forgetting to update them when changing a request was easy.</p><p>Frustrated with not having a satisfying solution to manage my API documentation, I started working on a small side-project, <a href="https://uselaradoc.com/">Laradoc</a>.</p><h3>How automatize OpenAPI specification?</h3><p>I built <a href="http://uselaradoc.com/">Laradoc</a>, a solution that <strong>generates OpenAPI specifications </strong>because writing them by hand is not only a tedious task; it’s a surefire way to end up with outdated API documentation.</p><p>Furthermore, I avoided requiring PHP annotations as I prefer minimalism and dislike having extensive DocBlocks at the top of my controllers. This was quite a challenge, but I leveraged static analysis to extract request parameters, validation rules, and response schemas.</p><p>It doesn’t matter if you prefer FormRequest or inline validation, returning arrays or JsonResource, or even skipping validation — <a href="http://uselaradoc.com/">Laradoc</a> handles it all!</p><p>Take the following Laravel controller:</p><pre>public function search()<br>{<br>    request()-&gt;validate([<br>        &#39;email&#39; =&gt; [&#39;required&#39;, &#39;email&#39;],<br>    ]);<br><br>    $patient = User::query()<br>        -&gt;whereRole(&#39;patient&#39;)<br>        -&gt;whereEmail(request(&#39;email&#39;))<br>        -&gt;firstOrFail();<br><br>    return new UserResource($patient);<br>}</pre><p>Laradoc transforms it to these nice OpenAPI specifications:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*0PXBbgWIczUfKklwJgCOPw.png" /><figcaption>Even the endpoint title is automatically generated!</figcaption></figure><p>As you can see in the screenshot above, a real-world API response example is provided. This has been made possible by running project tests and capturing transmitted data in the responses.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*JwPUOnAJMs58IeB9kHNYpA.png" /><figcaption>Feature tests in your project allow Laradoc to add response examples</figcaption></figure><h3>Embedded OpenAPI viewer</h3><p>Once Laradoc has analyzed your Laravel project, you can choose between downloading the OpenAPI specification file (JSON) or opening them on the application.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*MMd8KX1eVHzmya8qoQim5Q.png" /><figcaption>Open or download?</figcaption></figure><p>When you open it on the app, you get a beautiful API documentation viewer powered by <a href="https://stoplight.io/open-source/elements">Spotlight</a>. It allows you to easily navigate between your API endpoints and see the parameters accepted by a request, as well as the response schema you can expect.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*UKEUmecY6uHk904zX-YZVg.png" /><figcaption>API documentation viewer on Laradoc</figcaption></figure><p>One last thing, team collaboration – after launching Laradoc, a user requested that I implement access controls to allow his collaborators access to his API documentation. This has now been done, allowing you to invite team members to your project.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*CTqTHHNQPZ3q4eI48oVJVg.png" /><figcaption>Onboard your team members!</figcaption></figure><p>Laradoc is now stable but still in an early stage. If you decide to give it a shot, your feedback is more than welcome!</p><p>➡️ <a href="https://uselaradoc.com/">Laradoc website</a><br>➡️ <a href="https://app.uselaradoc.com/register">Create an account</a><br>➡️ <a href="https://uselaradoc.com/docs/introduction">Documentation</a></p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=e0f402da0c3e" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Add Cloudflare R2 storage to Laravel in 5 minutes]]></title>
            <link>https://medium.com/@antoine.lame/cloudflare-r2-storage-with-laravel-in-5-minutes-553a5403c6f8?source=rss-bb104292599f------2</link>
            <guid isPermaLink="false">https://medium.com/p/553a5403c6f8</guid>
            <category><![CDATA[web-development]]></category>
            <category><![CDATA[laravel]]></category>
            <category><![CDATA[s3]]></category>
            <category><![CDATA[cloudflare]]></category>
            <category><![CDATA[aws]]></category>
            <dc:creator><![CDATA[Antoine Lamé]]></dc:creator>
            <pubDate>Fri, 30 Dec 2022 15:07:19 GMT</pubDate>
            <atom:updated>2023-06-26T14:11:37.898Z</atom:updated>
            <content:encoded><![CDATA[<h3>Cloudflare R2 storage with Laravel in 5 minutes</h3><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*fvtlkg2bBoGWUGIj3OPXGQ.png" /></figure><p>Cloudflare R2 is an S3-compatible object storage solution, which could be an excellent alternative to AWS S3. R2 is cheaper; using it instead of AWS S3 can save you costs (75% savings in my case).</p><p>Let’s see how to create a Laravel filesystem disk that uses Cloudflare R2.</p><h3>1. Install the S3 filesystem driver</h3><pre>composer require league/flysystem-aws-s3-v3 &quot;^3.0&quot;</pre><h3>2. Create a new disk configuration</h3><p>Update the config/filesystems.php file to add a new r2 disk.</p><pre>&lt;?php<br><br>return [<br><br>    // ...<br><br>    &#39;disks&#39; =&gt; [<br><br>        // ...<br><br>        &#39;r2&#39; =&gt; [<br>             &#39;driver&#39; =&gt; &#39;s3&#39;,<br>             &#39;key&#39; =&gt; env(&#39;CLOUDFLARE_R2_ACCESS_KEY_ID&#39;),<br>             &#39;secret&#39; =&gt; env(&#39;CLOUDFLARE_R2_SECRET_ACCESS_KEY&#39;),<br>             &#39;region&#39; =&gt; &#39;us-east-1&#39;,<br>             &#39;bucket&#39; =&gt; env(&#39;CLOUDFLARE_R2_BUCKET&#39;),<br>             &#39;url&#39; =&gt; env(&#39;CLOUDFLARE_R2_URL&#39;),<br>             &#39;visibility&#39; =&gt; &#39;private&#39;,<br>             &#39;endpoint&#39; =&gt; env(&#39;CLOUDFLARE_R2_ENDPOINT&#39;),<br>             &#39;use_path_style_endpoint&#39; =&gt; env(&#39;CLOUDFLARE_R2_USE_PATH_STYLE_ENDPOINT&#39;, false),<br>             &#39;throw&#39; =&gt; false,<br>         ],<br>    ],<br>];</pre><p>Don’t mind the region; just put us-east-1 even though there are no R2 regions with Cloudflare.</p><p>Add the environment variables to your .env.example.</p><pre>CLOUDFLARE_R2_ACCESS_KEY_ID=<br>CLOUDFLARE_R2_SECRET_ACCESS_KEY=<br>CLOUDFLARE_R2_BUCKET=<br>CLOUDFLARE_R2_ENDPOINT=<br>CLOUDFLARE_R2_URL=</pre><h3>3. Create a new R2 bucket</h3><p>Go to your Cloudflare dashboard, choose “<em>R2”</em> from the sidebar and click on the “<em>Create bucket” </em>button.</p><p>We’re going to name it laradoc-storage for this example.</p><figure><img alt="Create a Cloudflare R2 bucket" src="https://cdn-images-1.medium.com/max/1024/1*P2Ea5uwXwoOgjn0ra8l1yA.png" /><figcaption>Create an R2 bucket</figcaption></figure><h3>4. Generate an API token</h3><p>From the R2 page, click on <em>“Manage R2 API Tokens”</em>.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*n31vWw9jnaj_zJFfNt-aqQ.png" /></figure><p>And then, create a new token with the “Edit” permission:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*BES8ofzIVP5zxUljfpnuCA.png" /><figcaption>Create a Cloudflare R2 API token</figcaption></figure><p>Once created, you get an <strong>Access Key ID </strong>and a <strong>Secret Access Key</strong>. Save them somewhere! 🔑</p><h3>5. Update your environment file</h3><p>Get your CLOUDFLARE_R2_ENDPOINT on your bucket page.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*vUD-14qHUmkbF9hqOAyiMw.png" /><figcaption>Where to find the CLOUDFLARE_R2_ENDPOINT</figcaption></figure><p>Then, fill your .env file like so:</p><pre>CLOUDFLARE_R2_ACCESS_KEY_ID=YOUR_ACCESS_KEY<br>CLOUDFLARE_R2_SECRET_ACCESS_KEY=YOUR_SECRET_KEY<br>CLOUDFLARE_R2_BUCKET=YOUR_BUCKET_NAME<br>CLOUDFLARE_R2_ENDPOINT=XXXXXXX.r2.cloudflarestorage.com<br>CLOUDFLARE_R2_URL=</pre><p>That’s it! You can now use your R2 disk like any other disk.</p><p>Let’s see a few examples from the <a href="https://laravel.com/docs/9.x/filesystem">Laravel documentation</a>, but applied to our new R2 disk.</p><pre>use Illuminate\Support\Facades\Storage;<br><br>// Retrieving Files<br>Storage::disk(&#39;r2&#39;)-&gt;get(&#39;file.jpg&#39;);<br><br>// Storing Files<br>Storage::disk(&#39;r2&#39;)-&gt;put(&#39;example.txt&#39;, &#39;Hello World&#39;);<br><br>// Check if a file exists<br>Storage::disk(&#39;r2&#39;)-&gt;exists(&#39;file.jpg&#39;)<br><br>// Moving Files<br>Storage::disk(&#39;r2&#39;)-&gt;copy(&#39;old/file.jpg&#39;, &#39;new/file.jpg&#39;);<br><br>// Copying Files<br>Storage::disk(&#39;r2&#39;)-&gt;move(&#39;old/file.jpg&#39;, &#39;new/file.jpg&#39;);</pre><pre>&lt;?php<br> <br>namespace App\Http\Controllers;<br> <br>use App\Http\Controllers\Controller;<br>use Illuminate\Http\Request;<br> <br>class UserAvatarController extends Controller<br>{<br>    /**<br>     * Update the avatar for the user.<br>     */<br>    public function update(Request $request)<br>    {<br>        $path = $request-&gt;file(&#39;avatar&#39;)-&gt;store(&#39;avatars&#39;, &#39;r2&#39;);<br><br>        $request-&gt;user()-&gt;update([<br>            &#39;avatar_path&#39; =&gt; $path,<br>        ]);<br> <br>        return back();<br>    }<br>}</pre><p>You may also set r2 as your default disk and you won’t even need to specify disk(&#39;r2&#39;) in all these examples.</p><p>If you want that, update the FILESYSTEM_DISK variable in your .env file.</p><pre>FILESYSTEM_DISK=r2</pre><p>👋 I offer <strong>tech consulting services</strong> for companies that need help with their Laravel applications. I can assist with upgrades, refactoring, testing, new features, or building new apps. Feel free to contact me through <a href="https://www.linkedin.com/in/antoine-lam%C3%A9/">LinkedIn</a>, or you can find my email on my <a href="https://github.com/antoinelame">GitHub</a> profile.</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=553a5403c6f8" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Using Soketi with Laravel ]]></title>
            <link>https://medium.com/@antoine.lame/using-soketi-with-laravel-902ee1e6b7b?source=rss-bb104292599f------2</link>
            <guid isPermaLink="false">https://medium.com/p/902ee1e6b7b</guid>
            <category><![CDATA[soketi]]></category>
            <category><![CDATA[websocket]]></category>
            <category><![CDATA[laravel]]></category>
            <category><![CDATA[php]]></category>
            <category><![CDATA[web-development]]></category>
            <dc:creator><![CDATA[Antoine Lamé]]></dc:creator>
            <pubDate>Thu, 24 Nov 2022 12:26:06 GMT</pubDate>
            <atom:updated>2023-06-26T14:13:37.434Z</atom:updated>
            <content:encoded><![CDATA[<p>You may have already used Pusher on Laravel to broadcast WebSocket messages to your front-end application. Using Pusher is very convenient; it’s cloud-based, and you don’t have to install anything, but it can become pricey since you pay for your usage.</p><p>Let’s see how to use <strong>Soketi</strong> instead of Pusher. Soketi is a simple, fast, and open-source WebSockets server.</p><p>Unlike Pusher, we have to install and deploy it ourselves, but we’ll pay only for our infrastructure (I use a 5$ DigitalOcean Droplet <a href="https://uselaradoc.com/">for Laradoc</a>). Soketi is fast and has a negligible memory footprint per connection; you won’t need a big server.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/0*B6pwwovm1SjgZymU" /><figcaption>Photo by <a href="https://unsplash.com/@gillesrollandmonnet?utm_source=medium&amp;utm_medium=referral">Gilles Rolland-Monnet</a> on <a href="https://unsplash.com/?utm_source=medium&amp;utm_medium=referral">Unsplash</a></figcaption></figure><h3>Installation</h3><p>In this article, we are going to use Soketi locally. In the next part, we’ll see how to install it on a production server.</p><h4><strong>Option 1: Global installation with NPM</strong></h4><pre>npm install -g @soketi/soketi</pre><p>After the installation, you can start the server using the start command:</p><pre>soketi start</pre><h4><strong>Option 2: Docker</strong></h4><pre>docker run -p 6001:6001 -p 9601:9601 quay.io/soketi/soketi:1.4–16-debian</pre><h4><strong>Option 3: Laravel Sail (Docker)</strong></h4><p>Add the following service to your docker-compose.yml file:</p><pre>services:<br>    // ...<br><br>    soketi:<br>        image: &#39;quay.io/soketi/soketi:latest-16-alpine&#39;<br>        environment:<br>            SOKETI_DEBUG: &#39;1&#39;<br>            SOKETI_METRICS_SERVER_PORT: &#39;9601&#39;<br>        ports:<br>            - &#39;${SOKETI_PORT:-6001}:6001&#39;<br>            - &#39;${SOKETI_METRICS_SERVER_PORT:-9601}:9601&#39;<br>        networks:<br>            - sail</pre><p>Rebuild your container images:</p><pre>sail build --no-cache<br>sail up</pre><h3>Soketi default application</h3><p>Like Pusher, Soketi uses <em>“Apps”</em> for server authentication. Each “App” has:</p><ul><li>An <strong>App ID</strong> <em>(example: </em><em>laradoc)</em></li><li>An <strong>App Key</strong> <em>(this is a public key used by your front-end code)</em></li><li>An <strong>App Secret</strong> <em>(this is a secret key only known by your Laravel back-end)</em></li></ul><p>Soketi creates for you a default application with the following parameters:</p><ul><li>App ID: app-id</li><li>App Key: app-key</li><li>App Secret: app-secret</li></ul><p>Since we’re in a local environment, we will use this default app. But of course, for production (in the next part), we’ll create another app with unguessable credentials.</p><h3>Broadcasting configuration file</h3><p>Go to your config/broadcasting.php file and make sure you have a connection called pusher that looks like this:</p><pre>// ...<br><br>&#39;connections&#39; =&gt; [<br>    // ...<br><br>    &#39;pusher&#39; =&gt; [<br>        &#39;driver&#39; =&gt; &#39;pusher&#39;,<br>        &#39;key&#39; =&gt; env(&#39;PUSHER_APP_KEY&#39;),<br>        &#39;secret&#39; =&gt; env(&#39;PUSHER_APP_SECRET&#39;),<br>        &#39;app_id&#39; =&gt; env(&#39;PUSHER_APP_ID&#39;),<br>        &#39;options&#39; =&gt; [<br>            &#39;host&#39; =&gt; env(&#39;PUSHER_HOST&#39;),<br>            &#39;port&#39; =&gt; env(&#39;PUSHER_PORT&#39;),<br>            &#39;scheme&#39; =&gt; env(&#39;PUSHER_SCHEME&#39;, &#39;http&#39;),<br>            &#39;encrypted&#39; =&gt; true,<br>            &#39;useTLS&#39; =&gt; env(&#39;PUSHER_SCHEME&#39;) === &#39;https&#39;,<br>        ],<br>    ],<br>]<br><br>// ...</pre><p>You may be surprised that the connection is called pusher. To be clear, we will not use Pusher, but Soketi implements the Pusher Protocol. That’s why we keep calling it pusher, but you can rename it to soketi if you want.</p><h3>Environment file</h3><p>Let’s change our environment file (.env) to use Soketi and its default application.</p><p><strong>If you’re running your Laravel application locally:</strong></p><pre>BROADCAST_DRIVER=pusher<br><br>PUSHER_HOST=localhost<br>PUSHER_PORT=6001<br>PUSHER_SCHEME=http<br>PUSHER_APP_ID=app-id<br>PUSHER_APP_KEY=app-key<br>PUSHER_APP_SECRET=app-secret<br><br>VITE_PUSHER_APP_KEY=&quot;${PUSHER_APP_KEY}&quot;<br>VITE_PUSHER_HOST=&quot;${PUSHER_HOST}&quot;<br>VITE_PUSHER_PORT=&quot;${PUSHER_PORT}&quot;</pre><p><strong>If you’re running your Laravel application with Sail:</strong></p><pre>BROADCAST_DRIVER=pusher<br><br>PUSHER_HOST=soketi<br>PUSHER_PORT=6001<br>PUSHER_SCHEME=http<br>PUSHER_APP_ID=app-id<br>PUSHER_APP_KEY=app-key<br>PUSHER_APP_SECRET=app-secret<br><br>VITE_PUSHER_APP_KEY=&quot;${PUSHER_APP_KEY}&quot;<br>VITE_PUSHER_HOST=&quot;127.0.0.1&quot;<br>VITE_PUSHER_PORT=&quot;${PUSHER_PORT}&quot;</pre><h3>Laravel Echo initialization</h3><p>Go to your JavaScript file where you initialize Laravel Echo, probably bootstrap.js. It must be initialized like this:</p><pre>import Echo from &#39;laravel-echo&#39;;<br>import Pusher from &#39;pusher-js&#39;;<br><br>window.Pusher = Pusher;<br><br>window.Echo = new Echo({<br>  broadcaster: &#39;pusher&#39;,<br>  key: process.env.VITE_PUSHER_APP_KEY,<br>  wsHost: process.env.VITE_PUSHER_HOST,<br>  wsPort: process.env.VITE_PUSHER_PORT,<br>  wssPort: process.env.VITE_PUSHER_PORT,<br>  forceTLS: false,<br>  encrypted: true,<br>  disableStats: true,<br>  enabledTransports: [&#39;ws&#39;, &#39;wss&#39;],<br>});</pre><p>Don’t forget to rebuild your assets since we changed environment variables used in our JavaScript.</p><pre>npm run dev</pre><h4>That’s it! ✅</h4><p>Everything is ready. We have:</p><ul><li>a running WebSockets server (Soketi) on port 6001</li><li>a Laravel back-end knowing how to reach Soketi (broadcasting.php)</li><li>a JavaScript front-end knowing how to reach Soketi (bootstrap.js)</li></ul><p>👋 I offer <strong>tech consulting services</strong> for companies that need help with their Laravel applications. I can assist with upgrades, refactoring, testing, new features, or building new apps. Feel free to contact me through <a href="https://www.linkedin.com/in/antoine-lam%C3%A9/">LinkedIn</a>, or you can find my email on my <a href="https://github.com/antoinelame">GitHub profile</a>.</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=902ee1e6b7b" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Which Laravel authentication package to choose?]]></title>
            <link>https://medium.com/@antoine.lame/which-laravel-authentication-package-to-choose-290551a82a44?source=rss-bb104292599f------2</link>
            <guid isPermaLink="false">https://medium.com/p/290551a82a44</guid>
            <category><![CDATA[laravel-framework]]></category>
            <category><![CDATA[php]]></category>
            <category><![CDATA[laravel]]></category>
            <category><![CDATA[authentication]]></category>
            <category><![CDATA[oauth]]></category>
            <dc:creator><![CDATA[Antoine Lamé]]></dc:creator>
            <pubDate>Wed, 16 Nov 2022 23:03:44 GMT</pubDate>
            <atom:updated>2023-06-26T14:15:14.338Z</atom:updated>
            <content:encoded><![CDATA[<p>At its core, Laravel provides “guards” and “providers” to manage authentication. However, controllers, routes, and views still need to be implemented.</p><p>To achieve this, Laravel offers first-party packages that offer a complete authentication system, ready to use in just a few minutes. This is unique and the strength of Laravel: in no time, you can start developing your ideas.</p><p>However, multiple packages are related to authentication, and it can be challenging to know which one is adequate for your project.</p><p>Should you use <strong>Laravel Jetstream</strong> or <strong>Laravel Breeze</strong>? What are the differences between <strong>Laravel Passport</strong> and <strong>Laravel Sanctum</strong>? Is <strong>Laravel UI</strong> still an option?</p><p>To help you find your way around, I have made a diagram to help you understand their differences and make the best choice for your project.</p><figure><img alt="Diagram explaining which Laravel authentication to choose for different projects" src="https://cdn-images-1.medium.com/max/1024/1*_WpqfuGXuFUAG96zKre1Ig.png" /><figcaption>Which Laravel authentication package to install for your project?</figcaption></figure><h4>Laravel Breeze</h4><p><a href="https://laravel.com/docs/9.x/starter-kits">Breeze</a> is a minimal and simple implementation of Laravel’s authentication features. It works both for full-stack applications and APIs. This is an excellent choice if you like simplicity or are brand new to Laravel.</p><ul><li>You can choose between <strong>Blade</strong>, <strong>Vue/React (with Inertia),</strong> or <strong>API</strong></li><li>It uses Tailwind CSS when Blade or Inertia stacks are chosen</li><li>✅ Registration</li><li>✅ Login</li><li>✅ Profile Management</li><li>✅ Password Reset</li><li>✅ Email Verification</li></ul><h4>Laravel Jetstream</h4><p>More complete and stylish, <a href="https://jetstream.laravel.com/2.x/introduction.html">Jetstream</a> is an interesting alternative to Breeze. It provides more features but requires that your project uses Livewire or Inertia.</p><ul><li>You can choose between <strong>Blade + Livewire</strong> or <strong>Vue/React + Inertia</strong></li><li>It uses Tailwind CSS</li><li>✅ Registration</li><li>✅ Login</li><li>✅ Profile Management</li><li>✅ Password Reset</li><li>✅ Email Verification</li><li>✅ Two-Factor Authentication (2FA)</li><li>✅ Teams Management</li><li>✅ Browser Sessions Management (let users see where they’re logged-in)</li><li>✅ API Tokens &amp; Permissions (let users generate API tokens)</li></ul><h4>Laravel Fortify</h4><p><a href="https://laravel.com/docs/9.x/fortify">Fortify</a> is a front-end agnostic implementation of all authentication features. It provides all routes and controllers needed to implement your authentication logic but requires you to code the user interface yourself. There are no views out of the box!</p><p>This is a great choice if you don’t use Tailwind CSS, want to code your front end yourself, or are building an API.</p><ul><li>❌ No views, no user interface</li><li>✅ Registration</li><li>✅ Login</li><li>✅ Profile Management</li><li>✅ Password Reset</li><li>✅ Email Verification</li><li>✅ Two-Factor Authentication (2FA)</li><li>✅ Teams Management</li><li>✅ Browser Sessions Management (let users see where they’re connected and logout sessions)</li><li>✅ API Tokens &amp; Permissions (let users generate API tokens)</li></ul><p>For info, Jetstream uses Fortify under the hood and adds the UI layer.</p><h4>Laravel UI</h4><p><a href="https://github.com/laravel/ui">Laravel UI</a> is the legacy scaffolding and brings a basic authentication system built on the Bootstrap CSS framework. <strong>Today, the only reason to install it is that your project uses Bootstrap CSS.</strong></p><ul><li>You can choose between simple HTML, Vue, or React</li><li>✅ Registration</li><li>✅ Login</li><li>✅ Password Reset</li><li>✅ Email Verification</li></ul><h4>Laravel Passport</h4><p><a href="https://laravel.com/docs/9.x/passport">Passport</a> provides a full OAuth2 server implementation.</p><h4>Laravel Sanctum</h4><p><a href="https://laravel.com/docs/9.x/sanctum">Sanctum</a> offers a simple way to authenticate SPAs or mobile applications that need to communicate with your Laravel-powered API. If you don’t need full OAuth2 support, this is a much simpler alternative to Passport.</p><ul><li>✅ Middleware to authenticate your SPA (using cookie-based sessions)</li><li>✅ Middleware to authenticate clients using API tokens</li><li>✅ API tokens generation &amp; permissions</li><li>❌ No authentication routes or controllers</li></ul><p>Using Sanctum, you still have to implement your own authentication logic (creating your routes and controllers) or use it in combination with Fortify.</p><p>If you use it to authenticate a SPA, it needs to be hosted on the same root domain as your API since it uses cookie-based sessions.</p><p>I hope that all the authentication packages and starter kits of Laravel have no more secrets for you and that this article has made your choice easier if you were hesitating between them.</p><p>👋 I offer <strong>tech consulting services</strong> for companies that need help with their Laravel applications. I can assist with upgrades, refactoring, testing, new features, or building new apps. Feel free to contact me through <a href="https://www.linkedin.com/in/antoine-lam%C3%A9/">LinkedIn</a>, or you can find my email on my <a href="https://github.com/antoinelame">GitHub profile</a>.</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=290551a82a44" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[What’s new in Laravel? November edition.]]></title>
            <link>https://medium.com/@antoine.lame/whats-new-in-laravel-november-edition-a114be864adf?source=rss-bb104292599f------2</link>
            <guid isPermaLink="false">https://medium.com/p/a114be864adf</guid>
            <category><![CDATA[web-development]]></category>
            <category><![CDATA[framework]]></category>
            <category><![CDATA[php]]></category>
            <category><![CDATA[laravel]]></category>
            <category><![CDATA[upgrade]]></category>
            <dc:creator><![CDATA[Antoine Lamé]]></dc:creator>
            <pubDate>Wed, 01 Dec 2021 19:00:24 GMT</pubDate>
            <atom:updated>2023-06-26T14:16:37.694Z</atom:updated>
            <content:encoded><![CDATA[<figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*1dtVqKoqDLngR41Amskv2Q.png" /></figure><p>Laravel has a vibrant community that never stops proposing and adding new features to the framework. In November alone, 204 pull requests were submitted and 108 were merged into the framework.</p><p>To help you stay up to date, I’ve compiled all the major new features released in November that you can already use in Laravel 8.</p><h3>Helpers</h3><h4>hasDebugModeEnabled()</h4><p>You can now use the hasDebugModeEnabled() method to check if the debug mode is enabled. No need to write config(&#39;app.debug&#39;) anymore.</p><p>Thank you <a href="https://github.com/laravel/framework/pull/39755">Joel Mellon</a>!</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*hI024J1ELRn2LaKjWTF4jQ.png" /></figure><h4>Str::reverse()</h4><p>It becomes a breeze to reverse a string thanks to the new Str::reverse() method.</p><p>Thank you <a href="https://github.com/laravel/framework/pull/39816">netpoq</a>!</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*daJs2KUDZFKtwASxdH7qkw.png" /></figure><h4>Str::mask()</h4><p>You need to obfuscate sensible parts of texts like emails or addresses? Str::mask() is the new method you need.</p><p>Thank you <a href="https://github.com/laravel/framework/pull/39393">DarkGhostHunter</a>!</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*8v1WRp3VF9-Znv6VKb5wFA.png" /></figure><h4>Arr::undot()</h4><p>This one has been proposed several times by the community and was finally accepted a few days ago. The new Arr::undot() method basically does the opposite of Arr::dot(), it takes an associative array and converts it to a multi-dimensional one based on keys with dot notation.</p><p>Thank you <a href="https://github.com/laravel/framework/pull/39729">Amir Rami</a>!</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*qLHvmFrUV_gQh0O10cjzRw.png" /></figure><h3>Testing</h3><h4>Event::fakeExcept()</h4><p>When testing code that dispatches events, I sometimes instruct Laravel to not actually execute the event’s listeners by using the Event::fake() method.</p><p>However, I often found myself in scenarios where I had to fake all the events except one. There is now a method for that, it’s Event::fakeExcept() and it does exactly that. I love it!</p><p>Thank you <a href="https://github.com/laravel/framework/pull/39752">Jeffrey Angenent</a>.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*BqWdG1ghVs4Kp_77kA1Z8Q.png" /></figure><h4>forgetMock()</h4><p>You could have multiple tests using a mocked instance of an object which is prepared in the setUp() method. In that class, maybe some tests need to run without this mock. From now on, all you have to do is to use the brand new $this-&gt;forgetMock() method.</p><p>Thank you <a href="https://github.com/laravel/framework/pull/39713">Khalil Laleh</a>!</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*zaM3xwcjDwSqEh7jLco5zQ.png" /></figure><h4>assertSuccessful() and assertFailed()</h4><p>When testing artisan commands, two new assertions are now available, assertSuccessful() and assertFailed().</p><p>Thank you <a href="https://github.com/laravel/framework/pull/39435">Roberto Aguilar</a>!</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*Um6PyYWaP8YYNYJAuzVhlw.png" /></figure><h3>Validation</h3><h4>Enum rule</h4><p>PHP 8.1 was released a few days ago and adds support for Enums. It is now possible in Laravel to validate whether the field under validation contains a valid enum value.</p><p>Thank you <a href="https://github.com/laravel/framework/pull/39437">Mohamed</a>!</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*gam5oDLLiMuTaxa5TOI_5A.png" /></figure><h4>declined and declined_id rules</h4><p>Two new validation rules have been introduced this month. These are basically the opposites of accepted and accepted_if rules:</p><ul><li>declined <br>The field under validation must be “no”, “off”, 0, or false</li><li>declined_if:anotherfield,value,... <br>The field under validation must be “no”, “off”, 0, or false if another field under validation is equal to a specified value.</li></ul><p>Thank you <a href="https://github.com/laravel/framework/pull/39579">Nicklas Kevin Frank</a>.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*NTjXDTcMdARzqSM-9OOsVg.png" /></figure><h3>Blade</h3><h4>Js::from() helper</h4><p>Sometimes you may pass an array to your view with the intention of rendering it as JSON in order to initialize a JavaScript variable.</p><p>You can now use the new Js::from() method for this. It will return a string JSON.parse JavaScript statement that will convert the given object or array into a valid JavaScript object.</p><p>Thank you <a href="https://github.com/laravel/framework/pull/39389">TinaH</a>!</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*9OcMExRAMGh8k3facqX_AQ.png" /></figure><h3>Migrations</h3><h4>Fulltext index</h4><p>A fulltext index can be added to one or more columns in a MySQL database. It allows you to search for keywords very quickly in these columns, using full-text searching, which is a technique that lets you search for records that may not perfectly match the search criteria.</p><p>Laravel migrations now support the creation of these FULLTEXT indexes for MySQL databases. PostgreSQL could be supported as well in the future.</p><p>Thank you <a href="https://github.com/laravel/framework/pull/39821">Taylor</a>!</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*cBf6kGgveGF3Ffj6Ot_x_g.png" /></figure><h3>Route definitions</h3><h4>can() method</h4><p>If you perform authorization checks in a routes file, you may be interested to know that it is now possible to chain the can() method instead of using middleware(&#39;can:...&#39;).</p><p>Thank you <a href="https://github.com/laravel/framework/pull/39464">Taylor</a>.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*rc_5q2XmBIFJ0rktFCJzCg.png" /></figure><h3>Commands</h3><h4>request flag</h4><p>Laravel includes a new --request flag when making models or controllers via Artisan. This option will create a Store{Model}Request and Update{Model}Request form request and update the controller to use those requests on their respective methods.</p><p>Thank you <a href="https://github.com/laravel/framework/pull/39120">Povilas Korop</a>!</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*I-0UAcGrH5Qo_ayhVZnmMw.png" /></figure><p>I hope you enjoyed this post. Let me know in the comments if I missed a new feature, so I can add it to this article.</p><p>👋 I offer <strong>tech consulting services</strong> for companies that need help with their Laravel applications. I can assist with upgrades, refactoring, testing, new features, or building new apps. Feel free to contact me through <a href="https://www.linkedin.com/in/antoine-lam%C3%A9/">LinkedIn</a>, or you can find my email on my <a href="https://github.com/antoinelame">GitHub profile</a>.</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=a114be864adf" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[How to implement Rate Limiting in Laravel]]></title>
            <link>https://medium.com/@antoine.lame/using-the-rate-limiter-in-laravel-dab58a5040bc?source=rss-bb104292599f------2</link>
            <guid isPermaLink="false">https://medium.com/p/dab58a5040bc</guid>
            <category><![CDATA[php]]></category>
            <category><![CDATA[laravel-framework]]></category>
            <category><![CDATA[rate-limiting]]></category>
            <category><![CDATA[laravel]]></category>
            <category><![CDATA[security]]></category>
            <dc:creator><![CDATA[Antoine Lamé]]></dc:creator>
            <pubDate>Mon, 29 Nov 2021 16:16:59 GMT</pubDate>
            <atom:updated>2023-06-26T14:17:53.477Z</atom:updated>
            <content:encoded><![CDATA[<figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*2phGsWwxv6wBYhA3DMSopw.png" /></figure><p>Limiting the number of requests per minute sent to an application is often necessary to protect against attacks trying to saturate your server or to brute force authentication forms. That’s why Laravel comes with a <strong>rate limiting</strong> mechanism, which we will learn to use here.</p><p>There are two ways to implement rate limiting with Laravel:</p><ol><li>Using the <strong>Rate Limiter Middleware</strong>: to rate limiting incoming HTTP requests before reaching the controller</li><li>Using the <strong>Rate Limiting abstraction</strong>: to interact more finely with the rate limiter at the controller level</li></ol><p>In this article, we will see only the first implementation method, which is the use of the throttle middleware to setup a simple rate limit system.</p><h3>Get started</h3><p>The throttle middleware is made to protect <strong><em>routes</em></strong> against excessive reception of too many HTTP requests and restricting them once a limit has been reached.</p><p>To get started, you should start by defining the rate limiter configurations that your application needs. For that, go to the App\Providers\RouteServiceProvider class in the definition of the configureRateLimiting() method.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/f54bb7cd497c6cfa5fd20b2d95c3b2ee/href">https://medium.com/media/f54bb7cd497c6cfa5fd20b2d95c3b2ee/href</a></iframe><p>In the above example, we have defined a rate limiter called global which limits access to the routes associated with it to 1000 requests per minute.</p><p>You can create as many configurations as you want, with the names you want. We could for example imagine having global, downloads, chat to customise the limit depending on the routes.</p><p>Once you defined your rate limiters, you can apply them to routes you want to rate limite using the throttle middleware, as follows:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/93c307d6f1ecb1167ded63dd3e3eac52/href">https://medium.com/media/93c307d6f1ecb1167ded63dd3e3eac52/href</a></iframe><p>As you can see, you just need to pass a string containing throttle: followed by the name of the rate limiter you have defined.</p><p>For these routes, if the rate limit is reached, the application will return a <em>“429 — Too Many Requests”</em> response instead of serving the expected response.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*bMafOeqKSzBoablHEsYBEQ.png" /><figcaption>Default “429 Too Many Requests” response in Laravel</figcaption></figure><p><strong>⚠️ Be aware!</strong> If your application has an API using the api middleware group, by default the throttles:api middleware is applied on these routes as defined in the $middlewareGroups[&#39;api&#39;] property of your App\Http\Kernel class. So you have to define a rate limiter called api or to remove it from the group.</p><h3>Segmenting Rate Limits</h3><p>Until now, as defined in our previous example, our rate limiters didn’t differentiate between users. They blocked the total number of requests received per minute by the application, from all users.</p><p>This is not very efficient, because it can block users who weren’t trying to flood the application. So it’s better to set <strong>a limit of requests per user</strong>. To accomplish it, you may use the by method when building your rate limiter:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/d8edf8441da72d2646ae8fde846ced3f/href">https://medium.com/media/d8edf8441da72d2646ae8fde846ced3f/href</a></iframe><p>Therefore, each user will only be able to access the associated routes 10 times per minute before being restricted.</p><p>What if we want to apply the rate limit to routes that don’t necessarily require authentication? Indeed, $request-&gt;user() could be null so it’s a good idea to use the IP address as well.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/5f1d96ba629f18c04358c2c8bef8a979/href">https://medium.com/media/5f1d96ba629f18c04358c2c8bef8a979/href</a></iframe><p>Authenticated users can now send <strong>100 requests per minute</strong> while unauthenticated users can only send <strong>20 requests per minute</strong> (their rate is associated with their IP address).</p><p>It looks pretty good! We now have an efficient configured rate limiter. Using the throttle middleware with the RateLimiter facade is awesome and enjoyable to use.</p><p>However, the RateLimiter is not only meant to be used with the throttle middleware, you can also use it in a more advanced way in your controllers for example. I invite you <a href="https://laravel.com/docs/8.x/rate-limiting">to consult the documentation</a> to learn more about this.</p><p>I hope you enjoyed this post. Feel free to give it a clap! 👏</p><p>👋 I offer <strong>tech consulting services</strong> for companies that need help with their Laravel applications. I can assist with upgrades, refactoring, testing, new features, or building new apps. Feel free to contact me through <a href="https://www.linkedin.com/in/antoine-lam%C3%A9/">LinkedIn</a>, or you can find my email on my <a href="https://github.com/antoinelame">GitHub profile</a>.</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=dab58a5040bc" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Laravel Prunable trait: Periodically remove obsolete models]]></title>
            <link>https://medium.com/@antoine.lame/laravel-prunable-trait-periodically-remove-obsolete-models-b0662019ced2?source=rss-bb104292599f------2</link>
            <guid isPermaLink="false">https://medium.com/p/b0662019ced2</guid>
            <category><![CDATA[php]]></category>
            <category><![CDATA[eloquent]]></category>
            <category><![CDATA[laravel]]></category>
            <dc:creator><![CDATA[Antoine Lamé]]></dc:creator>
            <pubDate>Tue, 16 Nov 2021 15:01:08 GMT</pubDate>
            <atom:updated>2023-06-26T14:19:05.841Z</atom:updated>
            <content:encoded><![CDATA[<figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*Uvr6mcpjrt1xp6prpHIcsQ.png" /></figure><p>Since <a href="https://github.com/laravel/framework/pull/37889">this Nuno Maduro’s PR</a> and the release of Laravel 8.5, there is a new Prunable trait to automatically remove obsolete model records from the database.</p><p>You need to use the Illuminate\Database\Eloquent\Prunable trait in the models you would like to periodically prune. Next, you need to implement the prunable() method which returns a query builder targeting records that must be deleted.</p><p>For instance, we have here an Alert model that is no longer needed in our database after one month.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/7171348ae50eb4db8aa5d15afa3f292f/href">https://medium.com/media/7171348ae50eb4db8aa5d15afa3f292f/href</a></iframe><p>Each time you run php artisan model:prune it will delete Alert records older than one month. You should schedule this command in your application’s App\Console\Kernel class so that you don’t have to run it manually.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/37077aeaac04249e36bb358a4982e2da/href">https://medium.com/media/37077aeaac04249e36bb358a4982e2da/href</a></iframe><h4>The pruning() method</h4><p>You can also define a pruning() method in your model that will be called <strong><em>before</em></strong> deleting a model record. This method could be useful for deleting any additional resources associated with the model.</p><pre>/**<br> * Prepare the model for pruning.<br> */<br>protected function pruning(): void<br>{<br>    //<br>}</pre><h4>Mass Pruning</h4><p>You can also use Illuminate\Database\Eloquent\MassPrunable trait. Instead of deleting obsolete models one by one, it will perform a mass-deletion query. This means that instead of executing one SQL query per record, it will execute only one.</p><p>But beware! Although the pruning process will be much faster, the pruning method will not be invoked, nor will the deleting and deleted model events be dispatched.</p><p>I hope you enjoyed this post. Feel free to give it a clap! 👏</p><p>👋 I offer <strong>tech consulting services</strong> for companies that need help with their Laravel applications. I can assist with upgrades, refactoring, testing, new features, or building new apps. Feel free to contact me through <a href="https://www.linkedin.com/in/antoine-lam%C3%A9/">LinkedIn</a>, or you can find my email on my <a href="https://github.com/antoinelame">GitHub profile</a>.</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=b0662019ced2" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[From PHPUnit to Pest]]></title>
            <link>https://medium.com/@antoine.lame/from-phpunit-to-pest-b825b1d881f3?source=rss-bb104292599f------2</link>
            <guid isPermaLink="false">https://medium.com/p/b825b1d881f3</guid>
            <category><![CDATA[pest]]></category>
            <category><![CDATA[phpunit]]></category>
            <category><![CDATA[laravel]]></category>
            <category><![CDATA[php]]></category>
            <category><![CDATA[testing]]></category>
            <dc:creator><![CDATA[Antoine Lamé]]></dc:creator>
            <pubDate>Mon, 15 Nov 2021 17:11:18 GMT</pubDate>
            <atom:updated>2023-06-26T14:19:52.078Z</atom:updated>
            <content:encoded><![CDATA[<figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*E-YywrZTenQsDAAKa_th8w.png" /></figure><p>Pest is a great testing framework built on top of PHPUnit. It has an elegant syntax simplifying your tests as much as possible.</p><p>Starting to work with Pest is very easy and we will see how to write more readable tests with it if you are used to PHPUnit.</p><h3>Installation</h3><p>First, install Pest via the <a href="https://getcomposer.org/">Composer</a> package manager:</p><pre>composer require pestphp/pest --dev --with-all-dependencies</pre><p>On Laravel, install the pest-plugin-laravel and run the pest:install Artisan command:</p><pre>composer require pestphp/pest-plugin-laravel --dev<br>php artisan pest:install</pre><h3>Running your tests</h3><p>You are probably used to execute vendor/bin/phpunit to run your tests. With Pest, you have to run vendor/bin/pest.</p><p>There is no difference in usage, you can continue to use the same options as with PHPUnit (--filter for example).</p><p>You can have PHPUnit and Pest tests in the same project. The vendor/bin/pest command will run your PHPUnit tests as well. But vendor/bin/phpunit will not run your Pest tests.</p><h3>Files structure</h3><p>No change here. Pest uses the same file structure as PHPUnit.</p><pre>tests<br>    - Unit/ComponentTest.php<br>    - Feature/HomeTest.php<br>phpunit.xml</pre><h3>Skeleton</h3><p>With Pest, there is no class. You just have to use the helper test() and a closure.</p><p>With PHPUnit, we do:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/fbfcc86477716d8bead84482cebbcc5d/href">https://medium.com/media/fbfcc86477716d8bead84482cebbcc5d/href</a></iframe><p>With Pest, we do:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/6120e3fc1dca7ac1620237d0cfb22463/href">https://medium.com/media/6120e3fc1dca7ac1620237d0cfb22463/href</a></iframe><p>For convenience, you can also use the it() helper which is an alias of test() so you can writeit(&#39;renders the homepage&#39;, ...) .</p><h3>Test Case</h3><p>The closure you provide to your test function is always bound to a specific test case class. By default, that class is extending PHPUnit\Framework\TestCase.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/39a19a81dff9d2e8d3f8a6adf266ff7a/href">https://medium.com/media/39a19a81dff9d2e8d3f8a6adf266ff7a/href</a></iframe><p>If you have to change it, you can call the uses() function:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/99ed7b7e861266abd3e6ed15cdac2115/href">https://medium.com/media/99ed7b7e861266abd3e6ed15cdac2115/href</a></iframe><p>It will affect all tests of your file.</p><p>You may also use a specific test case class for all tests contained in a specified folder. For that, you can call the in() function in your tests/Pest.php file.</p><pre>&lt;?php</pre><pre>uses(Tests\TestCase::<em>class</em>)-&gt;in(&#39;Feature&#39;);</pre><pre>// ...</pre><h3>Traits</h3><p>uses() is not only for test case classes. It’s the function you have to use to apply traits.</p><p>With PHPUnit, we do:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/63c0526fd2de13bda02b1b620a1036e2/href">https://medium.com/media/63c0526fd2de13bda02b1b620a1036e2/href</a></iframe><p>With Pest, we do:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/cce25457d79cbac16e3a5be8972c54a8/href">https://medium.com/media/cce25457d79cbac16e3a5be8972c54a8/href</a></iframe><h3>Setup And Teardown</h3><p>In your tests, you may want to run some code before and after each test or file.</p><p>With PHPUnit, we do:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/955f873b4b9697a3b94a73f60b3825a9/href">https://medium.com/media/955f873b4b9697a3b94a73f60b3825a9/href</a></iframe><p>With Pest, we do:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/4e7f851d4fb99b30d029697d3606d674/href">https://medium.com/media/4e7f851d4fb99b30d029697d3606d674/href</a></iframe><h3>Data Providers / Datasets</h3><p>Data Providers allows you to run the same test multiple times with different data. With Pest, it’s called <em>Datasets</em>.</p><p>With PHPUnit, we do:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/cadd62ffe57c8de08dcd0c3ec238cc6c/href">https://medium.com/media/cadd62ffe57c8de08dcd0c3ec238cc6c/href</a></iframe><p>With Pest, we do:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/093791767479d094890ccbf2617f7a4e/href">https://medium.com/media/093791767479d094890ccbf2617f7a4e/href</a></iframe><p>Pest offers more features than PHPUnit, like <a href="https://pestphp.com/docs/datasets#shared-datasets">Shared Datasets</a> or <a href="https://pestphp.com/docs/datasets#lazy-datasets">Lazy Datasets</a>.</p><h3>Expectations</h3><p>With PHPUnit, you have <strong>assertions</strong>. You can use them with Pest as well, without any change. But in addition to that, Pest offers <strong>expectations</strong>. Expectations allow you to write your tests like you would a natural sentence.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/cac9e69d0dd9b5541db40b20ea2ed8b5/href">https://medium.com/media/cac9e69d0dd9b5541db40b20ea2ed8b5/href</a></iframe><p>There are lot of <a href="https://pestphp.com/docs/expectations#available-expectations">available expectations</a> like for example:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/65645796c1d66e1694b846ac462e3f19/href">https://medium.com/media/65645796c1d66e1694b846ac462e3f19/href</a></iframe><p>As you can see, switching from PHPUnit to Pest is not difficult, and brings a lot of readability to your tests. Besides the simplicity, Pest offers new and useful features that I let you discover <a href="https://pestphp.com/docs/installation">on the documentation</a>.</p><p>I hope you enjoyed this post. Feel free to give it a clap! 👏</p><p>👋 I offer <strong>tech consulting services</strong> for companies that need help with their Laravel applications. I can assist with upgrades, refactoring, testing, new features, or building new apps. Feel free to contact me through <a href="https://www.linkedin.com/in/antoine-lam%C3%A9/">LinkedIn</a>, or you can find my email on my <a href="https://github.com/antoinelame">GitHub profile</a>.</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=b825b1d881f3" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Laravel : Asynchronous and concurrent HTTP requests]]></title>
            <link>https://medium.com/@antoine.lame/laravel-asynchronous-and-concurrent-http-requests-9c8e3e6d36bd?source=rss-bb104292599f------2</link>
            <guid isPermaLink="false">https://medium.com/p/9c8e3e6d36bd</guid>
            <category><![CDATA[asynchronous]]></category>
            <category><![CDATA[laravel]]></category>
            <category><![CDATA[concurrency]]></category>
            <category><![CDATA[http-request]]></category>
            <category><![CDATA[guzzle]]></category>
            <dc:creator><![CDATA[Antoine Lamé]]></dc:creator>
            <pubDate>Thu, 04 Nov 2021 21:43:40 GMT</pubDate>
            <atom:updated>2023-06-26T14:20:55.667Z</atom:updated>
            <content:encoded><![CDATA[<h3>Laravel: Asynchronous and concurrent HTTP requests</h3><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*8qrDD5Ukj4Slhrt4-R8jBw.png" /></figure><p>When reaching an API, sending outgoing HTTP requests can take time. If you are using the <strong>Laravel HTTP client</strong>, you can implement <strong>asynchronous requests</strong> to significantly improve performance.</p><h3>Asynchronous requests</h3><p>When you send a non-asynchronous HTTP request, your code waits for the response before moving on to the next statement.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/0ab754e147c6d170c6c6793e48d95a01/href">https://medium.com/media/0ab754e147c6d170c6c6793e48d95a01/href</a></iframe><p>This means that if the HTTP request is slow and takes 5 seconds, your code will idle for 5 seconds. It will just wait, without doing anything.</p><p><strong>What if you want to take advantage of this idle time to perform other actions? You can do this by using asynchronous requests.</strong></p><p>An asynchronous request is a request that is executed in the background, so you don’t have to wait for the response to continue your code. The Laravel HTTP Client has the async() method for that.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/ce9ef3115f72d9e663c238ac36771f7e/href">https://medium.com/media/ce9ef3115f72d9e663c238ac36771f7e/href</a></iframe><p>Take a look at the output:</p><pre>The request has been sent, but we didn&#39;t wait for the response.</pre><pre>Response received!</pre><pre>{&quot;I am the request response&quot;}</pre><p>We have sent an HTTP request <strong>in the background</strong> without blocking our code. The HTTP request is encapsulated in a promise, which informs us about the status of the request and will contain the response once it is received.</p><p>You can use $promise-&gt;getState() in order to know the state of your asynchronous request that is being executed in the background. It will return pending, rejected or fulfilled.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/dacfc2fb9a6f4245f6a6a9ad508004a7/href">https://medium.com/media/dacfc2fb9a6f4245f6a6a9ad508004a7/href</a></iframe><p>Be aware! If your code ends before the asynchronous request is finished, it will be interrupted. This is why you can, at the end of your code, use $promise-&gt;wait() to wait for the response to be received.</p><p>⚡️ You now know how to perform other tasks while requests are being sent instead of waiting for responses without doing anything.</p><h3>Concurrent requests</h3><p>A concrete use of asynchronous requests is to send several requests at the same time rather than one after the other. We call them <strong>concurrent requests</strong>.</p><p>Imagine that you need to retrieve a list of users via an API, but the results are paginated. It’s going to take a lot of time to get the users page by page if you send the HTTP requests sequentially like this:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/67f7c11d35e41cd867f1bda5cdce1d7f/href">https://medium.com/media/67f7c11d35e41cd867f1bda5cdce1d7f/href</a></iframe><p>Instead of sending requests sequentially, we could send our 30 requests at the same time. This will greatly improve performance, especially if the API is slow.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*aQht-Nx4Dpjt2cwNDxFenw.png" /></figure><p>From what we have just learned, we could do:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/04e4b53c9220563b1c67ad6734f2988a/href">https://medium.com/media/04e4b53c9220563b1c67ad6734f2988a/href</a></iframe><p>It works well and this is a good solution. But the Laravel HTTP Client comes with an handy method called pool() to make it even easier. Check this out:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/1aff71518bf60be6cc7453300a64a71d/href">https://medium.com/media/1aff71518bf60be6cc7453300a64a71d/href</a></iframe><p>$responses is an array and each response instance can be accessed based on the order it was added to the pool.</p><p>So our code becomes:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/ddcbd6fd489614c88b49660d4ce68fb6/href">https://medium.com/media/ddcbd6fd489614c88b49660d4ce68fb6/href</a></iframe><p>That’s it!</p><p>I hope you enjoyed this post. Feel free to clap this article (👏) and subscribe to my Medium newsletter for more.</p><p>👋 I offer <strong>tech consulting services</strong> for companies that need help with their Laravel applications. I can assist with upgrades, refactoring, testing, new features, or building new apps. Feel free to contact me through <a href="https://www.linkedin.com/in/antoine-lam%C3%A9/">LinkedIn</a>, or you can find my email on my <a href="https://github.com/antoinelame">GitHub profile</a>.</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=9c8e3e6d36bd" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[How to encrypt files with PHP?]]></title>
            <link>https://medium.com/@antoine.lame/how-to-encrypt-files-with-php-f4adead297de?source=rss-bb104292599f------2</link>
            <guid isPermaLink="false">https://medium.com/p/f4adead297de</guid>
            <category><![CDATA[security]]></category>
            <category><![CDATA[openssl]]></category>
            <category><![CDATA[encryption]]></category>
            <category><![CDATA[php]]></category>
            <dc:creator><![CDATA[Antoine Lamé]]></dc:creator>
            <pubDate>Wed, 20 Oct 2021 09:30:25 GMT</pubDate>
            <atom:updated>2023-06-26T14:21:35.992Z</atom:updated>
            <content:encoded><![CDATA[<h3>How to encrypt files in PHP?</h3><p>If you handle sensitive data, encrypting files before storing them on a server or cloud storage may be necessary. Let’s see how to encrypt and decrypt files in PHP with the openssl_encrypt function.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*HGNYW3XCO86OQwvBc1ReAw.png" /><figcaption>Encrypt files using PHP</figcaption></figure><h3>🚫 Don’t pass the entire content to openssl_encrypt</h3><p>openssl_encrypt function is simple to use: you pass a string and a key (as well as the algorithm to use and the initialisation vector), and it returns the encrypted version of your string.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*Pjj8qMAXhgxlj6RfGrr2gw.png" /></figure><p>You might think that all you have to do is to pass the whole file content into openssl_encrypt to get the encrypted content. This is however a bad idea because it will load the entire file into memory, as well as the encrypted version.</p><p>If you want to encrypt a 30M file, your PHP request will use at least 60M of memory. With larger files, you risk to exceed the <a href="https://www.php.net/manual/fr/ini.core.php#ini.memory-limit">PHP memory limit</a>.</p><p>Let’s try the following code with a 30M file. It loads the entire file content with file_get_contents() and passes it to openssl_encrypt().</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/ab2534ceada96522dba7838a98525856/href">https://medium.com/media/ab2534ceada96522dba7838a98525856/href</a></iframe><pre>➜ php encrypt_file.php</pre><pre>File encrypted!<br>Memory usage: 64.98M</pre><p>64.98M is allocated to PHP. It means <strong>this code won’t work with large files</strong> since we’ll exceed the PHP memory limit.</p><h3>✅ Encrypt smaller chunks of a large file</h3><p>To avoid loading a large file into memory, the best practice is to encrypt smaller chunks of the file, one after the other, and write them into another file.</p><h4>1. Create a random Initialisation Vector (IV)</h4><p>As you may know, when we use openssl_encrypt() or openssl_decrypt() we need to pass a non-null initialisation vector. Let’s generate a random one using openssl_random_pseudo_bytes().</p><h4>2. Store this IV at the beginning of the encrypted file</h4><p>We can’t decrypt anything without the IV, so we have to store this IV somewhere so we can decrypt the content later. It’s not a secret information as the encryption key, so we can store it at the beginning of the encrypted file.</p><h4>3. Take a first chunk, encrypt it with the IV, and store it</h4><p>Now that we have a random IV, we can take a first chunk of the plaintext file, and encrypt it, then store it at the end of the encrypted file.</p><h4>4. Use the first characters of the encrypted chunk for the next IV</h4><p>The idea is to change the IV for each chunk encryption. The next IV will always be the first characters of the last encrypted chunk.</p><h4>5. Take the next chunk and repeat step 3</h4><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/a726f59126ffe8d9ce364293f5345be2/href">https://medium.com/media/a726f59126ffe8d9ce364293f5345be2/href</a></iframe><pre>➜ php encrypt_file.php</pre><pre>File encrypted!<br>Memory usage: 0.38M</pre><p>For the same file, it tooks this time 0.38M of memory to encrypt it. This is much better than the 64.98M of our first approach!</p><p>To decrypt the file, we just have to do the reverse operation.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/8181ebe45b14001d5e72f426b228665d/href">https://medium.com/media/8181ebe45b14001d5e72f426b228665d/href</a></iframe><h3>➡️ Full code</h3><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/5faab61e51a07089f9f6f617255d3ed4/href">https://medium.com/media/5faab61e51a07089f9f6f617255d3ed4/href</a></iframe><p>👋 I offer <strong>tech consulting services</strong> for companies that need help with their Laravel applications. I can assist with upgrades, refactoring, testing, new features, or building new apps. Feel free to contact me through <a href="https://www.linkedin.com/in/antoine-lam%C3%A9/">LinkedIn</a>, or you can find my email on my <a href="https://github.com/antoinelame">GitHub profile</a>.</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=f4adead297de" width="1" height="1" alt="">]]></content:encoded>
        </item>
    </channel>
</rss>