<?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 Dariusz Włodarczyk on Medium]]></title>
        <description><![CDATA[Stories by Dariusz Włodarczyk on Medium]]></description>
        <link>https://medium.com/@Volmarg?source=rss-28a4f8831724------2</link>
        <image>
            <url>https://cdn-images-1.medium.com/fit/c/150/150/1*HIls41KmxsjHTVvuDNj_aw.jpeg</url>
            <title>Stories by Dariusz Włodarczyk on Medium</title>
            <link>https://medium.com/@Volmarg?source=rss-28a4f8831724------2</link>
        </image>
        <generator>Medium</generator>
        <lastBuildDate>Tue, 01 Dec 2026 14:42:31 GMT</lastBuildDate>
        <atom:link href="https://medium.com/@Volmarg/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[Symfony Form — JsonType]]></title>
            <link>https://medium.com/@Volmarg/symfony-form-jsontype-ddc7a086ff4e?source=rss-28a4f8831724------2</link>
            <guid isPermaLink="false">https://medium.com/p/ddc7a086ff4e</guid>
            <category><![CDATA[web-development]]></category>
            <category><![CDATA[programming]]></category>
            <category><![CDATA[symfony]]></category>
            <category><![CDATA[php]]></category>
            <dc:creator><![CDATA[Dariusz Włodarczyk]]></dc:creator>
            <pubDate>Sun, 20 Feb 2022 11:47:36 GMT</pubDate>
            <atom:updated>2022-02-24T13:17:02.223Z</atom:updated>
            <content:encoded><![CDATA[<h3>Symfony Form — JsonType</h3><p>Handling Json input</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/600/0*lNokD9CyGhJ4Lc6P.jpg" /></figure><p><strong>Use case</strong></p><p>Imagine given use case of sending whole <strong>JSON</strong> via form (by encoding array on Frontend) like that:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/534/1*kZfawIQ0BkyBKa2Rr32-qg.png" /><figcaption>Data sending &amp; handling</figcaption></figure><p><strong>Related article</strong>: <a href="https://medium.com/nerd-for-tech/vue-js-and-symfony-forms-ba5139f77f5f">Handling Vue.js form in Symfony</a></p><p><strong>The issue</strong></p><p><strong>Symfony</strong> form handler allows using different input types like <em>TextType</em>, <em>ArrayChoiceList</em>, <em>ChoiceType</em> etc. while using <em>Array</em> based type the result is an empty array. The only one working case is <em>TextType</em> but it still yields just raw json — but that’s undesired result.</p><p><strong>JsonType</strong></p><p>After struggling for a while, the only working solution I’ve found is providing new input type (JsonType) and new DataTransformer like that:</p><p><strong>DataTransformer</strong></p><pre><em>&lt;?php<br><br>namespace </em>App\Form\DataTransformer;<br><br><em>use </em>Symfony\Component\Form\DataTransformerInterface;<br><br><em>/**<br> * Handles transforming json to array and backward<br> */<br>class </em>JsonTransformer <em>implements </em>DataTransformerInterface<br>{<br><br>    <em>/**<br>     * </em><strong><em>@inheritDoc<br>     </em></strong><em>*/<br>    public function </em>reverseTransform($value): <em>mixed<br>    </em>{<br>        <em>if </em>(<em>empty</em>($value)) {<br>            <em>return </em>[];<br>        }<br><br>        <em>return json_decode</em>($value);<br>    }<br><br>    <em>/**<br>     * </em><strong><em>@ihneritdoc<br>     </em></strong><em>*/<br>    public function </em>transform($value): <em>mixed<br>    </em>{<br>        <em>if </em>(<em>empty</em>($value)) {<br>            <em>return json_encode</em>([]);<br>        }<br><br>        <em>return json_encode</em>($value);<br>    }<br>}</pre><p><strong>JsonType</strong></p><pre><em>&lt;?php<br><br>namespace </em>App\Form\Type;<br><br><em>use </em>App\Form\DataTransformer\JsonTransformer;<br><em>use </em>Symfony\Component\Form\AbstractType;<br><em>use </em>Symfony\Component\Form\Extension\Core\Type\TextType;<br><em>use </em>Symfony\Component\Form\FormBuilderInterface;<br><em>use </em>Symfony\Component\Serializer\SerializerInterface;<br><br><em>/**<br> * Handles json based input (for example multiselect tag)<br> * Will only transform the json to array (and back),<br> *<br> * If necessary, could probably accept custom options (className) which would be used to<br> * transform the data into given class via {</em><strong><em>@see </em></strong><em>SerializerInterface}<br> */<br>class </em>JsonType <em>extends </em>AbstractType<br>{<br>    <em>/**<br>     * </em><strong><em>@inheritDoc<br>     </em></strong><em>*/<br>    public function </em>getParent(): <em>string<br>    </em>{<br>        <em>return </em>TextType::<em>class</em>;<br>    }<br><br>    <em>/**<br>     * </em><strong><em>@inheritDoc<br>     </em></strong><em>*/<br>    public function </em>buildForm(FormBuilderInterface $builder, <em>array </em>$options): <em>void<br>    </em>{<br>        $builder-&gt;addModelTransformer(<em>new </em>JsonTransformer());<br>    }<br>}</pre><p><strong>Summary</strong></p><p>The end result is an array transformed from the json.</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=ddc7a086ff4e" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Splitting Symfony project into chunks]]></title>
            <link>https://medium.com/@Volmarg/splitting-symfony-project-into-chunks-c297b5a55dab?source=rss-28a4f8831724------2</link>
            <guid isPermaLink="false">https://medium.com/p/c297b5a55dab</guid>
            <category><![CDATA[php]]></category>
            <category><![CDATA[web-development]]></category>
            <category><![CDATA[software-development]]></category>
            <category><![CDATA[backend]]></category>
            <category><![CDATA[symfony]]></category>
            <dc:creator><![CDATA[Dariusz Włodarczyk]]></dc:creator>
            <pubDate>Sun, 19 Dec 2021 08:06:33 GMT</pubDate>
            <atom:updated>2021-12-20T14:21:19.877Z</atom:updated>
            <content:encoded><![CDATA[<figure><img alt="" src="https://cdn-images-1.medium.com/max/700/0*ejDJd2rZvDjZ1bNy" /></figure><h3>Why would You even split?</h3><p>I’ve been (and still am) traveling often between 2 countries. My main desktop PC (more powerful RIG) stays in one country, while I always take my laptop when visiting second one.</p><p>Now the problem arose:</p><ul><li>My laptop is around 7 years old and pretty outdated,</li><li>My desktop is also a bit old but has more powerful RIG,</li></ul><p>While I can run <strong>Symfony</strong> server, <strong>Websocket</strong> server, <strong>debugging tools</strong> and <strong>Vue.js</strong> dev tools (with constant on fly compiling and code analyse), it simply chokes on laptop and it literally cries for help.</p><h3>(over)chunking</h3><p>Chunking bigger <strong>Symfony</strong> project is actually very easy. Whenever You need to ad some additional logic to the project think of it this way:</p><blockquote><em>“Can this logic be standalone code? Can I eventually reuse it? Is it maybe to small to split or is it integral part of current workspace?</em>”</blockquote><p>You don’t want to end up splitting Your code to much. It might be tempting at first but it just doesn’t make much sense to create small bundle providing service which wraps up other composer package logic. Following this way may even lead to issues where You might eventually end up with tones of packages in Your own repository (because You definitely want to store them somewhere).</p><p>Consider an example of having shopping platform:</p><ul><li>The core itself with user login / accounts / shopping logic should remain a core of the project,</li><li>Products providing logic (let it be integration with external services) could be a <em>“Product-Provider-Bundle”,</em></li><li><em>“Opinions-Collector-Bundle”</em> could be other package responsible for fetching data from some pages,</li></ul><p>You could think of it as <strong>Bridge / Connector</strong> logic but while it sounds the same there is a tiny difference in implementation here.</p><p>There is no Project to which we need to <strong>Connect</strong> or have <strong>Bridge</strong> to.</p><h3>How to</h3><p><strong><em>What is chunk?</em></strong><em> </em>In this case <strong>chunk</strong> is just another <strong><em>“Standalone Symfony Project”</em> </strong>with some extra configuration allowing to <strong>Plug-In</strong> the bundle to the main project.</p><p>To make chunk fully working it requires configuring it as a <strong>Bundle</strong>, and for that all You need are just 2 files placed in proper directories like on the screenshot below:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/765/1*qcs4xprXRzk1kXDYAUCh5Q.png" /><figcaption>Bundle file (<strong>LinguaBundle</strong>) &amp; Extension file (<strong><em>LinguaExtension</em></strong>)</figcaption></figure><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/b8e6e358ce9d29a4aeeed094da39fbd6/href">https://medium.com/media/b8e6e358ce9d29a4aeeed094da39fbd6/href</a></iframe><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/c90e1b0f6175bff9d8143a7da336925c/href">https://medium.com/media/c90e1b0f6175bff9d8143a7da336925c/href</a></iframe><p>There is probably much more to it, but this minimum configuration is all it takes to make the Bundles work.</p><ul><li>Full details are available here: <a href="https://symfony.com/doc/current/bundles/extension.html">https://symfony.com/doc/current/bundles/extension.html</a></li></ul><p><strong><em>Installing chunk (bundle) with composer.</em></strong><em> </em>You can either install Your package like You would normally do it via composer (by for example adding it to Your private Gitlab instance) or You can install the package locally.</p><p>With local installation, <strong>Composer</strong> actually creates link, so You can still work with Your bundle project and it will be automatically updated in the vendor folder of the project in which You installed You package.</p><blockquote><em>How to install package locally?</em></blockquote><ul><li>Update the composer.json file of the project that will have the bundle installed by providing this section in the root of the mentioned file:</li></ul><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/39b9a679a909f6c7d77dda5817d5e50d/href">https://medium.com/media/39b9a679a909f6c7d77dda5817d5e50d/href</a></iframe><ul><li>Set the name in the composer.json of the <strong>bundle</strong> (this name will be used to install the package), for example: &quot;bundle&quot;: &quot;volmarg/lingua-bundle&quot; ,</li><li>Install the package like You normally would by just calling: composer require volmarg/lingua-bundle @dev</li></ul><p><strong><em>Activating Bundle</em>. </strong>That’s actually just one additional line in the bundles.phpfile. Following the above <strong>LinguaBundle </strong>example, activating (adding) it’s logic is equal to:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/940/1*9bxdokIddA5ZDa2Ni1V8sw.png" /><figcaption>bundles.php</figcaption></figure><p>With this it’s now possible to call the <strong>Controllers &amp; Services</strong> in the project, otherwise <strong>Symfony</strong> will throw Exceptions such as <em>“No service named </em><em>&lt;ServiceName&gt;was found”</em>. Provided example bundle configuration allows <strong>Symfony </strong>to load the additional services.yamlfile.</p><p><strong>Issues</strong></p><p>The only issue that I’ve found so far is that migrations are not working as they should — meaning that if a bundle has a migrations included then these won’t work in the project which has the bundle installed.</p><ul><li>This however can be fixed by following this guide: <a href="https://www.goetas.com/blog/multi-namespace-migrations-with-doctrinemigrations-30/#symfony-integration">https://www.goetas.com/blog/multi-namespace-migrations-with-doctrinemigrations-30/#symfony-integration</a></li></ul><p><strong>Simply explaining</strong></p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1015/1*qPCUpCk43JAe05kcRTNuxA.png" /><figcaption>doctrine_migrations.yaml</figcaption></figure><p>Also keep in mind that the <strong>key</strong> must be equal to the namespace of the migration file in bundle. So instead of using default namespace DoctrineMigrations it should become LinguaBundleDoctrineMigrations .</p><h3>Summary</h3><p>For me personally the biggest benefit is that I can work on smaller parts of the project while using the older Laptop.</p><p>Also what I found nice is that I’m actually now <strong>re-using</strong> some of the bundles in another bundles — I often reach a point when I’m like <em>“I would like to have that logic from bundle </em><strong><em>A</em></strong><em>”</em>, and It turns out that I’ve already got that logic ready to be re-used.</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=c297b5a55dab" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Vue3 (3.2.x) — Vue variables in CSS (style section)]]></title>
            <link>https://medium.com/@Volmarg/vue3-3-2-x-vue-variables-in-css-style-section-2803047bf7f9?source=rss-28a4f8831724------2</link>
            <guid isPermaLink="false">https://medium.com/p/2803047bf7f9</guid>
            <category><![CDATA[web-development]]></category>
            <category><![CDATA[vuejs]]></category>
            <category><![CDATA[vue]]></category>
            <category><![CDATA[frontend]]></category>
            <category><![CDATA[development]]></category>
            <dc:creator><![CDATA[Dariusz Włodarczyk]]></dc:creator>
            <pubDate>Sun, 07 Nov 2021 19:40:40 GMT</pubDate>
            <atom:updated>2021-11-09T03:19:40.982Z</atom:updated>
            <content:encoded><![CDATA[<h3>Vue3 (3.2.x) — Vue variables in CSS (style section)</h3><figure><img alt="" src="https://cdn-images-1.medium.com/max/730/0*Im15Sv4W-QFcM1jA.png" /></figure><h3>Use case</h3><p>Recently I’ve stumbled upon a problem where I’ve implemented some simple <strong>CSS</strong> based <strong>tabs</strong>. The best in it was that it <em>“was already there”</em>, <em>“was free”</em> and <em>“worked almost out of box”.</em> Once I was done with base case (just 2 tabs), I’ve started adding some more of them, and… things didn’t quite worked as expected.</p><p>The problem was that the <strong>SCSS</strong> code had hard-coded variable $tab-count with which all the widths, heights etc. were calculated and clickable tabs were showing &amp; hiding content.</p><p>I didn’t wanted to have hard-coded value but rather a dynamic one, based on how many tabs are passed via prop to the Tabs component.</p><blockquote>In the end, what I write about didn’t solved my problem but got learned about the new functionality, and case in which it won’t work.</blockquote><h3>`v-bind` in styles section</h3><p>Vue <strong>3.2.x </strong>allows to use v-bind directly in the &lt;style&gt; section.</p><p>Here is the simple example:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/37a73edaf1308fdc6907addb3f2a0d97/href">https://medium.com/media/37a73edaf1308fdc6907addb3f2a0d97/href</a></iframe><p>That above is literally everything that there is to be done to make it works. This also works with <strong>SCSS</strong> (alongside with vars $varName: v-bind() ).</p><h3>Issue: v-bind returns string</h3><p>In most cases v-bind returning string is just fine. This will probably cover 90% of cases. But it didn’t covered mine, as I needed to do loop in <strong>SCSS </strong>like that:</p><ul><li>$tabCount: v-bind(tabsCount)</li><li>@for $i from 1 through $tabCount</li></ul><p>This causes unavoidable error, which in my use case makes it impossible to use v-bind in style section.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/968/1*_9dCP8Xp-WZV-gWsu_sHFQ.png" /></figure><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=2803047bf7f9" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Vue.js & Symfony — roles authentication]]></title>
            <link>https://medium.com/@Volmarg/vue-js-symfony-roles-authentication-f9c5ea00d750?source=rss-28a4f8831724------2</link>
            <guid isPermaLink="false">https://medium.com/p/f9c5ea00d750</guid>
            <category><![CDATA[vue]]></category>
            <category><![CDATA[symfony]]></category>
            <category><![CDATA[security]]></category>
            <category><![CDATA[vuejs]]></category>
            <category><![CDATA[authentication]]></category>
            <dc:creator><![CDATA[Dariusz Włodarczyk]]></dc:creator>
            <pubDate>Sun, 19 Sep 2021 08:57:31 GMT</pubDate>
            <atom:updated>2021-09-20T12:24:16.803Z</atom:updated>
            <content:encoded><![CDATA[<h3>Vue.js &amp; Symfony — roles authentication</h3><p>Authenticating user roles and permissions with <strong>Vue.js</strong> &amp; <strong>Symfony</strong> based project</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/686/1*Dedp0UIE0_rny_5ICQeLRQ.png" /></figure><h3><strong>Use case</strong></h3><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*p-cbQg5C7_zIxvpNw8D05A.png" /></figure><p>All the Roles based checks (on front) are relying on the data in the JWT token. Once the JWT token is received it’s being stored in the Browser Storage.</p><p>Extracted payload of the JWT looks like this:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/694/1*Z3-xCe83HtAyRFJZotbO1w.png" /><figcaption>JWT payload</figcaption></figure><h3><strong>Setting roles requirements on Vue.js routes</strong></h3><p><strong>Vue.js</strong> route object has special property called meta, which allows to set any kind of additional data that is required. Following that I’m just setting meta property requiredRole like this:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/fc8fcfb36de76d23850b0b2e55001bd0/href">https://medium.com/media/fc8fcfb36de76d23850b0b2e55001bd0/href</a></iframe><h3><strong>Setting Vue.js guards for roles checks</strong></h3><p>Now additionally this solution requires configuring router object (which is part of <strong>vue-router </strong>package) just like this:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/6848791c2c7e0b75f08e016ea0831131/href">https://medium.com/media/6848791c2c7e0b75f08e016ea0831131/href</a></iframe><h3><strong>Bonus — role based component</strong></h3><p>Following all the roles checks logic, I’ve also added small role based component to wrap sub-components into permission validations. With this I can control what user sees also based on the role he has (for example debug toolbox which works regardless of the environment).</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/551146777d7f1bd599c23df1917e5a37/href">https://medium.com/media/551146777d7f1bd599c23df1917e5a37/href</a></iframe><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=f9c5ea00d750" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Emitting Vue.js events in Typescript]]></title>
            <link>https://medium.com/@Volmarg/emitting-vue-js-events-in-typescript-d99d5ec9d15f?source=rss-28a4f8831724------2</link>
            <guid isPermaLink="false">https://medium.com/p/d99d5ec9d15f</guid>
            <category><![CDATA[typescript]]></category>
            <category><![CDATA[web-development]]></category>
            <category><![CDATA[javascript]]></category>
            <category><![CDATA[vue]]></category>
            <category><![CDATA[vuejs]]></category>
            <dc:creator><![CDATA[Dariusz Włodarczyk]]></dc:creator>
            <pubDate>Sat, 31 Jul 2021 09:23:43 GMT</pubDate>
            <atom:updated>2021-08-01T00:57:22.328Z</atom:updated>
            <content:encoded><![CDATA[<p>How to emit an event in <strong>Typescript</strong> and listen for it in <strong>Vue.js</strong></p><figure><img alt="" src="https://cdn-images-1.medium.com/max/783/0*4lOdkNgq2y6utzbr.png" /></figure><p><strong>Vue.js</strong> allows users to emit and event from a child component and react to it in parent component via this.$emit(&#39;event-name&#39;) . While this works like a charm when being used directly in components i does create an issue when working with <strong>TypeScript</strong>.</p><h4><strong>Understanding the issue</strong></h4><p>Let’s assume (based on my private code) that</p><ul><li>there is an UserController.ts class (<em>on front</em>), which then has a method invalidateTokenAndRedirectUser</li><li>VueRouterGuard.ts class which handles the onRouteChange logic and this is where the invalidation happens,</li></ul><p>Now — user won’t do anything for like 1 hour and eventually the token will be invalidated. The VueRouterGuard checks in place (<em>before going to next route</em>) if the the token has expired.</p><p>Now if the user get’s invalidated I want to:</p><ul><li>move user to login page,</li><li>hide the logout menu element — and this is the problem as the nav is controlled by <strong>Vue.js</strong> and DOM should <strong>NOT</strong> be manipulated directly for that purpose,</li></ul><p>With this solution where VueRouterGuard invalidates the user, none of the <strong>Vue.js </strong>components / Views ever receives information that something happened.</p><h4><strong>Emitting event from Typescript</strong></h4><p>By checking the source code of <strong>Vue.js </strong>to better understand how the events emitting works I’ve found this:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/593/1*jTf1euE6RTdKBsHc958cVw.png" /></figure><p>And this looks just like the standard Event from <strong>Js</strong>.</p><p>So I’ve personally came up with this solution:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/71906775318e945767edb5bb84f962b0/href">https://medium.com/media/71906775318e945767edb5bb84f962b0/href</a></iframe><p>The emitEventOnRouterView method will take a RouterView DOM element and emit and event on it. And with that, provided event can be listened just like standard $emit(&#39;name&#39;)</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/08ece65ffd5cd32614846a411e929105/href">https://medium.com/media/08ece65ffd5cd32614846a411e929105/href</a></iframe><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=d99d5ec9d15f" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[jQuery vs Vue.js]]></title>
            <link>https://medium.com/@Volmarg/jquery-vs-vue-js-aaa69744ebef?source=rss-28a4f8831724------2</link>
            <guid isPermaLink="false">https://medium.com/p/aaa69744ebef</guid>
            <category><![CDATA[jquery]]></category>
            <category><![CDATA[vue]]></category>
            <category><![CDATA[javascript]]></category>
            <category><![CDATA[web-development]]></category>
            <category><![CDATA[vuejs]]></category>
            <dc:creator><![CDATA[Dariusz Włodarczyk]]></dc:creator>
            <pubDate>Tue, 01 Jun 2021 14:33:12 GMT</pubDate>
            <atom:updated>2021-06-01T23:12:34.710Z</atom:updated>
            <content:encoded><![CDATA[<p><em>Perspective from years of using </em><strong><em>jQuery </em></strong><em>and switching to </em><strong><em>Vue.js</em></strong></p><figure><img alt="" src="https://cdn-images-1.medium.com/max/571/0*QgsyJwfJhWh3adf5.png" /></figure><p>Why using <strong>jQuery</strong>? Why bothering? Isn’t it dead? Isn’t it way to messy? Is it still being used in nowadays projects? Is it still…</p><p>and so on and on…</p><p><strong>jQuery</strong> is still alive and as far as I know it’s doing fine:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/896/1*e0j38IPz9JHPGOt4-5Odcw.png" /><figcaption>Stack Overflow Developer Survey 2020</figcaption></figure><p>Latest version of <strong>jQuery</strong> is 3.6.0 and was actually released in current year (2021).</p><p>There are still tones of companies looking for developers with <strong>jQuery </strong>among the required tech stack. People are still building plugins for it, use them in commercial / private projects (so did I). It’s been there with for years and most likely for years it will remain.</p><p><strong>Modern JS</strong></p><p>When we talk about modern JS, we mostly (at least I and many people with which I had the opportunity to talk/chat) mean: <strong>React / Angular / Vue.</strong></p><p>I already had a chance to play with all of them, and by far <strong>Vue.js</strong> is a win — but this is not the main topic here.</p><p>At one point of using <strong>Vue.js</strong> I even thought:</p><blockquote><strong>“Vue.js</strong> is … terrifying…”</blockquote><p>There is nothing to fear about, it’s just that… compared to <strong>jQuery</strong> it’s so much better in many fields, and this is … what makes it terryfing! What <strong>Vue.js</strong> offers for example is:</p><ul><li>less code to write,</li><li>cleaner code,</li><li>modularity,</li><li><strong>TS</strong> support,</li><li>reactivity,</li><li>super easy to learn (from perspective of <strong>jQuery</strong> / <strong>TS</strong> person),</li><li>growing popularity,</li><li>simplicity of usage,</li></ul><p>Don’t get it wrong — this is not perfect tool, there are bugs which are constantly being fixed.</p><p>Another cool example is that I had my <em>“todo”</em> list of what I want to implement in my project — I’ve finished it so fast that I tought:</p><blockquote>“I must’ve forgotten something”</blockquote><p>I didn’t — coding with <strong>Vue.js</strong> goes really smooth.</p><p><strong>Long story short — moving forward</strong></p><p>I’ve been using <strong>jQuery</strong> for years now and I had this feeling that it’s time to grab something modern (not just for future self, but also out of curiosity), besides <strong>Vue.js</strong> allowed/allows me to learn modern vanilla JS solutions.</p><p><strong>How hard is it to learn Vue.js?</strong></p><figure><img alt="" src="https://cdn-images-1.medium.com/max/697/1*UlbmaA1OWbLaJfgdzgjFag.png" /></figure><p>Like always <em>“It depends”</em> — I will present that on my case:</p><ul><li>couple years with <strong>Vanilla JS</strong> and <strong>jQuery,</strong></li><li>1y + with <strong>Typescript,</strong></li><li>one project in <strong>React </strong>(messy as hell, written few years ago),</li><li>one big update in <strong>Angular </strong>(4+?),</li></ul><p>It took me…. <strong><em>2 weeks</em></strong> to learn Vue.js to the point where I feel confident enough to work with it.</p><p>However… these <strong><em>2 weeks </em></strong>were not like <strong><em>24/7 </em></strong>in tutorials, rather I was reading the manuals during the Christmas<em> (while other stared on the TV i followed the official </em><strong><em>Vue.js</em></strong><em> manuals).</em></p><p>In my opinion, <strong>Vue.js</strong> is super easy to learn — official documentation is well written, consist of good examples, use cases and explanation. All that is left afterwards is just writing Your own code.</p><p><strong>React </strong>and <strong>Angular </strong>are way to big, way to complicated — compared to both of them <strong>Vue.js</strong> is a little/compacted grain of salt — but works the way it should.</p><p><strong>But what abut Vue CLI, Vuex etc…</strong></p><p>You don’t need this to start using <strong>Vue.js</strong> — the really necessary packages that I use are:</p><ul><li><strong>vue-router</strong>,</li><li><strong>i18n </strong>(however I use my own implementation which I’ve described <a href="https://medium.com/@Volmarg/translations-in-vue-js-with-ts-and-symfony-e91b3382acb1">here</a>)</li></ul><p><strong>Can You safelly use Vue.js alongside with jQuery?</strong></p><p>The answer is — up to some degree.</p><p>The problem is that <strong>jQuery </strong>or <strong>Vanilla Js</strong> update / insert / remove the elements (their values) by manipulating DOM directly — Vue.js doesn’t like that, and it won’t be informed that something was changed by other library.</p><p><strong>Vue.js</strong> is meant to update the <strong>DOM </strong>by manipulating the delivered data (that is — changing the state of variables automatically reflects in changes of <strong>DOM</strong>)</p><p><strong>Turning world upside down — separation of logic vs <em>Single File Component</em></strong></p><figure><img alt="" src="https://cdn-images-1.medium.com/max/614/1*qK6h9TYnqmg5CqXJnQSgqg.png" /></figure><p>The biggest issue in switching to <strong>Vue.js</strong> is changing the way of thinking of the code, it’s structure — the way it should be written.</p><p>In my case I moved to <em>Single File Components</em> — this was one big <strong>no(!)</strong> for me as all the time I’ve been separating js / html / css — this is no longer the way to go in terms of <em>Single File Components</em>.</p><p>In the old school solution we would go this way:</p><ul><li><em>styles.scss</em></li><li><em>script.js</em></li><li><em>index.html</em></li></ul><p>While in <strong>Vue.js</strong> we have simply:</p><ul><li><em>component.vue</em> — which consist of template (html), script (js) and style (css) sections</li></ul><p><em>That is — I got one file with my solution. Nobody says that You can’t separate the logic — but I wanted to follow the </em><strong><em>Vue.js</em></strong><em> coding standards from scratch and to learn modern js.</em></p><p><strong>Less code — make it easier to read and maintain</strong></p><p>One of the things worth mentioning is that, we are no longer relying on the let $element = $(element) or $element.on(&#39;click&#39;, () =&gt; {}) , instead we have $refs and @ event.</p><p>Seems like nothing big, but this already makes code clean. Consider Your template code with something like this (attaching an event):</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/660ed2b0e9bd948d141150bce2968994/href">https://medium.com/media/660ed2b0e9bd948d141150bce2968994/href</a></iframe><p>In <strong>jQuery</strong> this depends on place where we call it, while in <strong>Vue.js </strong>it’s almost always referencing to the root so there is no need to create copy of this to use other context in on — this could be solved somewhat with bind but I could never get along with this concept.</p><p>So if we transform this to vue, we get something like this:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/9a811c95ece72ce33d81f1b1e7dfd1af/href">https://medium.com/media/9a811c95ece72ce33d81f1b1e7dfd1af/href</a></iframe><p><strong>Removal of </strong><strong>$(selector) , reducing need to update DOM</strong></p><p>In Vue.js there is no longer need to grab selectors this way, we can simply do this.$refs.divElement wherever we need that dom element</p><p>It doesn’t seem to be much but this already reduces the code, we get rid of small repetatable code in lot of places this way.</p><p>Additionally we no longer need to <em>manually update the values / DOM elements</em> whenever we do some change in other script — this is possible due to <strong>reactivity</strong> meaning that if we update a variable in one place… it’s all usages in other places are also being updated — this… reduces the code significantly.</p><p>From perspective of <strong>jQuery </strong>the process would look like this:</p><ul><li>set some value (hardcoded) in html (template),</li><li>update it in one place (grab the $(selector) )</li><li>then update it in all the other places, adding tones of $(selector).find.parent</li><li>maybe add the if dom element is not existing ,</li></ul><p>I think You get the point — You pretty much like duplicate tones of code.</p><p>While from perspective of <strong>Vue.js</strong> You pretty much do something like this:</p><ul><li>define initial value of variable — bind it to html (template),</li><li>update the variable, wherever You need by calling: this.variableName = &quot;something&quot;</li><li>eventually update child component from parent this.$refs.component.variableName = &quot;something&quot;,</li></ul><p><em>Simply saying, </em><strong><em>Vue.js</em></strong><em> handles updating all ocurences of given variable!</em></p><p><strong>Vue.js — cons (<em>compared to jQuery</em>)</strong></p><figure><img alt="" src="https://cdn-images-1.medium.com/max/452/1*F7OOr5zJysvNyjKsCaEkyw.png" /></figure><p>Vue.js is not perfect — this is something that needs to be understood here. Bugs happen and so there was a bug in <strong>3.0.0</strong> with which i struggled but was then fixed in <strong>3.0.2</strong> — but hey, that’s normal.</p><p>The only real <strong>cons </strong>I can write about - in terms of technical solutions / concept is <em>“partial </em><strong><em>TS</em></strong><em> support”</em>, meaning <em>“no Typehint support in Vue.js methods”</em> (this however works in CLI — never used that one, just saw some information here and there).</p><blockquote>The biggest downside is lak of well known <strong>jQ </strong>plugins for <strong>Vue</strong></blockquote><p>As an example — I really wanted to use <strong>datatables </strong>for <strong>Vue.js</strong> but I couldn’t find any decent working component with the same functionallity. This forced me to write my own lite version of that plugin — and so it shows how simple it actually is to recreate code in Vue.</p><p><strong>Few words of summary</strong></p><p>I could naturally write so much more about it, but nobody is simply going to read endless chunk of text.</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=aaa69744ebef" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Symfony / Doctrine — database encryption]]></title>
            <link>https://medium.com/@Volmarg/symfony-doctrine-database-encryption-50d7e01d8f69?source=rss-28a4f8831724------2</link>
            <guid isPermaLink="false">https://medium.com/p/50d7e01d8f69</guid>
            <category><![CDATA[database]]></category>
            <category><![CDATA[security]]></category>
            <category><![CDATA[php]]></category>
            <category><![CDATA[symfony]]></category>
            <category><![CDATA[encryption]]></category>
            <dc:creator><![CDATA[Dariusz Włodarczyk]]></dc:creator>
            <pubDate>Tue, 25 May 2021 09:43:51 GMT</pubDate>
            <atom:updated>2021-05-28T00:16:39.147Z</atom:updated>
            <content:encoded><![CDATA[<h3>Symfony / Doctrine — database encryption</h3><p><em>Encrypting database with </em><strong><em>Doctrine</em></strong><em> and </em><strong><em>Symfony</em></strong></p><figure><img alt="" src="https://cdn-images-1.medium.com/max/659/0*GY1vkml8VtV5UFVC.png" /></figure><p>As much as personal data should be kept private, there are some rare case where for some reason we must have access to them in public services. I have my own private project for managing personal data (with source code accessible <a href="https://github.com/Volmarg/personal-management-system">here</a>), but I’ve came to the situation where due to my incoming mobility I simply <strong>must</strong> have access to my personal information in other places.</p><p>There are few solutions for such problem naturally, like 2 came to my mind instantly:</p><ul><li>moving the files / information on laptop,</li><li>having access to the data over internet,</li></ul><p>I’ve picked up the 2nd option due to few cases:</p><ul><li>I don’t have to remember to update data on storage (crons will do that),</li><li>I won’t always have access to laptop etc (I can get access to data in phone),</li><li>I want to get better / more familiar with <strong>Vue.js </strong>(and this is an opportunity then),</li></ul><blockquote><em>“However the picked solution has one big downside which had / needs to be solved — possibility of data leakage”</em></blockquote><p><strong>Protecting personal data</strong></p><p>Honestly I’ve became a bit obsessed with protecting my data from potential leakage (<em>there is always a risk, but You can reduce it, bring closer to zero</em>).</p><p>With all of the implemented ideas of mine I came to the point of asking myself <em>“what if the database will leak?”.</em></p><p>Solution for this came almost instantly as I already rely on it partially in my private project: “<strong>Database encryption</strong>”.</p><p>While I’ve already seen few encryption packages for <strong>Symfony</strong> with <strong>Doctrine</strong>, I will only focus on the one that I keep using (there is actually small issue in it but it’s easy to solve):</p><ul><li>EncryptBundle (<a href="https://github.com/mogilvie/EncryptBundle">mogilvie</a> — <strong>the one I rely on</strong>),</li><li>DoctrineEncryptBundle (<a href="https://github.com/GiveMeAllYourCats/DoctrineEncryptBundle">GiveMeAllYourCats</a> — fork with some fixes),</li></ul><p>Both packages share same base functionality which is encrypting / decrypting database cells with special key, however the <strong>DoctrineEncryptBundle</strong> comes with CLI commands which allow to handle whole database at once — which is a rely nice feature.</p><p>I moved on with <strong>EncryptBundle</strong> as I already use, and I have encryption key generated for it thus I will be able to share one key between internal / external project.</p><p><strong>How things work</strong></p><p>Using the <strong>EncryptBundle </strong>is very easy and intuitive. First of all naturally the package must be added to the project via composer, and afterwards we need to generate our encryption key (like described in original Readme “<a href="https://github.com/mogilvie/EncryptBundle#step-2-configure-the-bundle">here</a>”).</p><p>Our service Yaml will contain now this new section:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/637d8cad6125b562397bc92e10d98f6f/href">https://medium.com/media/637d8cad6125b562397bc92e10d98f6f/href</a></iframe><p>Further we got to add special annotation to the entity columns like this:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/73544a61b08bde9f9f6595cf82219161/href">https://medium.com/media/73544a61b08bde9f9f6595cf82219161/href</a></iframe><p>The bundle supports the doctrine events so this is generally all — nothing more is needed.</p><blockquote>“Can the encryption be handled without annotation?”</blockquote><p>Yes. I didn’t knew that the bundle handles doctrine events thus I’ve made my own method based on the bundle to handle data encryption manually.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/3088f844e569aa3e0a75325f6a03137f/href">https://medium.com/media/3088f844e569aa3e0a75325f6a03137f/href</a></iframe><blockquote><em>How the encrypted data looks like with incorrect key provided?</em></blockquote><p>It’s just a empty string which is actually cool, as in my case I don’t care if exception is thrown or wrong data is displayed — the goals has been achieved — data is secured.</p><p>I’ve had to debug the package at one point so I can say it relies on PHP named <strong>OpenSSL</strong>:</p><ul><li><a href="https://www.php.net/manual/en/book.openssl.php">https://www.php.net/manual/en/book.openssl.php</a></li></ul><p><strong>Even more security</strong></p><p>Since I want my data be accessible over the Internet I had to find a way <em>“not to store the key on server” — </em>this would basically look like this:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/480/0*mPP__FDeiFzGo2yX" /></figure><p>Now this is the issue that I’ve described on the beginning — the package that I use does not allow to add encryption key on-fly as exception is being throw in yaml parameter is missing — thus at the moment I’ve got it hardcoded but seems like the solutions is on it’s way (prepared by author himself)— even if not, it can be implemented manually. There is also small problem with non booted Kernel (but I’ve just missed booting check).</p><p>Instead of putting key under mat I’ve just expanded my login form:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/613/1*6Cy1NXeY2zPgIsPvYmEveA.png" /><figcaption>Key field for encryption key</figcaption></figure><p>My overall solution here is:</p><ul><li>creating <strong>OnBeforeSetKey,</strong></li><li>creating <strong>EncryptionKey</strong> file,</li><li>reading the file with each request and setting the key on-fly (replacing the placeholder 000000000... ),</li><li>having a cronjobs which check last user activity and removes the key from server if user is no longer there (this could be even more secure by storing the key in session instead of file, but I also remove the file when user logs-out so I will stick to my method as with all that IP filtering etc is just fine).</li></ul><p>Works like a charm — is not optimized (saying that file is being opened each time), but I don’t need it for my tiny sub-project.</p><p>Now the cells in database looks like this:</p><pre>3DDOXwqZAEEDPJDK8/LI4wDsftqaNCN2kkyt8+QWr8E=&lt;ENC&gt;</pre><p><strong>Personal notes</strong></p><p>As in my case I didn’t noticed any performance decrees it certainly hits performance in case of fetching / handling large amount of data — it simply must — no idea how much it hits but as we now need to handle encryption each time it might have visible impact.</p><p>Even though I’m not to familiar with encryption, logically saying — it most likely depends on used algorithm.</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=50d7e01d8f69" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Vue.js and Symfony — nested menu]]></title>
            <link>https://medium.com/@Volmarg/vue-js-and-symfony-nested-menu-5d593185ea1?source=rss-28a4f8831724------2</link>
            <guid isPermaLink="false">https://medium.com/p/5d593185ea1</guid>
            <category><![CDATA[vue]]></category>
            <category><![CDATA[vuejs]]></category>
            <category><![CDATA[symfony]]></category>
            <category><![CDATA[php]]></category>
            <category><![CDATA[web-development]]></category>
            <dc:creator><![CDATA[Dariusz Włodarczyk]]></dc:creator>
            <pubDate>Sun, 16 May 2021 07:01:59 GMT</pubDate>
            <atom:updated>2021-05-16T07:06:11.989Z</atom:updated>
            <content:encoded><![CDATA[<h3>Vue.js and Symfony — nested menu</h3><p>Based on hierarchical structure of categories (parent / child relations)</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/0*xSX0JJJx4vJ_ULYE.jpg" /></figure><p>There are tones o ways to build nested menu structure, where one are simply hardcoded arrays and other rely on… <strong>recursion </strong>— and that’s the solution for current <em>“code case”</em>.</p><blockquote><em>“Can You actually use recursion in Vue.js</em><strong><em> </em></strong><em>template?”</em></blockquote><p>The thing is… that You actually can and it’s pretty simple to do it.</p><p>I’m simply converting this database structure:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/504/1*bbkDMlc49QGyFKfZNp2JSQ.png" /><figcaption>Database structure</figcaption></figure><p>Into parent / child relational array:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/545/1*p9s6wsImyfRN2SzY2zDbsQ.png" /><figcaption>Parent child hierarchy array obtained from backend</figcaption></figure><p>And then output the menu</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/286/1*EqH4a1_3MQXRFbHeFivgGQ.png" /><figcaption>Rendered menu in Vue.js</figcaption></figure><p>In this solution, not only the deepest child is a link but also the parents themselves, which mean that in the structure presented above each element is a router link:</p><ul><li>clicking on Finances will load category data,</li><li>clicking on Goals will load category data,</li><li>clicking on arrow will show / hide nesting,</li></ul><p>Keep in mind that in this case I’m using:</p><ul><li><strong>bootstrap: 5.0.0-beta2</strong>,</li><li><strong>vue: 3.0.5,</strong></li><li><strong>php8.0,</strong></li><li><strong>Symfony 5.x</strong></li></ul><p><strong>Backend</strong></p><p>What we need here is a logic which will convert the database structure into the presented array structure above which will then be handled on the frontend.</p><p><strong>Database structure into array of depths</strong></p><p>First of all I simply fetch all data from database and output an array where <strong>categoryId is a key and value is depth level.</strong></p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/fbfb67e4580986044b3e34203b701d64/href">https://medium.com/media/fbfb67e4580986044b3e34203b701d64/href</a></iframe><p><strong>Array of depths into parent / child array</strong></p><p>As next we use the previously fetched id / depth to build relation array. CategoryId is being used to fetch category data, which is then passed to the recursive call of buildParentChildDtoForHierarchy (as each category can contain children and these children can consist of children which children… <em>“You get the point”</em>).</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/a274248dd4989ea895536c04897f4c98/href">https://medium.com/media/a274248dd4989ea895536c04897f4c98/href</a></iframe><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/adaa4c2f605f82f04fa3cf225a8a5098/href">https://medium.com/media/adaa4c2f605f82f04fa3cf225a8a5098/href</a></iframe><p>Now the last polishing of data structure and this can be sent to the frontend.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/e15d77efcb7b59f319385e8a8b888b7e/href">https://medium.com/media/e15d77efcb7b59f319385e8a8b888b7e/href</a></iframe><p><strong>Frontend</strong></p><p>Frontend handling get’s way much easier in here, as the only things we need to do is:</p><ul><li>fetch data from backend,</li><li>converting json response into parentChildDtoArray,</li><li>pass the array to the component,</li></ul><p>Keep in mind:</p><ul><li>router-link is simply <a href="https://router.vuejs.org/"><strong>Vue Router</strong></a><strong>,</strong></li></ul><p>So first thing first — fetching the data from backend with <a href="https://www.npmjs.com/package/axios"><strong>Axios</strong></a><strong>:</strong></p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/fa158614d14a4f66336a3898b7bdeceb/href">https://medium.com/media/fa158614d14a4f66336a3898b7bdeceb/href</a></iframe><p>We will also need a component nested-menu-node.vue .</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/d88f08c30cb7375d6726f6f70c8967a8/href">https://medium.com/media/d88f08c30cb7375d6726f6f70c8967a8/href</a></iframe><p>And that’s it, now call the component in Your template, like this:</p><pre>&lt;template <em>#submenu</em>&gt;<br>  &lt;nested-menu-node <em>:nodes</em>=&quot;parentChildDtoArray&quot;<br>                    <em>:node-identifier</em>=&quot;notesCategoriesMenuNodeId&quot;<br>                    <em>:to-path-name</em>=&quot;routeNameModuleNotesCategory&quot;<br>                    <em>:to-id-param-name</em>=&quot;routeNameModuleNotesCategoryIdParam&quot;<br>  /&gt;<br>&lt;/template&gt;</pre><blockquote>“But… where is the recursion actually happening in <strong>Vue</strong> template?”</blockquote><p>In nested-menu-node.vue component the component itself gets imported:</p><pre>&lt;script <em>type</em>=&quot;ts&quot;&gt;<br><em>import </em>NestedMenuNode <em>from </em>&quot;./nested-menu-node&quot;;<br>&lt;/script&gt;</pre><p>Which is then being used like that:</p><pre>&lt;li <em>v-for</em>=&quot;node <em>in </em>nodes&quot;<br>    <em>class</em>=&quot;sidebar-item&quot;<br>&gt;<br>...<br>  &lt;nested-menu-node <em>:nodes</em>=&quot;node.children&quot;<br>                    <em>:node-identifier</em>=&quot;node.id&quot;<br>                    <em>:to-path-name</em>=&quot;toPathName&quot;<br>                    <em>:to-id-param-name</em>=&quot;toIdParamName&quot;<br>  &gt;&lt;/nested-menu-node&gt;<br>&lt;/li&gt;</pre><h3><strong>ParentChildDto used in the process</strong></h3><p><strong>Frontend</strong></p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/ff8700923d589b16d3e38fde7221d61e/href">https://medium.com/media/ff8700923d589b16d3e38fde7221d61e/href</a></iframe><p><strong>Backend</strong></p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/f4dbb9ff56911c1d898164a7c80508e6/href">https://medium.com/media/f4dbb9ff56911c1d898164a7c80508e6/href</a></iframe><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=5d593185ea1" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Vue.js and Symfony forms]]></title>
            <link>https://medium.com/@Volmarg/vue-js-and-symfony-forms-ba5139f77f5f?source=rss-28a4f8831724------2</link>
            <guid isPermaLink="false">https://medium.com/p/ba5139f77f5f</guid>
            <category><![CDATA[javascript]]></category>
            <category><![CDATA[vuejs]]></category>
            <category><![CDATA[symfony]]></category>
            <category><![CDATA[php]]></category>
            <category><![CDATA[web-development]]></category>
            <dc:creator><![CDATA[Dariusz Włodarczyk]]></dc:creator>
            <pubDate>Sun, 09 May 2021 11:05:47 GMT</pubDate>
            <atom:updated>2021-05-26T00:37:25.135Z</atom:updated>
            <content:encoded><![CDATA[<p>How to handle <strong>Vue.js</strong> request within <strong>Symfony</strong> as normal form submission</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/0*unD28396N31q-UBk.png" /></figure><p>Symfony provides us the symfony/form package which makes handling of rendering and submitting forms easier than ever. Soon this will become even easier with new “<a href="https://symfony.com/blog/new-in-symfony-5-3-form-handler-helper">Form handler helper</a>”, but that’s not the point here.</p><p>What annoyed me when I first started working with <strong>Vue.js </strong>was the fact that I pretty much like need to say “<em>goodbye</em>” to the forms rendering directly in the <strong>Twig</strong> (<em>which is very handy</em>).</p><p>I’ve started searching for some “<em>form generators</em>” packages for <strong>Vue.js</strong>. There are few, but so far I didn’t liked any of them. In worst case, I could just render the form in some kind of API method like /get-form-view but this is so <strong>against</strong> the <strong>Vue.js </strong>workflow/concept.</p><p>So... I started to ask myself and think:</p><p>“<em>How can I render forms in </em><strong><em>Vue.js </em></strong><em>and handle them in </em><strong><em>Symfony</em></strong><em>? While I can quit rendering the forms like Twig handles it — I definitely want to keep the Form validations / handling provided by </em><strong><em>Symfony</em></strong>”</p><p>There are few problems that need to be solved here:</p><ul><li>handling of <strong>csrf</strong> token,</li><li>rebuilding the <strong>request <em>“</em></strong><em>it’s structure must be the same of that provided upon</em><strong><em> Symfony </em></strong><em>standard form submission</em><strong><em>”,</em></strong></li></ul><p><strong>CSRF token</strong></p><h3><strong>Backend</strong></h3><p>By default each Form created with symfony/form packages has the CSRF Token validation turned on. Meaning further that each request provided to the form handler <strong>must</strong> contain the CSRF Token. There are few solutions here, I will go with the 2 that I found for my case.</p><p><strong>The bad one (unless it’s just Your private project) — <em>disabling the CSR Token validation</em></strong></p><p>Each form class (meaning that it extends from Symfony\Component\Form\AbstractType ) allows us to turn the CSRF validation off.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/a0f892079e0522d78f2400b3fb106396/href">https://medium.com/media/a0f892079e0522d78f2400b3fb106396/href</a></iframe><p>While this is ok for small private projects or internal ones, it’s one big <strong>no</strong> when it comes to publicly accessible project.</p><p><strong>Sending and handling the csrf token with Vue.js call</strong> — <strong>the good solution</strong></p><ol><li>First thing to do is… disabling the csrf_protection like above.</li></ol><p>2. Now we need CsrfTokenValidatorService</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/8ff6cbf4545fdcf891bf3ebb2369610e/href">https://medium.com/media/8ff6cbf4545fdcf891bf3ebb2369610e/href</a></iframe><p>3. Now the check for token must be done in 2 places</p><ul><li>as I wrote in other article (<a href="https://medium.com/@Volmarg/vue-js-and-symfony-user-authentication-19ca983126d"><strong>here</strong></a>), each request goes through Authenticator — so the request must be validate inside of it as this is called <strong>before</strong> the KernelRequestEvent gets triggered</li><li>KernelRequest::REQUEST</li></ul><p><strong>KernelRequestListener</strong></p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/51c4a12dfe542b30db0fafe12e3b8f84/href">https://medium.com/media/51c4a12dfe542b30db0fafe12e3b8f84/href</a></iframe><p><strong>Authenticator</strong></p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/b79c5458445c9ac146642bb730cf1dab/href">https://medium.com/media/b79c5458445c9ac146642bb730cf1dab/href</a></iframe><p>4. We also need route which will deliver the CSRF token</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/a4ca56707307c97b5f66efd78be1458b/href">https://medium.com/media/a4ca56707307c97b5f66efd78be1458b/href</a></iframe><h3>Frontend</h3><p>Now instead of adding getCsrf over and over again in each <strong>Axios</strong> call i wrote my own plugin to handle the post call with csrf token call alongside.</p><p><strong>The plugin</strong></p><p>Simply the plugin does 2 things:</p><ul><li>calls backend: “<em>Hey — give me CSRF token- for this </em><strong><em>UUID</em></strong><em>”</em>,</li><li>then makes second call “<em>Hey — i got this CSRF for UUID, please check it and let me through”</em></li></ul><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/1fb4168bf839045d88f87fa9f22f874a/href">https://medium.com/media/1fb4168bf839045d88f87fa9f22f874a/href</a></iframe><p>Naturally nobody says that the token must be fetched on the moment that ajax call is done, it can as well be fetched in the beforeMount and then supplied in form, I just see no reason for that, as we must communicate backed for token anyway.</p><p>So.. does it actually work correctly if we would provide fake csrf token? <strong>Yes</strong> — i did a test with hardcoded token in code and the request was <strong>rejected</strong>.</p><p><strong>Not a perfect solution</strong></p><p>While this is doesn’t sound perfect it works pretty much the way that Symfony handles the CSRF tokens:</p><ul><li>it uses the form_name instead of UUID,</li><li>it generates new token on each request,</li></ul><p><strong>Rebuilding the Request</strong></p><p>Now once the CSRF token validation is working, here comes the easiest part actually. We simply need a logic which will <em>“take the </em><strong><em>Axios</em></strong><em> request — json” </em>and transform it into proper Request data used normally in <strong>Symfony</strong>.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/4c059df767f44c74391469ebadc83487/href">https://medium.com/media/4c059df767f44c74391469ebadc83487/href</a></iframe><p>And just use it like this:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/7133cbe7d92cbf603213adfe1e9f2ba0/href">https://medium.com/media/7133cbe7d92cbf603213adfe1e9f2ba0/href</a></iframe><p><strong>My thoughts to this solution</strong></p><p>The only main concern that I got here is that getCsrf route should be hidden behind authentication, and only the login should be allowed to pass it without authentication, otherwise upon “<em>fake form</em>” and copied front logic anyone can easily fetch the CSRF token.</p><p>Other nice “in-case” protection would be just checking where does the request comes from — if outside of our domain then — block it, thus nobody will ever get the token.</p><p>Other than this, is just that it took a bit to understand how to generate the CSRF token, validate it, handle the request and so on — so it’s time consuming when doing it for first time, but other than that it can be copied and reapplied.</p><p>And hey… the <strong>Symfony Form Validation </strong>is working as it should!</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=ba5139f77f5f" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Vue.js and Symfony — User Authentication]]></title>
            <link>https://medium.com/@Volmarg/vue-js-and-symfony-user-authentication-19ca983126d?source=rss-28a4f8831724------2</link>
            <guid isPermaLink="false">https://medium.com/p/19ca983126d</guid>
            <category><![CDATA[programming]]></category>
            <category><![CDATA[vue]]></category>
            <category><![CDATA[php]]></category>
            <category><![CDATA[symfony]]></category>
            <category><![CDATA[vuejs]]></category>
            <dc:creator><![CDATA[Dariusz Włodarczyk]]></dc:creator>
            <pubDate>Sun, 02 May 2021 07:12:11 GMT</pubDate>
            <atom:updated>2021-06-08T00:44:36.744Z</atom:updated>
            <content:encoded><![CDATA[<h3>Vue.js and Symfony — User Authentication</h3><p><em>Authenticating users in </em><strong><em>Symfony</em></strong><em> — with </em><strong><em>Vue.js</em></strong><em> as frontend framework</em></p><figure><img alt="" src="https://cdn-images-1.medium.com/max/724/1*5yTswPY7EVA3b0OA_8lRxg.png" /></figure><p>In this article I will skip the Symfony authentication process as this can be found in the official documentation (it’s more about presenting the solutions in case of using <strong>Vue.js</strong>):</p><ul><li><a href="https://symfony.com/doc/current/components/security/authentication.html">Symfony Authentication</a>,</li><li><a href="https://symfony.com/doc/current/security/form_login_setup.html">Symfony Login Form</a>,</li><li><a href="https://symfony.com/doc/current/security/guard_authentication.html">Symfony Guard Authentication</a>,</li></ul><p><strong>JWT Token based authentication — does it have to be this way?</strong></p><p><strong>Vue.js</strong> allows us to either create a <strong>SPA</strong> (<strong>Single-Page Application</strong>) or to be used in form o hybrid where we can inject the components into already existing code or use <strong>Vue.js</strong> as an extension of current frontend code (<em>In which case I learned the hard way that it can become very messy — You can find more </em><a href="https://medium.com/@Volmarg/vue-js-and-twig-in-symfony-5c20aef5c57d"><em>here</em></a>) — as for both cases the authentication can implemented / solved in different ways.</p><p>Personally I didn’t even intended to test both solution, as the hybrid one works very well — but — I’ve already had a chance to see some interesting / confusing authentication in <strong>Symfony</strong> by using <strong>JWT</strong> to validate user on <strong>Angular</strong> based frontend (<em>in this case the solution / end goal was different than simple login form</em>).</p><p>In general the <strong>JWT</strong> solution does work, makes everything it should, there is no problem — so what’s the point? Since <strong>Vue.js</strong> is the first modern JS framework used on my own alongside with backend framework I wondered:</p><p><strong><em>“Do I really need to use some tokens, as the Symfony auth no longer work the way it should? Is it always like this in modern JS?”</em></strong></p><p>The long story short — it doesn’t need to be this way, there is actually no magical, special token required in the authentication process — though the full <strong>SPA</strong> does requires special logic but it’s still “<em>almost</em>” standard <strong>Symfony</strong> logic.</p><p><strong>The easiest solution — Hybrid authentication</strong></p><p>In this case the login from works is the only one page which is fully rendered by <strong>Twig</strong> — no js logic is used here, thus normal authentication process is being used.</p><ol><li><strong>Authentication Page</strong> — simple Twig based template with a login route</li></ol><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/f9535737e058bab878223ba3c68e51a1/href">https://medium.com/media/f9535737e058bab878223ba3c68e51a1/href</a></iframe><p><strong>2. Authentication Route </strong>— route to return the login page</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/c9a3f287c164cd45d5d728309c2011c9/href">https://medium.com/media/c9a3f287c164cd45d5d728309c2011c9/href</a></iframe><p>And that’s it ( naturally assuming that project is already configured to use Symfony Authentication presented in links above ), authentication works already on this step — simply add some rules to <strong>security.yaml</strong> and everything will work as intended.</p><p>Be aware that each route added with<strong> Vue-Router must </strong>be also present in Symfony, otherwise how does the backend know what page was accessed? After all <strong>Vue.js</strong> only pushes new state in browser history, unless simple location.pathname = url is used.</p><p><strong>Adding</strong> <strong>Vue.js routes to Symfony</strong></p><p>Personally I’ve got a very simple solution for this, which is defining one method (with base twig template for it — it contains the <strong>mounting #id </strong>for <strong>Vue-App</strong>):</p><ol><li><strong>Handler in backend</strong></li></ol><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/d94232ea3b4c8804b53198c97fd5bc12/href">https://medium.com/media/d94232ea3b4c8804b53198c97fd5bc12/href</a></iframe><p><strong>2. Twig with mountig #id</strong></p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/0f67f9b1ca2abb67b0a8fe940ae44449/href">https://medium.com/media/0f67f9b1ca2abb67b0a8fe940ae44449/href</a></iframe><p>With this — if we open the SPA page new tab it will still work as it’s just template, which later in process get’s rebuilt/controlled by <strong>Vue.js.</strong></p><p><strong>3. Mounting Vue.js</strong></p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/6f2595ed480f34a9c0cae34c9edeed4e/href">https://medium.com/media/6f2595ed480f34a9c0cae34c9edeed4e/href</a></iframe><p><strong>The tricky solution — API call Authentication</strong></p><p>In this solution everything is handled in <strong>Vue.js</strong> besides the usage of base <strong>Twig</strong> template (<em>just scroll up to </em><strong><em>“Adding Vue.js routes to symfony”</em></strong>).</p><ol><li><strong>Login Authenticator (<em>backend</em>)</strong></li></ol><p><em>So what’s so special in this authenticator?</em></p><p>The response — that’s the important thing, it’s a <strong>JsonResponse</strong> (BaseApiDTO) consumed in frontend for each call.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/4f77632025c5dff7ea543836936e8155/href">https://medium.com/media/4f77632025c5dff7ea543836936e8155/href</a></iframe><p>2.<strong> Handling call in Vue.js (<em>frontend</em>)</strong></p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/b41f3683d56fdfb3baf9e3770240d8ca/href">https://medium.com/media/b41f3683d56fdfb3baf9e3770240d8ca/href</a></iframe><p>3.<strong> The response</strong></p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*mlEk5WNDHJS2TXW0fJtWlw.png" /><figcaption>Unauthorized response</figcaption></figure><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*5Z52kr0XjBeDPpPERaSLgw.png" /><figcaption>Authorized response</figcaption></figure><p>4. <strong>Logout user</strong></p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/c9c7cc9604e59f1457d50cb114c8b979/href">https://medium.com/media/c9c7cc9604e59f1457d50cb114c8b979/href</a></iframe><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/9fca11e80e2e256d087f887d5db5f379/href">https://medium.com/media/9fca11e80e2e256d087f887d5db5f379/href</a></iframe><p><strong>Summarizing</strong></p><p>Both solutions work like a charm, but if I was to choose the faster one — then it’s definitely the <strong>Hybrid-one</strong>.</p><p>Do keep in mind — that for both solutions, it’s required to send the <strong>JsonResponse</strong> to front, so it’s a good idea to use some <strong>BaseResponse</strong> which will always consist of user authentication status.</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=19ca983126d" width="1" height="1" alt="">]]></content:encoded>
        </item>
    </channel>
</rss>