<?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 Roberto Butti on Medium]]></title>
        <description><![CDATA[Stories by Roberto Butti on Medium]]></description>
        <link>https://medium.com/@robertodev?source=rss-769d0c7c70c7------2</link>
        <image>
            <url>https://cdn-images-1.medium.com/fit/c/150/150/1*Ck0z0VXodXpA9hW41Wjo7w.png</url>
            <title>Stories by Roberto Butti on Medium</title>
            <link>https://medium.com/@robertodev?source=rss-769d0c7c70c7------2</link>
        </image>
        <generator>Medium</generator>
        <lastBuildDate>Sat, 05 Dec 2026 21:54:42 GMT</lastBuildDate>
        <atom:link href="https://medium.com/@robertodev/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[PHP, what are dynamic properties?]]></title>
            <link>https://medium.com/geekculture/php-what-are-dynamic-properties-2509c6fbccf8?source=rss-769d0c7c70c7------2</link>
            <guid isPermaLink="false">https://medium.com/p/2509c6fbccf8</guid>
            <category><![CDATA[php]]></category>
            <category><![CDATA[dynamic-property]]></category>
            <category><![CDATA[rfc]]></category>
            <category><![CDATA[breaking-changes]]></category>
            <category><![CDATA[php9]]></category>
            <dc:creator><![CDATA[Roberto Butti]]></dc:creator>
            <pubDate>Tue, 16 Nov 2021 20:41:07 GMT</pubDate>
            <atom:updated>2021-11-17T09:34:05.094Z</atom:updated>
            <content:encoded><![CDATA[<p>These days we hear a lot about dynamic properties in PHP and the possibility of it being deprecated by version 8.2, only to disappear from version 9.<br>The proposal is documented in a dedicated RFC <a href="https://wiki.php.net/rfc/deprecate_dynamic_properties">https://wiki.php.net/rfc/deprecate_dynamic_properties</a>, and it generated a lot of discussion specially on the social networks (like twitter etc.).</p><h3>What are dynamic properties?</h3><p>A property is an attribute/variable declared in a class. For example, <em>$name</em> is a property for class <em>User</em>. We can declare <em>$name</em> at the beginning of the class, and we can declare the visibility (in this case <em>public</em>) and we may declare the type of the property (in this case <em>string</em>):</p><pre>class User {<br>    public string $name;<br>}<br></pre><p>In the methods of the class, we can access to the declared property:</p><pre>class User {<br>    public string $name;</pre><pre>    public function __construct($name=&quot;&quot;) {<br>        $this-&gt;name = $name;<br>    }<br>}</pre><p>Dynamic properties are property that are not declared in the class, but in the code in your class, you want to use a not declared property:</p><pre>class User {<br>    public string $name;</pre><pre>    public function __construct($name=&quot;&quot;, $age = 0) {<br>        $this-&gt;name = $name;<br>        // Assigns the not existent property age<br>        $this-&gt;age = $age;<br>    }<br>}</pre><p>In the example above, in the constructor method I accessed the <em>age</em> property, through <em>$this-&gt;age</em>. In this case PHP will create dynamically the property.</p><h3>An example with dynamic property</h3><pre>class User {<br>    public string $name;</pre><pre>public function __construct($name=&quot;&quot;, $age = 0) {<br>        $this-&gt;name = $name;<br>        // Assigns the not existent property age<br>        $this-&gt;age = $age;<br>    }<br>}</pre><pre>$user = new User;<br>// Assigns declared property User::$name.<br>$user-&gt;name = &quot;foo&quot;;</pre><pre>// Oops, a typo:<br>$user-&gt;nane = &quot;foo&quot;;<br>// PHP &lt;= 8.1: Silently creates dynamic $user-&gt;nane property.<br>// PHP    8.2: Raises deprecation warning, still creates dynamic property.<br>// PHP    9.0: Throws Error exception.<br>echo $user-&gt;nane;</pre><p>This example taken from <a href="https://wiki.php.net/rfc/deprecate_dynamic_properties">https://wiki.php.net/rfc/deprecate_dynamic_properties</a> and modified to add <em>age</em> parameter to show an example with a typo while you are accessing to the property. This is a “hidden” bug, because you think that you performed a correct assignment but at the end you have an unexpected behavior because of the typo (nane instead of name).</p><p>With the PHP 8.0 and 8.1, PHP will create a dynamic property named <em>nane</em>.</p><p>With PHP 8.2 you have a warning during the execution, but the dynamic property is created.</p><p>With PHP 9.0 you will have an exception.</p><h3>What can you do today?</h3><p>My strong suggestion is to use a code static analysis tool like PHPstan or Psalm.</p><p>With PHPstan you can execute it with level 2 to catch any dynamic property usage.</p><pre>phpstan analyze --level=2 .</pre><p>With the latest example you can have:</p><ul><li><em>Access to an undefined property User::$age</em> because of accessing age in the constructor;</li><li><em>Access to an undefined property User::$nane</em> because the typo (nane instead of name).</li></ul><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*vm6Wl-tMBvLZbhvZ9cE-sw.png" /></figure><h3>My considerations</h3><p>This is a great feature with the goal to improve the quality and lower the hidden mistakes.</p><p>This feature, could have a great negative impact on the existing code. Are we sure about that ?</p><p>Some considerations:</p><ul><li>watching the roadmap of PHP 8 and the forecast, we can use PHP 8 for “legacy code” for years;</li><li>if you are using a static code analysis, probably your code doesn’t use dynamic property;</li><li>if you are not using static code, the first thing to do is launch phpstan with level 2.</li></ul><p>So … welcome to “PHP RFC: Deprecate dynamic properties” and let’s prepare for the future ;)</p><p>Do you like or dislike “Deprecate dynamic properties”?</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=2509c6fbccf8" width="1" height="1" alt=""><hr><p><a href="https://medium.com/geekculture/php-what-are-dynamic-properties-2509c6fbccf8">PHP, what are dynamic properties?</a> was originally published in <a href="https://medium.com/geekculture">Geek Culture</a> on Medium, where people are continuing the conversation by highlighting and responding to this story.</p>]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[How to start with Svelte and Tailwind CSS]]></title>
            <link>https://medium.com/geekculture/how-to-start-with-svelte-and-tailwind-css-5df7e084af03?source=rss-769d0c7c70c7------2</link>
            <guid isPermaLink="false">https://medium.com/p/5df7e084af03</guid>
            <category><![CDATA[sveltekit]]></category>
            <category><![CDATA[svelte]]></category>
            <category><![CDATA[tailwind-css]]></category>
            <category><![CDATA[static-site-generator]]></category>
            <category><![CDATA[static-site]]></category>
            <dc:creator><![CDATA[Roberto Butti]]></dc:creator>
            <pubDate>Sat, 25 Sep 2021 12:37:38 GMT</pubDate>
            <atom:updated>2021-10-25T13:01:18.772Z</atom:updated>
            <content:encoded><![CDATA[<figure><img alt="" src="https://cdn-images-1.medium.com/max/1000/0*XE6qPC63JoXPkwRL.png" /></figure><p>In this post, I would like to share with you my process for starting to build a new static website for launching my next open-source project.</p><p>My goal is to build a website with few pages, a home page and some pages for explaining the project.</p><p>To achieve that, I would like to use:</p><ul><li>a kit to help me to structure pages/modules/layout (a couple of pages that share some modules like header, footer, nav bar etc.);</li><li>on-demand file serving over native ESM;</li><li>fast Hot Module Replacement;</li><li>optimized build;</li><li>serve static pre-render files (no server-side logic/language);</li><li>Tailwind CSS for styling;</li><li>follow the same old and boring SEO guidelines.</li></ul><p>I decided to use Svelte for developing the frontend part, and I was starting to use Vite Js with Svelte template. But I see that Svelte ships Svelte Kit, based on Vite Js, more optimized for Svelte features. So I decided to proceed with Svelte Kit.</p><p>The goal of this post is to show you all basics step to have an environment for creating pages, it is not focused on the HTML, CSS, JS development. So I’m going to:</p><ul><li>install Svelte Kit;</li><li>create index and about pages;</li><li>add Tailwind in the “Svelte Kit” way;</li><li>setup correctly the process in order to have a static build, ready to be deployed on service, with no server-side language.</li></ul><h3>Install Svelte Kit</h3><p>Usually I use NPM for managing packages and node modules:</p><pre>npm init svelte@next my-app<br>cd my-app<br>npm install<br>npm run dev</pre><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*njxJjym26hq2b62KdANelA.png" /></figure><p>The <strong><em>npm init</em></strong> command will set up a blank Svelte project. In <strong><em>src/routes</em></strong> you will find your home page ( <strong><em>index.svelte</em></strong>).</p><h3>Create about page</h3><p>If you want to create a new page with URL <strong><em>/about</em></strong>, you need to create a file named <strong><em>about.svelte</em></strong> in <strong><em>src/routes</em></strong> directory. There is an implicit convention that the <strong><em>src/routes</em></strong> structure determines the URL.</p><p>For example:</p><ul><li>URL <strong><em>/</em></strong> is for <strong><em>src/routes/index.svelte</em></strong></li><li>URL <strong><em>/about</em></strong> for <strong><em>src/routes/about.svelte</em></strong></li></ul><p>So now you can create <strong><em>src/routes/about.svelte</em></strong></p><p>For <strong><em>about.svelte</em></strong> file, you can simply copy and paste this snippet:</p><pre>&lt;h1&gt;About page&lt;/h1&gt;<br>&lt;div&gt;Ciao!&lt;/div&gt;</pre><p>If you have your “<strong><em>npm run dev</em></strong>” up and running now, you can go to <a href="http://localhost:3000/about"><strong><em>http://localhost:3000/about</em></strong></a></p><figure><img alt="" src="https://cdn-images-1.medium.com/max/770/1*D5OjKAW8s3n3L1ty1at6Ig.png" /><figcaption>The about page powered by Svelte</figcaption></figure><h3>Add Tailwind CSS</h3><p>Now, we can focus on the style, including Tailwind CSS (with Just In Time option). To do that, I’m going to use “ <strong><em>svelte-add</em></strong>” instead of installing manually the tailwind and PostCSS packages.<br> However, I see that you need to add manually <em>svelte-preprocess</em>. So the commands are:</p><pre>npx svelte-add tailwindcss --jit<br>npm install --save svelte-preprocess</pre><p>Now, you can use your new Tailwind CSS style.</p><p>To do that, update your <strong><em>src/routes/about.svelte</em></strong> file, using some Tailwind CSS classes with:</p><pre>&lt;section class=&quot;text-center w-full h-screen bg-gradient-to-r from-yellow-200 via-red-300 to-pink-300&quot;&gt;<br>&lt;h1 class=&quot;pt-10 text-7xl&quot;&gt;About page&lt;/h1&gt;<br>&lt;div class=&quot;font-serif pt-12 text-2xl&quot;&gt;Ciao!&lt;/div&gt;<br>&lt;/section&gt;</pre><p>If you have your “ <strong><em>npm run dev</em></strong>” up and running, you can see the result on <a href="http://localhost:3000/about"><em>http://localhost:3000/about</em></a>.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/848/1*0sKPnQUHCEDnME4QowVuPw.png" /></figure><h3>Build statics pages (Static Site Generator)</h3><p>In order to serve your pages with no server-side language (node or something else) you need to <strong>pre-render</strong> pages. In this way, you can use any platform for delivering static assets (surge.sh, Vercel, Netlify, GitHub Pages, Amazon S3 + Amazon CloudFront, etc.).<br> To do that, Svelte Kit needs an “adapter” to use during the building process.</p><pre>npm i -D @sveltejs/adapter-static@next</pre><p>In <strong><em>svelte.config.js</em></strong> add the import for the adapter:</p><pre>import adapter from &#39;@sveltejs/adapter-static&#39;;</pre><p>and add the configuration for the adapter in <strong><em>svelte.config.js</em></strong> file:</p><pre>import preprocess from &quot;svelte-preprocess&quot;;<br>import adapter from &#39;<a href="http://twitter.com/sveltejs/adapter-static">@sveltejs/adapter-static</a>&#39;;</pre><pre>const config = {<br>    kit: {<br>        // hydrate the &lt;div id=&quot;svelte&quot;&gt; element in src/app.html<br>        target: &#39;#svelte&#39;,<br>        adapter: adapter({<br>            // default options are shown<br>            pages: &#39;build&#39;,<br>            assets: &#39;build&#39;,<br>            fallback: null<br>        }),<br>        /*<br>        If you need to serve your file from a sub directory<br>        paths: {<br>            base: &#39;/your-sub-dir&#39;,<br>        },<br>        */<br>    },<br>    preprocess: [preprocess({<br>        postcss: true<br>    })]<br>};</pre><pre>export default config;</pre><p>For testing locally your built files, you can use:</p><pre>npm run build</pre><p>With command above, you are going to build your static assets (html, js, and css). They will be stored in <strong><em>build/</em></strong> directory.</p><p>Then for testing your build you can copy somewhere served by a webserver, or you can use the preview option:</p><pre>npm run preview</pre><p>Essentially, it serves locally your built files (not the svelte files, but the built files in your build directory).</p><p>To publish your files on the public server (Netlify, Vercel, Surge.sh, GitHub pages etc.) you need to copy the content of the <strong><em>build/</em></strong> directory.</p><p>If you are using some service for publishing your website, that force you to use a subdirectory to serve your static files (for example GitHub pages), you can use <em>paths</em> in <em>config.kit</em> section:</p><pre>paths: { base: &#39;/your-repo-name&#39;, },</pre><p>I hope that these steps help you to install and set up an environment with Svelte Kit (based on Vite JS) and Tailwind CSS.<br> Feel free to provide any feedback…</p><p><em>Originally published at </em><a href="https://dev.to/robertobutti/how-to-start-building-your-static-website-with-svelte-and-tailwindcss-hbk"><em>https://dev.to</em></a><em> on September 25, 2021.</em></p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=5df7e084af03" width="1" height="1" alt=""><hr><p><a href="https://medium.com/geekculture/how-to-start-with-svelte-and-tailwind-css-5df7e084af03">How to start with Svelte and Tailwind CSS</a> was originally published in <a href="https://medium.com/geekculture">Geek Culture</a> on Medium, where people are continuing the conversation by highlighting and responding to this story.</p>]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[RandoPHP, a PHP package for generating random values]]></title>
            <link>https://levelup.gitconnected.com/randophp-is-a-php-package-for-generating-random-values-4d3ade58c976?source=rss-769d0c7c70c7------2</link>
            <guid isPermaLink="false">https://medium.com/p/4d3ade58c976</guid>
            <category><![CDATA[package]]></category>
            <category><![CDATA[random]]></category>
            <category><![CDATA[php-developers]]></category>
            <category><![CDATA[open-source]]></category>
            <category><![CDATA[php]]></category>
            <dc:creator><![CDATA[Roberto Butti]]></dc:creator>
            <pubDate>Sun, 05 Sep 2021 20:36:13 GMT</pubDate>
            <atom:updated>2021-09-12T12:50:26.705Z</atom:updated>
            <content:encoded><![CDATA[<figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*uLKM4Nn8_xrfeRKhmHP2qQ.png" /></figure><p>RandoPHP is a PHP open source package for generating random numbers, characters, strings and sequences. With this package you can:</p><ul><li><strong>Draw</strong>: Extract random items (sample) from an array. This is useful when you want to “draw” some numbers or items;</li><li><strong>Generate</strong>: useful for create random items like integer, byte, boolean, float, lat/long coordinates, char (numeric, alphabetic, alphanumeric), and sequences like array of integer or char.</li></ul><p>With a fluent interface, you can control some behavior like:</p><ul><li>minimum and maximum value;</li><li>how many items you want to create;</li><li>for sequences if you want or not duplicates (for example in the array [1,5,3,1,1], the value 1 is duplicated).</li></ul><h3>Why RandoPHP</h3><p>I started to write RandoPHP in order to wrap up all methods, functions, approaches to generate values. Every time I needed some Random values in my applications, I see that I was writing the same piece of codes, applying the same controls and validations. Then, I decided to release it with an Open Source package.</p><h3>Installation</h3><p>You can install the package via composer:</p><pre>composer require hi-folks/rando-php</pre><p>Source code is available: <a href="https://github.com/Hi-Folks/rando-php">https://github.com/Hi-Folks/rando-php</a></p><h3>Usage</h3><p>Once you installed the package, you can import the autoload file (if you are not using a framework that provides import all needed files) and them declare the right class via “use” statement, in your PHP file:</p><pre>require __DIR__ . &quot;/vendor/autoload.php&quot;;<br>use HiFolks\RandoPhp\Randomize;</pre><h3>Generate random stuff</h3><h4>Generate Char</h4><p>Sometimes you want to obtain a random char, for example, a <strong>numeric char</strong> (‘0’..‘9’):</p><pre>Randomize::char()-&gt;numeric()-&gt;generate();</pre><p>Or you might want an <strong>alphabetic char</strong> (‘a’ ..‘z’ or ‘A’ ..‘Z’):</p><pre>Randomize::char()-&gt;alpha()-&gt;generate();</pre><p>You can generate both, <strong>alphabetic or numeric char</strong> (‘a’ ..‘z’ or ‘A’ ..‘Z’ or ‘0’..’9&#39;).</p><pre>Randomize::char()-&gt;alphanumeric()-&gt;generate();</pre><p>You can generate a <strong>lower case char</strong> (‘a’..’z’):</p><pre>Randomize::char()-&gt;alpha()-&gt;lower()-&gt;generate();</pre><p>You can generate an <strong>upper case char</strong> (‘A’..’Z’):</p><pre>Randomize::char()-&gt;alpha()-&gt;upper()-&gt;generate();</pre><p>You can generate <strong>a special character</strong>.<br>A special character is one of ‘!”#$%&amp;’()*+,-./:;&lt;=&gt;?@[\]^_`{|}~’:</p><pre>$specialChar = Randomize::char()-&gt;specialCharacters()-&gt;generate();</pre><h4>Generate Boolean</h4><p>Sometimes you want to obtain a <strong>random boolean</strong> true or false (flip a coin, yes or no, red or blue pillow … ):</p><pre>$randomBool = Randomize::boolean()-&gt;generate(); // true or false</pre><h4>Generate a Float</h4><p>Sometimes you want to obtain a <strong>random float</strong> (default min — max range of 0.0–1.0). For example, you want to generate a random number in the range 0.0 to 1.0:</p><pre>$randomFloat = Randomize::float()-&gt;generate();</pre><p>Or you can set the range, for example 0.0 to 90.0, which is equivalent to -&gt;min(0)-&gt;max(90):</p><pre>$randomFloat = Randomize::float()-&gt;min(0)-&gt;max(90)-&gt;generate();</pre><h4>Generate an Integer</h4><p>Sometimes you want to obtain a random integer.</p><pre>$randomNumber = Randomize::integer()-&gt;generate();</pre><p>By default you will obtain an integer between 0 .. 100.</p><p>If you want to customize the range ( min and max ) you have some methods like <em>min()</em>, <em>max()</em>, <em>range()</em> and a construct.</p><p>For example, you want to roll the dice, you can use <em>min()</em> and <em>max()</em> method:</p><pre>$randomNumber = Randomize::integer()-&gt;min(1)-&gt;max(6)-&gt;generate();</pre><p>The same thing using <em>range()</em> method, instead of <em>min()</em> and <em>max()</em>:</p><pre>$randomNumber = Randomize::integer()-&gt;range(1,6)-&gt;generate();</pre><p>You can use a shortcut helper to generate an integer calling the constructor with min and max:</p><pre>$randomNumber = Randomize::integer(1,6)-&gt;generate();</pre><h4>Generate bytes</h4><p>Sometime you want to obtain some <strong>random bytes</strong> (<strong>hexadecimal</strong>). For example, you want to generate a random RGB color (a hex triplet in hexadecimal format):</p><pre>$randomRGB = Randomize::byte()-&gt;length(3)-&gt;generate();</pre><h4>Generate a Date</h4><p>Sometimes you want to obtain a <strong>random date</strong>. For example, you want to generate a random date:</p><pre>$randomDate = Randomize::datetime()-&gt;generate();</pre><p>By default, a date between the first day of current year to the last day of current year.</p><p>If you want to define the range, for example a range of 01 Jan 2020 to 10 Jan 2020, which is equivalent to -&gt;min(‘2020–01–01’)-&gt;max(‘2020–01–10’):</p><pre>$randomDate = Randomize::datetime()-&gt;min(‘2020–01–01’)-&gt;max(‘2020–01–10’)-&gt;generate();</pre><p>You can even specify your preferred format for the random date generated, by using <em>format()</em> method:</p><pre>$randomDate = Randomize::datetime()-&gt;format(‘d-M-Y’)-&gt;generate();</pre><p>You can use <em>min()</em> and <em>max()</em> with<em> format()</em>:</p><pre>$randomDate = Randomize::datetime()-&gt;min(‘2020–01–01’)-&gt;max(‘2020–01–10’)-&gt;format(‘d-M-Y’)-&gt;generate();</pre><p>With the latest example you can obtain something like: “05-Jan-2020”.</p><h4>Generate sequences</h4><p>Sometimes, you want to obtain some random sequences. For example, you want to roll the dice 15 times:</p><pre>$randomRolls = Randomize::sequence()-&gt;min(1)-&gt;max(6)-&gt;count(15)-&gt;generate();<br>// [3,6,5,5,2,2,4,1,4,4,5,4,5,2,6]</pre><p>Sometime you want to obtain some random char sequences. For example, char sequences of length 10:</p><pre>$randomChars = Randomize::sequence()-&gt;chars()-&gt;count(10)-&gt;generate();<br>// [&quot;b&quot;,&quot;q&quot;,&quot;f&quot;,&quot;k&quot;,&quot;o&quot;,&quot;o&quot;,&quot;m&quot;,&quot;i&quot;,&quot;z&quot;,&quot;h&quot;]</pre><p>Or you might want numeric char sequences.</p><pre>$randomChars = Randomize::sequence()-&gt;chars()-&gt;numeric()-&gt;count(10)-&gt;generate();<br>// [&quot;9&quot;,&quot;5&quot;,&quot;8&quot;,&quot;6&quot;,&quot;5&quot;,&quot;4&quot;,&quot;2&quot;,&quot;9&quot;,&quot;7&quot;,&quot;2&quot;]</pre><p>Or you might want alphabetical char sequences.</p><pre>$randomChars = Randomize::sequence()-&gt;chars()-&gt;alpha()-&gt;count(10)-&gt;generate();<br>// [&quot;t&quot;,&quot;f&quot;,&quot;U&quot;,&quot;O&quot;,&quot;r&quot;,&quot;Y&quot;,&quot;C&quot;,&quot;S&quot;,&quot;g&quot;,&quot;J&quot;]</pre><p>Yes, even both.</p><pre>$randomChars = Randomize::sequence()-&gt;chars()-&gt;alphanumeric()-&gt;count(10)-&gt;generate();<br>// [&quot;L&quot;,&quot;5&quot;,&quot;h&quot;,&quot;A&quot;,&quot;c&quot;,&quot;q&quot;,&quot;C&quot;,&quot;W&quot;,&quot;F&quot;,&quot;z&quot;]</pre><p>You want 20 lowercase letters as string, you could use <em>asString()</em> method:</p><pre>$randomString = Randomize::sequence()-&gt;chars()-&gt;alphaLowerCase()-&gt;count(20)-&gt;asString()-&gt;generate();<br>// &quot;ybmzekmvvqpzviswdhob&quot;</pre><p>You want a string with 20 upper case letters:</p><pre>$randomString = Randomize::sequence()-&gt;chars()-&gt;alphaUpperCase()-&gt;count(20)-&gt;asString()-&gt;generate();<br>// &quot;LIYSENBIJRSBWVMEFEVA&quot;</pre><p>Sometimes you want to get some random <strong>numbers array</strong> without duplicates. For example, you want to play “Tombola” (extracting number from 1 to 90 without duplicates):</p><pre>$randomTombola = Randomize::sequence()-&gt;min(1)-&gt;max(90)-&gt;count(90)-&gt;noDuplicates()-&gt;generate();</pre><p>Sometime you want to obtain some <strong>random letters array</strong> without duplicates.</p><pre>$randomChars = Randomize::sequence()-&gt;chars()-&gt;count(10)-&gt;noDuplicates()-&gt;generate();<br>// [&quot;e&quot;,&quot;j&quot;,&quot;b&quot;,&quot;s&quot;,&quot;o&quot;,&quot;z&quot;,&quot;x&quot;,&quot;i&quot;,&quot;c&quot;,&quot;l&quot;]</pre><p>Or you might want <strong>numeric char array</strong> without duplicates. For example, char sequences of length 10:</p><pre>$randomChars = Randomize::sequence()-&gt;chars()-&gt;numeric()-&gt;count(10)-&gt;noDuplicates()-&gt;generate();<br>// [&quot;6&quot;,&quot;0&quot;,&quot;8&quot;,&quot;9&quot;,&quot;4&quot;,&quot;3&quot;,&quot;5&quot;,&quot;7&quot;,&quot;2&quot;,&quot;1&quot;]</pre><p>Or you might want <strong>letters array</strong> without duplicates.</p><pre>$randomChars = Randomize::sequence()-&gt;chars()-&gt;alpha()-&gt;count(10)-&gt;noDuplicates()-&gt;generate();</pre><p>If you want <strong>alphanumeric</strong> (letters and number) <strong>array</strong> without duplicates.</p><p>$randomChars = Randomize::sequence()-&gt;chars()-&gt;alphanumeric()-&gt;count(10)-&gt;noDuplicates()-&gt;generate();</p><h4>Random String</h4><p>If you want to generate random string (alphanumeric, only letters, only numbers, with no duplication, lower case, upper case etc) you can use Random Chars generation.</p><p>To generate a string use Randomize::chars():</p><pre>$string = Randomize::chars()-&gt;generate();</pre><p>Default behavior is:</p><ul><li>10 chars;</li><li>only letters (a-z);</li><li>lower case;</li><li>duplication is allowed (the string ‘aba’ has a duplication for ‘a’ char).</li></ul><p>You can change the default values with some methods and parameters.<br>Methods availabe, to set the type of chars (numeric, alpha, alphanumeric…):</p><ul><li>alpha(): for selecting ‘a-z’, ‘A-Z’;</li><li>alphanumeric(): for selecting ‘a-z’, ‘A-Z’, ‘0–9’;</li><li>numeric(): for selecting ‘0–9’;</li><li>alphaLowerCase(): for selecting ‘a-z’;</li><li>alphaUpperCase(): for selecting ‘A-Z’;</li><li>specialCharacters(): for selecting !”#$%&amp;’()*+,-./:;&lt;=&gt;?@[\]^_`{|}~</li></ul><p>You have also some methods to “control” the output, for example avoid duplications:</p><ul><li>unique() : it generates a string with at most one occurrence per character.</li></ul><p>For example, if you want to generate a string with 16 numeric chars (‘0’-’9&#39;):</p><pre>$string = Randomize::chars(16)-&gt;numeric()-&gt;generate();<br>// &quot;1607986939661884&quot;</pre><p>You could obtain something like this “1607986939661884”.</p><p>If you want to obtain a string with 20 chars, lower case and just letters (‘a’-’z’), and you want to avoid character duplications, you should use <em>alphaLowerCase()</em> and <em>unique()</em> methods:</p><pre>$string = Randomize::chars(20)-&gt;alphaLowerCase()-&gt;unique()-&gt;generate();<br>// &quot;tibuxmhwndjprzlagsqc&quot;</pre><p>You could obtain something like this: ‘tibuxmhwndjprzlagsqc’.</p><p>If you want to obtain a string with 20 chars, with letters and special characters, you should use <em>specialCharacters()</em> and <em>alpha()</em> methods:</p><pre>$string = Randomize::chars(20)-&gt;specialCharacters()-&gt;alpha()-&gt;generate();<br>// &quot;NZ`,{of)&lt;-CeusEX+U&lt;n&quot;</pre><p>You could obtain something like this: “NZ`,{of)&lt;-CeusEX+U&lt;n”.</p><h3>Draw random stuff</h3><p>If you have a list of values and you want to extract/select/draw one or more elements, you could use <em>Draw</em> class instead of <em>Randomize</em>.<br>For using <strong>Draw</strong> class you need to declare <em>HiFolks\RandoPhp\Draw</em>:</p><pre>use HiFolks\RandoPhp\Draw;</pre><p>For example if you have an array with some programming language and you want to pick one:</p><pre>use HiFolks\RandoPhp\Draw;</pre><pre>$randomLanguage = Draw::sample([“PHP”, “Python”, “Golang”, “Javascript”])-&gt;snap();<br>// &quot;PHP&quot;</pre><p>With <em>snap()</em> method you obtain one element of the array.</p><p>If you want to obtain an array as result, you could use <em>extract()</em> method instead of <em>snap()</em>:</p><pre>$array=[“React.js”, “Vue.js”, “Svelte.js”, “Angular.js”, “Alpine.js”, “Vanilla js”];</pre><pre>$randomJs = Draw::sample($array)-&gt;extract();</pre><p>Extract three JS frameworks that you could use in your next project:</p><pre>$array=[“React.js”, “Vue.js”, “Svelte.js”, “Angular.js” , “Alpine.js”, “Vanilla js”];<br>$randomJs = Draw::sample($array)-&gt;count(3)-&gt;extract();</pre><p>Extract three JS framework (duplicates allowed):</p><pre>$array = [“React.js”, “Vue.js”, “Svelte.js”, “Angular.js”, “Alpine.js”, “Vanilla js”];</pre><pre>$randomJs = Draw::sample($array)-&gt;count(3)-&gt;allowDuplicates()-&gt;extract();</pre><h3>Warning</h3><p>Under the hood RandoPHP uses some native PHP functions like:<br>- array_rand(): [PHP Doc for array_rand](<a href="https://www.php.net/manual/en/function.array-rand.php">https://www.php.net/manual/en/function.array-rand.php</a>);<br>- random_int(): [PHP Doc for random_int](<a href="https://www.php.net/manual/en/function.random-int.php">https://www.php.net/manual/en/function.random-int.php</a>);<br>- shuffle(): [PHP Doc for shuffle](<a href="https://www.php.net/manual/en/function.shuffle">https://www.php.net/manual/en/function.shuffle</a>);<br>- random_bytes(): [PHP Doc for random_bytes](<a href="https://www.php.net/manual/en/function.random-bytes">https://www.php.net/manual/en/function.random-bytes</a>).</p><p>These PHP functions use a pseudo random number generator that is not suitable for cryptographic purposes.</p><h3>Some notes</h3><p>To submit ideas or feature requests or issues, please take a look if your request is already there <a href="https://github.com/Hi-Folks/rando-php/issues">https://github.com/Hi-Folks/rando-php/issues</a>.<br>If it is not present, you can create a new one <a href="https://github.com/Hi-Folks/rando-php/issues/new">https://github.com/Hi-Folks/rando-php/issues/new</a></p><p>The PHP versions supported are: ‘8.0’,’7.4&#39;,’7.3&#39;.</p><p>All these versions are used in the CI/CD workflow . If you want to take a look about the workflow used , go to <a href="https://github.com/Hi-Folks/rando-php/blob/master/.github/workflows/php.yml">https://github.com/Hi-Folks/rando-php/blob/master/.github/workflows/php.yml</a></p><p>RandoPHP is released with the Open Source license: “the MIT License (MIT)”.</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=4d3ade58c976" width="1" height="1" alt=""><hr><p><a href="https://levelup.gitconnected.com/randophp-is-a-php-package-for-generating-random-values-4d3ade58c976">RandoPHP, a PHP package for generating random values</a> was originally published in <a href="https://levelup.gitconnected.com/">Level Up Coding</a> on Medium, where people are continuing the conversation by highlighting and responding to this story.</p>]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Laravel Octane is here]]></title>
            <link>https://robertodev.medium.com/laravel-octane-is-here-9ce026f6180e?source=rss-769d0c7c70c7------2</link>
            <guid isPermaLink="false">https://medium.com/p/9ce026f6180e</guid>
            <category><![CDATA[performance]]></category>
            <category><![CDATA[laravel-octane]]></category>
            <category><![CDATA[laravel]]></category>
            <category><![CDATA[application-server]]></category>
            <category><![CDATA[php]]></category>
            <dc:creator><![CDATA[Roberto Butti]]></dc:creator>
            <pubDate>Tue, 06 Apr 2021 20:22:27 GMT</pubDate>
            <atom:updated>2021-04-07T18:57:40.557Z</atom:updated>
            <content:encoded><![CDATA[<figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*gek1FbQ3YtHnzM2JMy-OgQ.png" /></figure><p>With Laravel Octane you can serve your Laravel application using a powerful Application Server for PHP, like Swoole and/or Roadrunner.</p><p>Laravel Octane has the responsibility to boot correctly your application once, keeps it in memory, it cleans object states and manages the instance of the objects across the requests.</p><blockquote>You can obtain source code of Laravel Octane here: <a href="https://github.com/laravel/octane">https://github.com/laravel/octane</a></blockquote><p>Normally, the application and all objects are instanced once for each request. With Laravel Octane the application , the framework instance and the object instances are shared across the requests. It means that the developer needs to consider the state of some objects (remember some of that are instanced once at the boot time of the worker), in this case Laravel Octane helps to manage the states of the objects.</p><blockquote>What is Roadrunner? Roadrunner is an Application Server written in Go, for serving PHP application.</blockquote><p>What we are going to do:</p><ul><li>install a new Laravel application from scratch;</li><li>install Laravel Octane;</li><li>install Roadrunner Application Server;</li><li>launch Laravel Octane;</li><li>install a HTTP Benchmarking tool.</li></ul><p>And then we will execute a comparison benchmark with Nginx and Laravel Octane (Roadrunner).</p><h3>Install and Setup your Application</h3><p>I’m going to create a new Laravel application from scratch. So first of all, I need to install or update “laravel” tool. It is useful for creating a new Laravel application:</p><pre>composer global require laravel/installer</pre><p>Then i can create the new Laravel application:</p><pre>laravel new laravel-octane</pre><p>Then enter in the new directory:</p><pre>cd laravel-octane</pre><p>I suggest to create this application in the directory served by Valet. So you can reach this through your Browser via Nginx : <a href="http://laravel-octane.test/">http://laravel-octane.test</a></p><p>Then you can install and setup Laravel Octane:</p><pre>composer require laravel/octane<br>php artisan octane:install</pre><p>Laravel Octane creates the bridge between you Laravel application and the Application Server, but at the end you need to install the application server. In this case we will use Roadrunner:</p><pre>composer require spiral/roadrunner<br>./vendor/bin/rr  get-binary<br>chmod u+x rr</pre><p>You don’t need to launch that. If you are familiar with Roardrunner, usually you need to create <em>.rr.yaml</em> file and launch <em>rr</em> command. Instead, with Laravel Octane, the following Artisan Command will take in charge to launch the application server with the right configuration. To launch the application server and expose your Laravel application:</p><pre>php artisan octane:start</pre><p>If everything is fine, you could open your browser and access to your application at the URL: <a href="http://127.0.0.1:8000/">http://127.0.0.1:8000</a></p><p>If you want to change the port, you can use the <em>port</em> option:</p><pre>php artisan octane:start --port=8002</pre><p>You can specify also how many worker processes will be started. The default is the number of logical CPUs. If you know exactly what are you doing, you can use <em>workers</em> option:</p><pre>php artisan octane:start --port=8002 --workers=8</pre><h3>Execute test comparison, Nginx VS Roadrunner</h3><p>In order to retrieve some insight about the performance and mimic a lot of concurrent traffic on your Laravel application, I’m going to use wrk <a href="https://github.com/wg/wrk">https://github.com/wg/wrk</a>. If you have a macOS and you are using Homebrew , you can install wrk:</p><pre>brew install wrk</pre><p>With Wrk you can generate a lot of concurrent request for a defined amount of time.</p><p>If you launch wrk in order to generate traffic:</p><ul><li><a href="http://laravel-octane.test/">http://laravel-octane.test/</a> : it is resolved with local address 127.0.0.1 and will reply Nginx;</li><li><a href="http://laravel-octane.test:8000/">http://laravel-octane.test:8000/</a> : it is resolved with local address and the port 8000 is bind by Roadrunner.</li></ul><p>I’m going to execute wrk with:</p><ul><li>4 threads to use;</li><li>20 connections to keep open;</li><li>10 secondo of duration of test.</li></ul><p>So for testing Nginx:</p><pre>wrk -t4 -c20 -d10s <a href="http://laravel-octane.test/">http://laravel-octane.test</a></pre><p>For testing Laravel Octane (Roadrunner)</p><pre>wrk -t4 -c20 -d10s <a href="http://laravel-octane.test:8000/">http://laravel-octane.test:8000</a></pre><p>The result are different.</p><p>For Nginx:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*ocCGNxrXr7pIoxMMBaAbkw.png" /><figcaption>Launch wrk on Nginx</figcaption></figure><p>For Laravel Octane:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*PZ7tfaR9WXMa-SspYV4ZAg.png" /><figcaption>Launch wrk on Laravel Octane</figcaption></figure><h3>Some considerations</h3><p>The main difference with serving the Laravel application with Laravel Octane + Roadrunner instead of the classic Web server is:</p><ul><li>The Application is bootstrapped once, and multiple request will share same resources;</li><li>The construct of the controller are called once (when the worker is instanced);</li><li>Think about what happens to “static” class attribute.</li></ul><p>Let me explain this with an example.</p><p>Create 2 routes in routes/web.php:</p><pre>use App\Http\Controllers\MainController;<br>Route::get(&#39;/&#39;, [MainController::class, &#39;index&#39;]);<br>Route::get(&#39;/show&#39;, [MainController::class, &#39;show&#39;]);</pre><p>Create MainController.php in app/Http/Controllers directory where you:</p><ul><li>define a static attribute (array) $list;</li><li>define index method where you append a random integer to the self::$list array;</li><li>define show method where you use and show the self::$list array</li></ul><pre>&lt;?php</pre><pre>// app/Http/Controllers/MainController.php</pre><pre>namespace App\Http\Controllers;<br>use Illuminate\Http\Request;<br>class MainController extends Controller{<br>  public static $list = [];<br>  public function index()  {<br>    self::$list[]= \random_int(1,6);<br>    return view(&#39;welcome&#39;);<br>  }<br>  public function show()  {<br>    return view(&#39;show&#39;, [&quot;list&quot; =&gt; self::$list]);<br>  }<br>}</pre><p>Then create a very basic blade file as resources/views/show.blade.php:</p><pre>{{-- resources/views/show.blade.php --}}</pre><pre>&lt;h2&gt;LIST:&lt;/h2&gt;</pre><pre>&lt;p&gt;{{ implode(&quot;,&quot;,$list) }}&lt;/p&gt;</pre><pre>&lt;p&gt;DONE&lt;/p&gt;</pre><p>If you call multiple time <a href="http://laravel-turbo.test:8080/">http://laravel-octane.test:8080/</a> it will be called the <em>index</em> method so, some integers will be added to your list. The static list is shared across the request on the same worker. So if you try to call <a href="http://laravel-turbo.test:8080/show">http://laravel-octane.test:8080/show</a> it will be called show method where list attribute will be used.</p><p>So I think that working in an Application Server context for a PHP developer is a great changes specially thinking about the life cycle of the request. A PHP developer typically is used to have all the Framework instanced in every request.</p><p>Do you agree with those considerations?</p><p>Feel free to drop some comments and/or feedbacks.</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=9ce026f6180e" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[I Can’t Wait for Laravel Octane]]></title>
            <link>https://medium.com/geekculture/i-cant-wait-for-laravel-octane-6697d1d355c1?source=rss-769d0c7c70c7------2</link>
            <guid isPermaLink="false">https://medium.com/p/6697d1d355c1</guid>
            <category><![CDATA[roadrunner]]></category>
            <category><![CDATA[php]]></category>
            <category><![CDATA[application-server]]></category>
            <category><![CDATA[laravel]]></category>
            <category><![CDATA[octane]]></category>
            <dc:creator><![CDATA[Roberto Butti]]></dc:creator>
            <pubDate>Mon, 29 Mar 2021 20:05:33 GMT</pubDate>
            <atom:updated>2021-04-06T20:27:39.914Z</atom:updated>
            <content:encoded><![CDATA[<figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*CVWnCmdUDcFZ6Eqj6KqGSw.png" /></figure><p>I can’t wait for Laravel Octane, so I started playing with Laravel and Roadrunner.</p><blockquote>On 6th April 2021, Laravel Octane Beta is released, so this article is obsolete. My suggestion is to use Laravel Octane with Roadrunner. To do that, you can follow this post: <a href="https://robertodev.medium.com/laravel-octane-is-here-9ce026f6180e">https://robertodev.medium.com/laravel-octane-is-here-9ce026f6180e</a></blockquote><p>During Laracon 2021 🎉🎉🎉, and after Taylor’s great “Laravel Octane” speech, I was super excited about serving Laravel application with an Application Server with multiple workers.</p><p>Under the hood Laravel Octane could be used with Roadrunner, so I decided to play a little bit directly with Roadrunner.</p><p>What is Roadrunner: Roadrunner is an Application Server written in Go, for serving PHP application.</p><h3>Install</h3><p>What we are going to do:</p><ul><li>install a new Laravel application from scratch</li><li>install a HTTP Benchmarking tool</li><li>install Roadrunner</li><li>install Laravel “bridge” for using Roadrunner with Laravel framework</li><li>configure Roadrunner</li><li>start the Application Server</li><li>execute a comparison benchmark with Nginx and Roadrunner</li><li>show some consideration, because using an Application Server is different when you use a Web Server.</li></ul><h3>Install new Laravel application from scratch</h3><p>I’m going to create a new Laravel application from scratch.</p><p>So first of all, I need to install or update “laravel” tool. It is useful for creating a new Laravel application:</p><pre>composer global require laravel/installer</pre><p>Then i can create the new Laravel application:</p><pre>laravel new laravel-turbo<br>cd laravel-turbo</pre><p>I suggest to create this application in the directory served by Valet. So you can reach this through your Browser via Nginx : <a href="http://laravel-turbo.test/">http://laravel-turbo.test</a></p><h3>Install the HTTP Benchmarking Tool</h3><p>In order to retrieve some insight about the performance and mimic a lot of concurrent traffic on your Laravel App, I’m going to use “<em>Wrk</em>” <a href="https://github.com/wg/wrk">https://github.com/wg/wrk</a>.</p><p>If you have a macOS and you are using Homebrew , you can install “<em>Wrk</em>”:</p><pre>brew install wrk</pre><p>With <em>Wrk</em> you can generate many simultaneous requests for a defined period of time. Later I will use it to run some benchmarks. Before that, let me install the application server and its bridge for Laravel.</p><h3>Install Roadrunner</h3><p>Now I’m going to install the Application Server, later I will install the Laravel bridge.</p><p>The GitHub repository of the Application Server is: <a href="https://github.com/spiral/roadrunner-binary">https://github.com/spiral/roadrunner-binary</a></p><p>To install Roadrunner, you need:</p><ul><li>install the PHP package of the Application Server;</li><li>get the binary (executable files) of the application server;</li><li>make sure that <em>rr</em> binary is executable (executable permission);</li><li>configure the application server via <em>.rr.yaml</em> file.</li></ul><pre>composer require spiral/roadrunner:v2.0 nyholm/psr7<br>./vendor/bin/rr  get-binary<br>chmod u+x rr</pre><h3>Install Roadrunner Laravel Bridge</h3><p>For Laravel to work properly with the Application Server, you need to install Roadrunner Laravel Bridge and then publish the configuration file for Laravel Bridge:</p><pre>composer require spiral/roadrunner-laravel &quot;^4.0&quot;<br>php artisan vendor:publish --provider=&#39;Spiral\RoadRunnerLaravel\ServiceProvider&#39; --tag=config</pre><h3>Configure Roardrunner</h3><p>The configuration for the Application Server is in the file <em>.rr.yaml</em>.</p><p>A basic configuration for our example could be something like:</p><pre>server:<br>  # If you want to use Unix socket for internal communication:<br>  #command: &quot;php ./vendor/bin/rr-worker start --relay-dsn unix://rr-laravel-rpc.sock&quot;<br>  #relay: &quot;unix://rr-laravel-rpc.sock&quot;<br>  # If you want to use TCP socket for internal communication:<br>  command: &quot;php ./vendor/bin/rr-worker start --relay-dsn tcp://127.0.0.1:7001&quot;<br>  relay: &quot;tcp://127.0.0.1:7001&quot;<br>​<br>http:<br>  # The Web application is served via 8080 port<br>  address: 0.0.0.0:8080<br>  middleware: [&quot;headers&quot;, &quot;static&quot;, &quot;gzip&quot;]<br>  pool:<br>    num_workers: 4<br>    max_jobs: 64<br>    supervisor:<br>      exec_ttl: 60s<br>  headers:<br>    response:<br>      X-Powered-By: &quot;RoadRunner&quot;<br>  static:<br>    dir: &quot;public&quot;<br>    forbid: [&quot;.php&quot;]<br>​</pre><h3>Start the Application Server</h3><p>Start the application server with new “<em>rr</em>” executable just downloaded:</p><pre>./rr serve -c ./.rr.yaml</pre><p>Once the Application Server is started, according to the configuration (<em>num_workers</em>) 4 workers are started, ready to serve the Laravel application.</p><p>According to the “<em>address</em>” parameter in configuration file, the Laravel application is exposed on 8080 port.</p><h3>Execute test comparison, Nginx VS Roadrunner</h3><p>For executing tests, I’m going to use “<em>wrk</em>” to generate traffic and 2 different URL, each URL is referring two different service:</p><ul><li><a href="http://laravel-turbo.test/">http://laravel-turbo.test/</a> : is resolved with local address 127.0.0.1 and will reply Nginx Web Server;</li><li><a href="http://laravel-turbo.test:8080/">http://laravel-turbo.test:8080/</a> : is resolved with local address and the port 8080 is bind by Roadrunner Application Server.</li></ul><p>I’m going to use some specific option:</p><ul><li>4 threads to use;</li><li>20 connections to keep open;</li><li>10 secondo of duration of test.</li></ul><p>So for testing Nginx:</p><pre>wrk -t4 -c20 -d10s <a href="http://laravel-turbo.test/">http://laravel-turbo.test</a></pre><p>For testing Roadrunner:</p><pre>wrk -t4 -c20 -d10s <a href="http://laravel-turbo.test:8080/">http://laravel-turbo.test:8080</a></pre><p>The result are different.</p><p>For Nginx I had 478 requests in ten seconds:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*QOwSZl5gQ30UPq_nGm42SQ.png" /><figcaption>4 threads, 20 connections keep open, to Nginx</figcaption></figure><p>For Roardrunner I had 1728 requests in ten seconds</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*C6lhMubxu-jqvXMf4JD-hw.png" /></figure><p>The difference is impressive. Roadrunner is much faster than Nginx.</p><h3>Some considerations</h3><p>The main difference with serving the Laravel application with Roadrunner instead of the classic Web server is:</p><ul><li>The Application is bootstrapped once, and multiple request will share same resources.</li><li>The construct of the controller are called once (when the worker is instanced)</li><li>Think what happens to “static” class attribute</li></ul><p>Let me explain an example.</p><p>Create 2 routes in routes/web.php:</p><pre>use App\Http\Controllers\MainController;</pre><pre>Route::get(&#39;/&#39;, [MainController::class, &#39;index&#39;]);</pre><pre>Route::get(&#39;/show&#39;, [MainController::class, &#39;show&#39;]);</pre><p>Create MainController.php in app/Http/Controllers directory where you:</p><ul><li>define a static attribute (array) $list;</li><li>define index method where you append a random integer to the self::$list array;</li><li>define show method where you use and show the self::$list array</li></ul><pre>&lt;?php</pre><pre>namespace App\Http\Controllers;</pre><pre>use Illuminate\Http\Request;</pre><pre>class MainController extends Controller</pre><pre>{</pre><pre>  public static $list = [];</pre><pre>  public function index()</pre><pre>  {</pre><pre>    self::$list[]= \random_int(1,6);</pre><pre>    return view(&#39;welcome&#39;);</pre><pre>  }</pre><pre>  public function show()</pre><pre>  {</pre><pre>    return view(&#39;show&#39;, [&quot;list&quot; =&gt; self::$list]);</pre><pre>  }</pre><pre>}</pre><p>If you call multiple time http://laravel-turbo.test:8080/ it will be called index method so some integer will be added to your list. The static list is shared across the request on the same worker. So if you try to call <a href="http://laravel-turbo.test:8080/show">http://laravel-turbo.test:8080/show</a> it will be called show method where list attribute will be used.</p><p>So I think that working in an Application Server context for a PHP developer is a great changes specially thinking about the life cycle of the request. A PHP developer typically is used to have all the Framework instanced in every request.</p><p>Probably Laravel Octane will be responsible to expose to Laravel a clean state for each request making the life easier for PHP developers.</p><p>Do you agree with those considerations?</p><p>Feel free to drop some comments and/or feedbacks.</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=6697d1d355c1" width="1" height="1" alt=""><hr><p><a href="https://medium.com/geekculture/i-cant-wait-for-laravel-octane-6697d1d355c1">I Can’t Wait for Laravel Octane</a> was originally published in <a href="https://medium.com/geekculture">Geek Culture</a> on Medium, where people are continuing the conversation by highlighting and responding to this story.</p>]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Setup a VM for Laravel 8 with Ubuntu 20.04, PHP8, Nginx and MySql]]></title>
            <link>https://levelup.gitconnected.com/setup-a-vm-for-laravel-8-with-ubuntu-20-04-php8-nginx-and-mysql-4b709de88154?source=rss-769d0c7c70c7------2</link>
            <guid isPermaLink="false">https://medium.com/p/4b709de88154</guid>
            <category><![CDATA[nginx]]></category>
            <category><![CDATA[droplet]]></category>
            <category><![CDATA[ubuntu]]></category>
            <category><![CDATA[laravel]]></category>
            <category><![CDATA[php]]></category>
            <dc:creator><![CDATA[Roberto Butti]]></dc:creator>
            <pubDate>Mon, 15 Feb 2021 20:38:39 GMT</pubDate>
            <atom:updated>2021-03-11T09:16:06.204Z</atom:updated>
            <content:encoded><![CDATA[<figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*6PXc1jYO4mapPqomJXv_Jg.png" /><figcaption>Setup a Droplet for Laravel 8</figcaption></figure><p>My goal is to setup a GNU/Linux VM with Ubuntu 20.04 LTS, PHP8, Nginx and MySql to serve a Laravel 8 Application. For the Web Server I’m going to use Nginx so I will need php-fpm.</p><p>There are some ways to achieve that, you could consider to use:</p><ul><li>a software that helps you to maintain servers like Forge (<a href="https://forge.laravel.com/">https://forge.laravel.com/</a>);</li><li>a Serverless approach, using Vapor (<a href="https://vapor.laravel.com/">https://vapor.laravel.com/</a>);</li><li>an App Platform like DigitalOcean App Platform (<a href="https://www.digitalocean.com/products/app-platform/">https://www.digitalocean.com/products/app-platform/</a>)</li><li>a Linux Virtual Machine provided by DigitalOcean Droplets.</li></ul><p>For my side projects usually I use DigitalOcean Droplets because:</p><ul><li>it is a “manual” approach, but one of my goals, when I work on side project, is to learn and understand the processes/tools/configurations “under the hood”;</li><li>the costs are predictable;</li><li>I like to get my hands dirty on my side project.</li></ul><p>For my work projects, I usually choose “managed” solutions.</p><p>A thought about automation. I’m a fan of “automate everything”. But before to automate a process, or a setup, or a workflow, I think we need to figure out what’s going on under the hood.</p><h3>Setup Droplet</h3><p>As I mentioned before, I’m going to use a Droplet by DigitalOcean. A droplet is essentially a VM with an Operating System where you can add your software stack, with tools and applications provided by the operating system. In our case I will select Ubuntu 20.04, so I will add software with Apt. When you create a Droplet you can select some parameters, like operating system, VM size (cpu/memory), datacenter location…</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*WID3hiNXECeqVbb_mMMQFg.png" /><figcaption>Creating a droplet, you need to select some options</figcaption></figure><p>The cheapest configuration (but with less performance):</p><ul><li>Choose image: Distributions Ubuntu 20.04 LTS</li><li>Choose a plan: shared CPU Basic — 5 $ / month (1 GB , 1 CPU, 25 GB SSD Disk, 1000 GB transfer)</li><li>Choose a datacenter region : Frankfurt 1 (the closest to Italy), if you want to perform a speed test you could use this tool: <a href="http://speedtest-fra1.digitalocean.com/">http://speedtest-fra1.digitalocean.com/</a></li><li>Authentication, you can choose SSH Keys if you are able to upload your public key, otherwise you could use the classic way login / password for root user.</li></ul><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*IKCPEC8Mag92exMw0Mgj6Q.png" /><figcaption>The authentication method. My suggestion is to use SSH keys instead of a very long complex password.</figcaption></figure><p>Once you create the Droplet you can see your new IPv4 address.</p><p>This is very important to access to your new virtual machine.</p><h3>Your first access</h3><p>If you selected SSH key Auth method instead of login/password, when you created your droplet, I suggest you to create an SSH alias in your machine in order to have a shortcut for defining the “ssh” user name, the IP address and the key to use to access to your VM.</p><p>In this case in your <em>~/.ssh/config, </em>add these lines:</p><pre>host &lt;alias_name&gt;<br>    HostName &lt;ip_address&gt;<br>    User &lt;user_name&gt;<br>    IdentityFile &lt;key_file&gt;</pre><p>Where:</p><ul><li>&lt;alias_name&gt; is the name used when you will launch ssh;</li><li>&lt;ip_address&gt; the IP address provided by the Droplet;</li><li>&lt;user_name&gt; the user, for the initial configuration it is ok using <em>root</em>. During the setup process we will create a “no root” user;</li><li>&lt;key_file&gt; the ssh key file, in my case is <em>~/.ssh/id_rsa_sideproject_dev</em>;</li></ul><p>To access to your Droplet you can use ssh:</p><pre>ssh &lt;alias_name&gt;</pre><p>With the first access, you will be prompted a question about the creation of ECDSA key fingerprint. Select “yes” to accept it.</p><p>If you selected login/password method instead of SSH keys, you could use ssh root@&lt;ip_address&gt; to access to your VM.</p><h3>Upgrade your system packages</h3><p>First of all, I suggest you to upgrade your packages with Apt command (we are using Ubuntu so we have Apt). With the first access you are using “root” user, so for now you don’t need to use sudo .</p><pre>apt-get update<br>apt-get upgrade</pre><p>Now, we can start with the installation of all packages needed:</p><ul><li>Nginx</li><li>PHP 8 (fpm) (and the modules needed by Laravel 8)</li><li>MySql 8</li></ul><p>You are installing some services, so my suggestion is to configure also the firewall.</p><h3>Install Nginx</h3><pre>apt-get install nginx</pre><p>Optional: if you want to host multiple domains (or sub domains) I suggest to create one directory for each sub domain you want to serve with Nginx. Anyway, the default directory is : /var/www/html/. So, after installing Nginx you try to go tu url http://&lt;ip_address&gt;, you will see the default html file served by nginx from /var/www/html directory.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*zSb0jWKQ0FMP0L34j2o7yw.png" /><figcaption>The welcome page of a new fresh installation of Nginx</figcaption></figure><p>We are going to install Laravel so we need to set <em>public/</em> as document root.</p><pre>mkdir -p /var/www/mydomain/htdocs/public</pre><p>In this case:</p><ul><li>/var/www/mydomain/htdocs will be the directory where your Laravel application will be copied;</li><li>/var/www/mydomain/htdocs/public will be the document root.</li></ul><h3>Install PHP 8</h3><p>We just installed Ubuntu 20.04 that ships PHP 7.4. Before to install PHP we need to add a repository in order to have the opportunity to install PHP 8. So we’ll install PHP from the <em>ondrej/php</em> PPA repository. To enable the repo and update packages:</p><pre>apt install software-properties-common<br>add-apt-repository ppa:ondrej/php<br>apt update<br>apt upgrade</pre><p>Now you can install PHP 8:</p><pre>apt install php8.0-fpm</pre><p>To test if you have PHP 8:</p><pre>php -v</pre><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*pKGmyvYeT4hZlCfiepgTIg.png" /><figcaption>Your new PHP 8</figcaption></figure><p>To check if the PHP-fpm is up and running:</p><pre>systemctl status php8.0-fpm</pre><p>Now we can install all PHP dependencies for your Laravel 8 application:</p><pre>apt-get install php8.0-mysql php8.0-mbstring php8.0-xml php8.0-bcmath</pre><h3>Configure Nginx and PHP 8 fpm</h3><p>Now you are using <em>root</em> user, and it could be fine for initial setup. I suggest to create an unprivileged user in order to login to the system and access to the directory where is deployed your Laravel application.</p><pre>adduser deploy<br>usermod -aG www-data deploy<br>chown -R deploy:www-data /var/www/mydomain/</pre><p>Now let’s focus on the Nginx configuration, we can start from the default configuration for Nginx and then we will customize it:</p><pre>cp /etc/nginx/sites-available/default /etc/nginx/sites-available/mydomain</pre><p>Edit your new file following this example:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/688ef3e23a0745a74294e4acd310f9d8/href">https://medium.com/media/688ef3e23a0745a74294e4acd310f9d8/href</a></iframe><p>Then, you need to create a symbolic link in order to activate the new configuration (take a look sites-available VS. sites-enabled):</p><pre>ln -s /etc/nginx/sites-available/mydomain /etc/nginx/sites-enabled/</pre><p>If you want to check the syntax of your configuration:</p><pre>nginx -t</pre><p>To restart Nginx and load your new configuration:</p><pre>service nginx restart</pre><h3>Install Database</h3><p>We are going to install MySql server 8:</p><pre>apt install mysql-server<br>mysql_secure_installation</pre><p>during the execution of this script, it will ask you to type the root database password. When the execution is completed, try to access to your database server:</p><pre>mysql -u root -p</pre><p>and type the new password to access to the database administration console. In the database administration console:</p><ul><li>create the database <em>db_laravel8</em>;</li><li>create the user to access the database (<em>userdb</em>) with the password <em>somepassword</em></li><li>grant the right privileges to the new user;</li></ul><pre>CREATE DATABASE db_laravel8;<br>CREATE USER &#39;userdb&#39;@&#39;localhost&#39; IDENTIFIED BY &#39;somepassword&#39;;<br>GRANT ALL PRIVILEGES ON * . * TO &#39;userdb&#39;@&#39;localhost&#39;;<br>FLUSH PRIVILEGES;</pre><h3>Setup Firewall</h3><p>Open only HTTP and SSH ports:</p><pre>ufw allow &#39;Nginx HTTP&#39;<br>ufw allow &#39;OpenSSH&#39;</pre><p>Activate the firewall:</p><pre>ufw enable</pre><p>And finally check the status:</p><pre>ufw status</pre><p>The output should be something like this:</p><pre>Status: active<br>To Action From<br> — — — — — — <br>Nginx HTTP ALLOW Anywhere<br>OpenSSH ALLOW Anywhere<br>Nginx HTTP (v6) ALLOW Anywhere (v6)<br>OpenSSH (v6) ALLOW Anywhere (v6)</pre><h3>Setup HTTPS</h3><p>For side project usually I use <a href="https://letsencrypt.org/">https://letsencrypt.org/</a> to obtain the certificate to enable HTTPS protocol.</p><p>As root , to install the tool to install certificate, run:</p><pre>apt-get install certbot python3-certbot-nginx</pre><p>If you need to install a certificate for hostname <em>host.mydomain.com</em>:</p><pre>certbot --nginx -d host.mydomain.com</pre><p>This command will update also the Nginx configuration file (enabling directive <em>listen</em> on port 443 and load certificate just created).</p><p>You need also to open your firewall for HTTPS ports:</p><pre>ufw allow &#39;Nginx HTTPS&#39;</pre><h3>Wrap up</h3><p>If you followed these steps we:</p><ul><li>created a VM on DigitalOcean with Ubuntu 20.04 LTS</li><li>installed Nginx to serve files via HTTP;</li><li>installed PHP8 and the modules needed by Laravel 8</li><li>installed MySql 8 server;</li><li>configured Nginx in order to serve PHP files and a specific directory where we can copy Laravel files;</li><li>configured an unprivileged user to access to the VM;</li><li>configured a database user and create a new database to use it with Laravel Application (remember to update your .env file with DB_NAME, DB_USER, DB_PASSWORD);</li><li>setup HTTPS in order to serve Laravel application.</li></ul><p>Now you can deploy your application in /var/www/mydomain/htdocs.</p><p>Feel free to drop comments and feedback, I could improve this tutorial (or my english ;) )</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=4b709de88154" width="1" height="1" alt=""><hr><p><a href="https://levelup.gitconnected.com/setup-a-vm-for-laravel-8-with-ubuntu-20-04-php8-nginx-and-mysql-4b709de88154">Setup a VM for Laravel 8 with Ubuntu 20.04, PHP8, Nginx and MySql</a> was originally published in <a href="https://levelup.gitconnected.com/">Level Up Coding</a> on Medium, where people are continuing the conversation by highlighting and responding to this story.</p>]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Vitejs and React]]></title>
            <link>https://medium.com/swlh/vitejs-and-react-eb3f1afc6e6?source=rss-769d0c7c70c7------2</link>
            <guid isPermaLink="false">https://medium.com/p/eb3f1afc6e6</guid>
            <category><![CDATA[react]]></category>
            <category><![CDATA[javascript]]></category>
            <category><![CDATA[reactjs]]></category>
            <category><![CDATA[vites]]></category>
            <category><![CDATA[front-end-development]]></category>
            <dc:creator><![CDATA[Roberto Butti]]></dc:creator>
            <pubDate>Wed, 27 Jan 2021 17:58:26 GMT</pubDate>
            <atom:updated>2021-09-19T08:24:30.086Z</atom:updated>
            <content:encoded><![CDATA[<figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*BQnNNwFWxkavlG72yPJFaA.png" /></figure><p>Vite is a new frontend build tool that aims to improve the developer experience for development with the local machine, and for the build of optimized assets for production (go live).</p><p>Vite (or ViteJS) includes:</p><ul><li>a development server with ES _native_ support and Hot Module Replacement;</li><li>a build command based on rollup.js to output highly optimized static assets for production.</li></ul><p>If you are interested in Vite my suggestion is to start reading the official doc, “Getting Started” chapter: <a href="https://vitejs.dev/guide/">https://vitejs.dev/guide/</a>.</p><p>Vite is developed by the creator of VueJS. But one of the most important feature of Vite is that is frontend agnostic. So you can use Vite with Vue, but also with React.</p><p>In this post I would like to show you how to use Vite with React and show how is amazing fast the building phase.</p><p>The small demo that I’m going to build is a simple Roll The Dice application.</p><p>The user can click on a button and some simple JS logic will produce a random number. The frontend part will be updated in a reactive way. Very simple but useful to understand how much is easy to setup the frontend workflow with Vite (also using React).</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*R3xjwPflPQboKFETAKixdg.png" /><figcaption>The Demo application built with Vite + React + Tailwind CSS</figcaption></figure><h3>Setup your app</h3><p>You can use the scaffold to setup the Vite app, with React templates. The following command will:</p><ul><li>create the directory <em>rollthedice-vite-react</em> with the scaffold;</li><li>enter in the new directory;</li><li>install Npm packages;</li><li>execute the development WebServer.</li></ul><pre>npm init vite@latest rollthedice-vite-react -- --template react<br>cd rollthedice-vite-react<br>npm install<br>npm run dev</pre><figure><img alt="" src="https://cdn-images-1.medium.com/max/846/1*ilf9FK-LqRvD0yeMCkTKPg.png" /><figcaption>The local development server is started in few milliseconds</figcaption></figure><p>If you switch on your browser, and go to the URL: <a href="http://localhost:3000/">http://localhost:3000/</a> you will see a small web application up and running.</p><p>Now the Vite part is completed, so we can focus on the React part.</p><h3>The React Component</h3><p>Let’s create the React component: <em>src/components/RollTheDice.jsx</em>.</p><pre>mkdir -p src/components<br>touch src/components/RollTheDice.jsx</pre><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/0a61d5a180049cb51cec29fca60cc8f5/href">https://medium.com/media/0a61d5a180049cb51cec29fca60cc8f5/href</a></iframe><ul><li>001: using the hook <em>setState</em>, declare a new state variable “<em>dice</em>” to track the current roll, and the related function used to update the value of the state (<em>setNumber</em>);</li><li>002: using the hook <em>setState</em>, declare a new state variable (array) “<em>rolls</em>” to store all generated rolls. The function declared to update the rolls is <em>setRolls;</em></li><li>003 : implementing <em>useEffect</em> hook in order to change the title of the page on every changes of value of dice;</li><li>004: implement <em>roll</em> function, in order to generate a random number 1 .. 6;</li><li>005: setting state for value of <em>dice</em> calling the <em>setNumber</em> function (previously declared in the <em>useState</em> hook);</li><li>006: setting state for array for tracking all rolls, calling <em>setRolls</em> function (previously declared in the <em>useState</em> hook);</li><li>007: implement <em>reset</em> function;</li><li>008: resetting state for value of dice to 0, calling the <em>setNumber</em> function (previously declared in the <em>useState</em> hook);</li><li>009: resetting state for rolls array to empty array, calling <em>setRolls</em> function (previously declared in the <em>useState</em> hook);</li><li>010: return the HTML for the component. In this simple example the component is declared as Function;</li><li>011: conditional render elements inline using the JavaScript conditional operator <em>condition ? true : false</em>;</li><li>012: show value of state variable “<em>dice</em>” (it automatically changes when it changes the value of the state variable);</li><li>013: calling <em>roll()</em> function on click event;</li><li>014: calling<em> reset()</em> function on click event;</li><li>015: conditional render elements;</li><li>016: walk through the “<em>rolls</em>” array. The element is refreshed every time a value is added to the array;</li><li>017: display the number of rolls (it is refreshed every time a value is added to the array).</li></ul><h3>Include the new React Component</h3><p>Once the React Component is completed, you need to use it in another “parent” template or in the main App.jsx file.</p><p>If you need to include the new React component, for example in <em>src/App.jsx</em> file, you need to:</p><ul><li>import the component with a name (<em>RollTheDice</em>);</li><li>use in the template the new element <em>&lt;RollTheDice&gt;</em>.</li></ul><pre>import React, { useState } from &#39;react&#39;</pre><pre>import RollTheDice from &#39;./components/RollTheDice&#39;<br>function App() {<br>  return (<br>    &lt;div className=&quot;App&quot;&gt;<br>      &lt;RollTheDice&gt;&lt;/RollTheDice&gt;<br>    &lt;/div&gt;<br>  )<br>}</pre><pre>export default App</pre><h3>Launch the Local WebServer</h3><p>If you already launched the local WebServer with “<em>npm run dev</em>” you will see an instant reload every time you save your files (the RollTheDice.jsx component or the App.jsx file).</p><p>If your local WebServer is not running, launch “<em>npm run dev</em>” from your Terminal application.</p><h3>Prepare your files for the “GO LIVE”</h3><p>If you want to publish your file on a public server (Netlify, Vercel, Surge) you can build your html and assets executing:</p><pre>npm run build</pre><p>All your generated static files are stored in <em>dist/</em> directory, so you can copy them on your public server.</p><h3>Feedback, share, and follow</h3><p>If you think that this post is useful or you think that there is room for improvement, please drop a constructive comment and follow me in order to motivate me to write more additional infos for example:</p><ul><li>adding tailwind in React Vite application;</li><li>adding graphs to show statistical distributions of randomly generated values;</li><li>or feel free to ask new topic.</li></ul><h3>References</h3><ul><li>Vite: <a href="https://vitejs.dev/guide/introduction.html">https://vitejs.dev/guide/introduction.html</a></li><li>React hook state: <a href="https://reactjs.org/docs/hooks-state.html">https://reactjs.org/docs/hooks-state.html</a></li><li>React hook effect: <a href="https://reactjs.org/docs/hooks-effect.html">https://reactjs.org/docs/hooks-effect.html</a></li><li>React conditional rendering: <a href="https://reactjs.org/docs/conditional-rendering.html#inline-if-else-with-conditional-operator">https://reactjs.org/docs/conditional-rendering.html#inline-if-else-with-conditional-operator</a></li><li>Source Code: <a href="https://github.com/roberto-butti/vite-react-rollthedice">https://github.com/roberto-butti/vite-react-rollthedice</a></li><li>My Twitter profile: <a href="https://twitter.com/RmeetsH">https://twitter.com/RmeetsH</a></li></ul><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=eb3f1afc6e6" width="1" height="1" alt=""><hr><p><a href="https://medium.com/swlh/vitejs-and-react-eb3f1afc6e6">Vitejs and React</a> was originally published in <a href="https://medium.com/swlh">The Startup</a> on Medium, where people are continuing the conversation by highlighting and responding to this story.</p>]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Start Using PHP8]]></title>
            <link>https://medium.com/swlh/start-using-php-8-e6f2c8240be7?source=rss-769d0c7c70c7------2</link>
            <guid isPermaLink="false">https://medium.com/p/e6f2c8240be7</guid>
            <category><![CDATA[development]]></category>
            <category><![CDATA[php]]></category>
            <category><![CDATA[php-8]]></category>
            <category><![CDATA[docker]]></category>
            <category><![CDATA[php-developers]]></category>
            <dc:creator><![CDATA[Roberto Butti]]></dc:creator>
            <pubDate>Wed, 04 Nov 2020 08:33:40 GMT</pubDate>
            <atom:updated>2021-01-22T20:10:44.143Z</atom:updated>
            <content:encoded><![CDATA[<h3>Start Using PHP 8</h3><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*2tiSt4AlBT05T3EQKBfXWw.png" /><figcaption>Using PHP8 with Docker</figcaption></figure><p>If you want to try the new release of PHP 8 and you want to install it on your local machine without having side effects on your current PHP projects and your current PHP 7 stack, I have some suggestion for you.</p><p>I think that the safest and quickest way to install and use PHP 8 is running PHP in an isolated environment. To achieve that, you can use a Docker image with all things needed by PHP runtime avoiding negative impact or conflicts with your current local PHP 7 installation.</p><h3>Install Docker</h3><p>In order to install Docker , if you have a Mac device with macOS, you could install it via Homebrew:</p><pre>brew cask install docker</pre><p>Once the execution is completed, you have install Docker Desktop in: “<em>/Applications/Docker.app</em>”.</p><p>Docker Desktop is useful to launch all “internal” services needer by docker.</p><p>So, launch Docker Desktop. In your status bar , you will se the docker icon</p><figure><img alt="Docker status" src="https://cdn-images-1.medium.com/max/249/1*RVWwSJMGCTwlgAdT_N6IZQ.png" /><figcaption>Docker Desktop is running</figcaption></figure><p>With Docker, you have also a command line tool named “docker” very useful to launch, manage, install images.</p><h3>Install and launch PHP 8 environment</h3><p>Our goal is to use an official image that ships PHP 8.</p><p>In order to download PHP 8 image and launch it, you can run:</p><pre>docker run -it --rm php:8.0-cli-alpine</pre><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*_X_VItgi2cZLxDEYQZM9-g.png" /><figcaption>docker run -it — rm php:8.0-cli-alpine</figcaption></figure><p>If you see “<em>php &gt;</em>” prompt, everything is fine.</p><p>In order to verify that you are using PHP 8, you could show the magic constant PHP_VERSION.</p><pre>echo PHP_VERSION;</pre><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*MGkQyzOUg0sZjlsxJYB2bQ.png" /><figcaption>echo PHP_VERSION;</figcaption></figure><h3>PHP 8 environment and your local source code</h3><p>The previous example is useful to verify if your PHP 8 environment is setup correctly.</p><p>In a real case scenario, probably you have your PHP files/script in your local directory (on your filesystem) and you want that your new PHP 8 installation uses these files.</p><p>So, in your local directory create e new simple PHP file “test.php”:</p><pre>&lt;?php<br>echo &quot;Welcome to PHP&quot; . PHP_EOL;<br>echo &quot;--- &quot; .PHP_VERSION . PHP_EOL;<br>echo &quot;--------------&quot; . PHP_EOL;</pre><p>Typically to execute that PHP script, you should run “php test.php”. In this way you are using your local PHP installation (you are not using the PHP 8 inside your new docker image). In order to execute your script, using new PHP8 stack, use this docker command:</p><pre>docker run -it -v &quot;$PWD&quot;:/usr/src/app -w /usr/src/app php:8.0-cli-alpine php test.php</pre><p>Essentially we are mapping the current local directory (“$PWD”) with a internal directory of the docker image (/usr/src/app), executing the image “php:8.0-cli-alpine” and running “php test.php”:</p><ul><li><strong><em>docker run -it</em></strong>: it executes docker image;</li><li><strong><em>-v “$PWD”:/usr/src/app</em></strong> : map your current local directory (<em>“$PWD”</em>) with the <em>/usr/src/app</em> directory of your PHP docker image;</li><li><strong><em>-w /usr/src/app</em></strong> : make the <em>/usr/src/app</em> the current directory once you launch the docker image (and remember that <em>/usr/src/app</em> is mapped with your current local directory, so if you have a file in your current local directory, you can access to it from your docker image in <em>/usr/src/app</em> directory)</li><li><strong><em>php:8.0-cli-alpine</em></strong> : your PHP8 image</li><li><strong><em>php test.php</em></strong> : the command that you want to run. With the previous options/argument you are running php from your new docker image and using test.php that is accessible via <em>/usr/src/app</em> directory.</li></ul><h3>Modules</h3><p>I think this is the quickest way if you want to build a kind of playground where you can try new functionalities of PHP8.</p><p>There are a lot of article that covers new PHP8 features, my suggestion are:</p><ul><li><a href="https://stitcher.io/blog/new-in-php-8">https://stitcher.io/blog/new-in-php-8</a></li><li><a href="https://pociot.dev/32-php-8-try-out-all-new-features#php-8-try-out-all-new-features">https://pociot.dev/32-php-8-try-out-all-new-features#php-8-try-out-all-new-features</a></li><li>video tutorials: <a href="https://spatie.be/videos/front-line-php/the-match-expression">https://spatie.be/videos/front-line-php/the-match-expression</a></li></ul><p>If you are curious about which PHP modules are include with this image you could execute:</p><pre>docker run -it php:8.0-cli-alpine php -m</pre><p>In the next post I will cover how to customize your PHP 8 image with new modules.</p><h3>Upgrade the image</h3><p>If you want to update your image, you can update it via:</p><pre>docker pull php:8.0-cli-alpine</pre><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=e6f2c8240be7" width="1" height="1" alt=""><hr><p><a href="https://medium.com/swlh/start-using-php-8-e6f2c8240be7">Start Using PHP8</a> was originally published in <a href="https://medium.com/swlh">The Startup</a> on Medium, where people are continuing the conversation by highlighting and responding to this story.</p>]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Vue3, Using ref() or reactive()]]></title>
            <link>https://medium.com/swlh/vue3-using-ref-or-reactive-88d47c8f6944?source=rss-769d0c7c70c7------2</link>
            <guid isPermaLink="false">https://medium.com/p/88d47c8f6944</guid>
            <category><![CDATA[vue-3]]></category>
            <category><![CDATA[frontend]]></category>
            <category><![CDATA[vue]]></category>
            <category><![CDATA[javascript]]></category>
            <category><![CDATA[vuejs]]></category>
            <dc:creator><![CDATA[Roberto Butti]]></dc:creator>
            <pubDate>Wed, 29 Jul 2020 18:45:45 GMT</pubDate>
            <atom:updated>2022-08-20T17:12:49.566Z</atom:updated>
            <content:encoded><![CDATA[<figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*ct_PGaqcrVGM2_8o177qzg.png" /><figcaption>Using ref() or reactive() in Vue (vue3)</figcaption></figure><p>In my previous post, I implemented my first Vue3 component.</p><p>I implemented a very simple web app (roll the dice) where I had a “<em>div</em>” for showing the result, and a button to trigger the roll and generate a random number from 1 to 6.</p><p>In that case, I used “<em>ref()</em>” to create reactive properties.</p><blockquote>“My first Vue3 component” article where I used ref() for declaring reactive properties: <a href="https://medium.com/swlh/my-first-vue3-component-6e1ef1670544">https://medium.com/swlh/my-first-vue3-component-6e1ef1670544</a></blockquote><h3>The ref() “approach”</h3><p>For example in “<em>setup</em>” script section of my component I declared “<em>dice</em>” property in order to store the last result of the roll action.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*NitZiGcDhy3tLtfDR2aQSA.png" /><figcaption>Using ref()</figcaption></figure><p>The “<em>dice</em>” property was an integer, initialized with <em>0</em>. To make it reactive, I used the “<em>ref()</em>” function to initialize it. So “<em>dice</em>” was not just a simple integer but an object:</p><pre>const dice = ref(0);</pre><figure><img alt="" src="https://cdn-images-1.medium.com/max/576/1*nfrYSJUG6swJGkZnGucDXQ.png" /><figcaption>The property “dice” is not just a simple integer. It is a object with value attribute</figcaption></figure><p>In this way “<em>dice</em>” is an object with a “<em>value</em>” attribute. This means that, if you want to access the “<em>dice</em>” value in your component, you need to use “<em>dice.value</em>”.</p><h3>From ref() to reactive()</h3><p>Another way to make the properties reactive is to use the “<em>reactive()</em>” function.</p><p>The first difference from the developer&#39;s perspective is that using “<em>ref()</em>” you need to declare each single property:</p><pre>const dice = ref(0);<br>const rolls = ref([]);</pre><p>With “<em>reactive()</em>” function you need to collect all properties in one object:</p><pre>const game = reactive(<br>  {<br>    dice: 0,<br>    rolls: []<br>  }<br>)</pre><p>With this syntax you can pass use them in the template accessing the properties of the object, for example, “<em>game.dice</em>”.</p><p>Another difference is in the properties&#39; usage. You can avoid using “<em>.value</em>” but you need to use it as object property “<em>game.dice</em>”. For example:</p><pre>function restart() {<br>  game.dice=0<br>  game.rolls = [];<br>}</pre><h3>The template</h3><p>You can access to all the properties and functions defined in the game reactive object.</p><pre>&lt;template&gt;<br>  &lt;h1&gt;Number is: {{ game.dice }}&lt;/h1&gt;<br>  &lt;div&gt;Number of rolls: {{ game.rolls.length }}&lt;/div&gt;<br>  &lt;div&gt;Total: {{ game.total }}&lt;/div&gt;<br>  &lt;button <a href="http://twitter.com/click">@click</a>=&quot;roll()&quot;&gt;Let&#39;s roll the dice&lt;/button&gt;<br>  &lt;button <a href="http://twitter.com/click">@click</a>=&quot;restart()&quot;&gt;Restart&lt;/button&gt;<br>  &lt;ul&gt;<br>    &lt;li  v-for=&quot;(t, index) in game.rolls&quot; :key=&quot;index&quot;&gt;<br>       {{ t }}<br>    &lt;/li&gt;<br>  &lt;/ul&gt;<br>&lt;/template&gt;</pre><h3>Walking through the code</h3><pre>&lt;script setup&gt;<br>// ##001 : we are using script setup<br>// ##002 : import from vue3:<br>// - reactive for making properties reactive;<br>// - computed for computed function like total<br>import { reactive, computed } from &quot;vue&quot;;<br>const game = reactive(<br>  {<br>    dice: 0,<br>    rolls: [],<br>    // ##003 : we include also computed properties in game object<br>    total: computed(<br>      () =&gt; {<br>        let temptotal = 0;<br>        for (let i = 0; i &lt; game.rolls.length; i++) {<br>          temptotal = temptotal + game.rolls[i]<br>        }<br>        return temptotal;<br>      }<br>    )<br>  }<br>)<br>// ##004: implement roll function<br>function roll() {<br>  game.dice = Math.floor(Math.random() * Math.floor(5)) + 1;<br>  game.rolls.unshift(game.dice);<br>}<br>// ##005: implement restart function<br>function restart() {<br>  game.dice = 0<br>  game.rolls = [];<br>}<br>&lt;/script&gt;</pre><ul><li>##001 script setup, we are using script setup instead of the Composition API setup(). Composition API setup(): <a href="https://vuejs.org/api/composition-api-setup.html">https://vuejs.org/api/composition-api-setup.html</a> . Script setup: <a href="https://vuejs.org/api/sfc-script-setup.html">https://vuejs.org/api/sfc-script-setup.html</a></li><li>##002 import from vue3: <em>reactive</em> (for making properties reactive), <em>computed</em> (for computed functions);</li><li>##003 declare the reactive object with properties and also computed properties. To make it reactive we will use “<em>reactive()</em>” function;</li><li>##004 we can include also “computed properties” in the reactive object. You can access computed properties accessing to the object properties (<em>game.total</em> in this case);</li><li>##005 implement the <em>roll()</em> function in order to change the value of <em>dice</em> and add on top of the array <em>game.rolls</em> the new <em>game.dice</em> value (to keep track of all rolls);</li><li>##006 implement the <em>restart()</em> function in order to re-initialize “<em>game.dice</em>” and “<em>game.rolls</em>” (we are accessing them as object attribute).</li></ul><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*-30PVMGEwXoYh4bW4x40FA.gif" /><figcaption>The Vue3 “reactive()” component</figcaption></figure><h3>References</h3><p>This is a list of useful links:</p><ul><li>the sources: <a href="https://github.com/roberto-butti/vue3-demo-rollthedice">https://github.com/roberto-butti/vue3-demo-rollthedice</a></li><li>the component with reactive(): <a href="https://github.com/roberto-butti/vue3-demo-rollthedice/blob/master/src/components/RollTheDice.vue">https://github.com/roberto-butti/vue3-demo-rollthedice/blob/master/src/components/RollTheDiceReactive.vue</a></li><li>the component with ref(): <a href="https://github.com/roberto-butti/vue3-demo-rollthedice/blob/master/src/components/RollTheDice.vue">https://github.com/roberto-butti/vue3-demo-rollthedice/blob/master/src/components/RollTheDice.vue</a></li><li>Vite web dev build tool: <a href="https://github.com/vitejs/vite">https://github.com/vitejs/vite</a></li><li>Vue3 composition API cheat sheet: <a href="https://www.vuemastery.com/pdf/Vue-3-Cheat-Sheet.pdf">https://www.vuemastery.com/pdf/Vue-3-Cheat-Sheet.pdf</a></li><li>Vue composition API: <a href="https://composition-api.vuejs.org/api.html">https://composition-api.vuejs.org/api.html</a></li><li>Vue3 essential course by Vuemastery: <a href="https://www.vuemastery.com/courses/vue-3-essentials/">https://www.vuemastery.com/courses/vue-3-essentials/</a></li></ul><p>I’m exploring Vue3 so if you have some feedback, please let me know in the comments. Thank you!</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=88d47c8f6944" width="1" height="1" alt=""><hr><p><a href="https://medium.com/swlh/vue3-using-ref-or-reactive-88d47c8f6944">Vue3, Using ref() or reactive()</a> was originally published in <a href="https://medium.com/swlh">The Startup</a> on Medium, where people are continuing the conversation by highlighting and responding to this story.</p>]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[My first Vue3 component]]></title>
            <link>https://medium.com/swlh/my-first-vue3-component-6e1ef1670544?source=rss-769d0c7c70c7------2</link>
            <guid isPermaLink="false">https://medium.com/p/6e1ef1670544</guid>
            <category><![CDATA[vuejs]]></category>
            <category><![CDATA[vue]]></category>
            <category><![CDATA[javascript]]></category>
            <category><![CDATA[web-development]]></category>
            <category><![CDATA[vue-3]]></category>
            <dc:creator><![CDATA[Roberto Butti]]></dc:creator>
            <pubDate>Sat, 04 Jul 2020 09:34:01 GMT</pubDate>
            <atom:updated>2022-08-20T17:10:31.769Z</atom:updated>
            <content:encoded><![CDATA[<figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*y2pgsdDwXnf1AwDnk-VR-g.png" /><figcaption>How to build a Vue 3 reactive component with Vite 3.</figcaption></figure><p>I like to work with Vuejs, and today I tried to use the next version of Vuejs: Vue3.</p><p>My first question was: how can I create a new Web Application with Vue3 and the right npm dependencies?</p><p>To do that, I used Vite as a build tool. Vite is an amazing web dev build tool that serves your code via native ES Module imports during dev and bundles it with <a href="https://rollupjs.org/">Rollup</a> for production. Vite uses Vue3. So if we use Vite we have an amazing fast build tool and we have all vue3 dependencies ready to use.</p><blockquote>This post is updated for using ViteJS version 3</blockquote><h3>Install the skeleton for Vue 3</h3><p>In order to install Vite and your sample application based on Vue3:</p><pre>npm create vite@latest rollthedice -- --template vue<br>cd rollthedice<br>npm i<br>npm run dev</pre><p>Now you can open your browser at: <a href="http://localhost:5173/">http://localhost:5173/</a></p><figure><img alt="Vite and Vue example page" src="https://cdn-images-1.medium.com/max/1024/1*IOuUUM6_C0oPVptrCUpaFw.png" /><figcaption>Vite + Vue starter page</figcaption></figure><p>Yes, you are running a Vue3 web application.</p><h3>Roll the dice web application</h3><p>Now I would like to create a very simple “Roll the Dice” web application.</p><p>The user can click a button in order to generate a random number (from 1 to 6) and update the text where the result is shown and the list of the previous rolls. This example is very simple but useful for walking through the basic stuff of Vue3, starting to understand the main differences with Vue2, and looking at the CompositionAPI.</p><p>So now let’s jump on the code.</p><h3>The Code</h3><p>The command “<em>npm create vite@latest</em>” created for you a basic skeleton of Vue3 web application. Now we will focus on 2 files.</p><ul><li><strong>src/App.vue</strong>: where your component is loaded</li><li><strong>src/components/RollTheDice.vue</strong>: is the file that we will create from scratch with the template and the logic of our component (the button, the labels, the function that generates a random number). By default, our skeleton provides a HelloWorld.vue basic component. My suggestion for you is to take a look at this file, just to understand the structure. You will not see so many differences with Vue2. Now, create a new empty file <em>RollTheDice.vue</em> in the <em>src/components</em> directory, where we will test and try some basic functionalities of Vue3.</li></ul><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*xVNsAXOJrTNDo8PLlqz72A.png" /><figcaption>The basic structure of your Vue3 web application</figcaption></figure><h4>The src/App.vue file</h4><p>The main goal of <em>App.vue</em> file is to load the component.</p><pre>&lt;template&gt;<br>&lt;!-- ##001: use the component in template --&gt;<br>  &lt;RollTheDice /&gt;<br>&lt;/template&gt;</pre><pre>&lt;script&gt;<br>// ##002: import the component<br>import RollTheDice from &#39;./components/RollTheDice.vue&#39;</pre><pre>export default {<br>  name: &#39;App&#39;,<br>  components: {<br>    // ##003: declare the component<br>    RollTheDice<br>  }<br>}<br>&lt;/script&gt;</pre><ul><li>##001: we are using the component in the template, in this case we don’t need to pass any props to the component so &lt;RollTheDice /&gt; is enough;</li><li>##002: import the component;</li><li>##003: declare the component</li></ul><p>So, as you can see, no differences with Vue2.</p><p>Let’s create the new component file: <em>src/components/RollTheDice.vue.</em></p><h4>The new src/components/RollTheDice.vue file</h4><p>The component file is a little longer than the <em>App.vue</em>.</p><p>Let me split it into two parts. The first one is the template part, where we will use some variables and functions that we will cover in the script section.</p><p>So, two-part: one is the <em>template</em> and the last one is the <em>script</em>. Both are included in the same file <em>RollTheDice.vue</em>.</p><h4>The template</h4><p>The first part of the file is for the template:</p><pre>&lt;template&gt;<br>  &lt;h1&gt;Number is: {{ dice }}&lt;/h1&gt;<br>  &lt;div&gt;Number of rolls: {{ rolls.length }}&lt;/div&gt;<br>  &lt;div&gt;Total: {{ total }}&lt;/div&gt;<br>  &lt;button <a href="http://twitter.com/click">@click</a>=&quot;roll()&quot;&gt;Let&#39;s roll the dice&lt;/button&gt;<br>  &lt;button <a href="http://twitter.com/click">@click</a>=&quot;restart()&quot;&gt;Restart&lt;/button&gt;<br>  &lt;ul&gt;<br>    &lt;li  v-for=&quot;(t, index) in rolls&quot; :key=&quot;index&quot;&gt;<br>       {{ t }}<br>    &lt;/li&gt;<br>  &lt;/ul&gt;<br>&lt;/template&gt;</pre><p>As you can see we are using:</p><ul><li>some <em>reactive</em> variables: <em>dice</em>, an array <em>rolls</em> (we are using length attribute);</li><li>a computed function <em>total</em>, computed function is to recalculate the value once of the reactive variables (used in the computed function) changes;</li><li>a couple of functions called by a user action (click on a button): <em>roll()</em> and <em>restart().</em></li></ul><h4>The script</h4><pre>&lt;script setup&gt;<br>// ##001 : we are using script setup <br>// ##002 : import ref and computed from vue3<br>import { ref, computed } from &quot;vue&quot;;</pre><pre>// ##003 : declare and initialize 2 reactive variables dice and rolls<br>const dice = ref(0);<br>const rolls = ref([]);<br>// ##004: implement roll function (inside setup() )<br>function roll() {<br>    dice.value = Math.floor(Math.random() * Math.floor(5)) + 1;<br>    rolls.value.unshift(dice.value);<br>}<br>// ##005: implement restart function (inside setup() )<br>function restart() {<br>    dice.value = 0<br>    rolls.value = [];<br>}<br>// ##006: define a computed function (total)<br>const total = computed(() =&gt; {<br>    let temptotal = 0;<br>    for (let i = 0; i &lt; rolls.value.length; i++) {<br>        temptotal = temptotal + rolls.value[i]<br>    }<br>    return temptotal;<br>});</pre><pre>&lt;/script&gt;</pre><ul><li>##01 setup script, we are defining a script setup instead of using the setup function (composition API). If you are interested in understanding the differences “script setup” VS “composition API setup()” take a look <a href="https://vuejs.org/api/sfc-script-setup.html">https://vuejs.org/api/sfc-script-setup.html</a>;</li><li>##002 import <em>ref</em> and <em>computed</em> from Vue3: we need to import some functionality from Vue3 like <em>ref</em> for declaring variables as reactive and <em>computed</em> for declaring computed function;</li><li>##003 declare 2 reactive objects <em>dice</em> and <em>rolls</em>. To make them reactive we will “encapsulate” the number and the array in <em>ref</em> objects. So <em>dice</em> is not just a simple integer but it is a <em>ref</em> object. For this reason, later when we will need to access to <em>dice</em> value we will use <em>dice.value</em>. We will do the same for the ref array <em>rolls</em>.</li><li>##004 implement the <em>roll()</em> function in order to change the value of <em>dice</em> and add on top of the array <em>rolls</em> the new <em>dice</em> value (to keep track of all rolls);</li><li>##005 implement the restart() function in order to re-initialize dice and rolls (we are accessing to them via <em>value</em> attribute);</li><li>##006 define the <em>total</em> computed function. When something will change in rolls array, the total is evaluated again. In the template we can use total computed function as we use the variables.</li></ul><p>That’s all for now. So if you have your “npm run dev” you have a hot reload provided by Vite so go to your browser, go to <a href="http://localhost:5173/">http://localhost:5173/</a> and take a look at your new Vue3 web application.</p><h4>The result</h4><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*JUXyxLl_yoHQfUc3aMOkvA.gif" /><figcaption>Roll the dice</figcaption></figure><h3>References</h3><p>This is a list of useful links:</p><ul><li>Vite web dev build tool: <a href="https://github.com/vitejs/vite">https://github.com/vitejs/vite</a></li><li>Vue3 composition API cheat sheet: <a href="https://www.vuemastery.com/pdf/Vue-3-Cheat-Sheet.pdf">https://www.vuemastery.com/pdf/Vue-3-Cheat-Sheet.pdf</a></li><li>Vue composition API: <a href="https://composition-api.vuejs.org/api.html">https://composition-api.vuejs.org/api.html</a></li><li>Vue3 essential course by Vuemastery: <a href="https://www.vuemastery.com/courses/vue-3-essentials/">https://www.vuemastery.com/courses/vue-3-essentials/</a></li><li>I released also the sources: <a href="https://github.com/roberto-butti/vue3-demo-rollthedice">https://github.com/roberto-butti/vue3-demo-rollthedice</a></li></ul><p>I’m exploring Vue3 so if you have some feedback, please let me know in the comments. Thank you!</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=6e1ef1670544" width="1" height="1" alt=""><hr><p><a href="https://medium.com/swlh/my-first-vue3-component-6e1ef1670544">My first Vue3 component</a> was originally published in <a href="https://medium.com/swlh">The Startup</a> on Medium, where people are continuing the conversation by highlighting and responding to this story.</p>]]></content:encoded>
        </item>
    </channel>
</rss>