<?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 Ideneal on Medium]]></title>
        <description><![CDATA[Stories by Ideneal on Medium]]></description>
        <link>https://medium.com/@ideneal?source=rss-8d2b88f87b92------2</link>
        <image>
            <url>https://cdn-images-1.medium.com/fit/c/150/150/1*CveeEHjm0HuLuSrJzz0CXA.jpeg</url>
            <title>Stories by Ideneal on Medium</title>
            <link>https://medium.com/@ideneal?source=rss-8d2b88f87b92------2</link>
        </image>
        <generator>Medium</generator>
        <lastBuildDate>Mon, 30 Nov 2026 19:06:28 GMT</lastBuildDate>
        <atom:link href="https://medium.com/@ideneal/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[PHPUnit — Execute original logic using a mock object]]></title>
            <description><![CDATA[<div class="medium-feed-item"><p class="medium-feed-image"><a href="https://ideneal.medium.com/phpunit-execute-original-logic-using-a-mock-object-ce82a8a0869b?source=rss-8d2b88f87b92------2"><img src="https://cdn-images-1.medium.com/max/2600/0*IQdE4jjz5xTHvR0Z" width="5184"></a></p><p class="medium-feed-snippet">Allowing to execute of the original logic of a mock object</p><p class="medium-feed-link"><a href="https://ideneal.medium.com/phpunit-execute-original-logic-using-a-mock-object-ce82a8a0869b?source=rss-8d2b88f87b92------2">Continue reading on Medium »</a></p></div>]]></description>
            <link>https://ideneal.medium.com/phpunit-execute-original-logic-using-a-mock-object-ce82a8a0869b?source=rss-8d2b88f87b92------2</link>
            <guid isPermaLink="false">https://medium.com/p/ce82a8a0869b</guid>
            <category><![CDATA[phpunit]]></category>
            <category><![CDATA[php-developers]]></category>
            <category><![CDATA[test]]></category>
            <category><![CDATA[testing]]></category>
            <category><![CDATA[php]]></category>
            <dc:creator><![CDATA[Ideneal]]></dc:creator>
            <pubDate>Tue, 02 Feb 2021 15:01:09 GMT</pubDate>
            <atom:updated>2021-02-02T15:01:09.373Z</atom:updated>
        </item>
        <item>
            <title><![CDATA[Symfony — Manage JSON API requests saving a lot of time]]></title>
            <description><![CDATA[<div class="medium-feed-item"><p class="medium-feed-image"><a href="https://ideneal.medium.com/symfony-manage-json-api-requests-saving-a-lot-of-time-d6fae1e5ee60?source=rss-8d2b88f87b92------2"><img src="https://cdn-images-1.medium.com/max/2600/0*EFIcbcy9fUeEBrU2" width="6016"></a></p><p class="medium-feed-snippet">Automatic request content deserialization and validation</p><p class="medium-feed-link"><a href="https://ideneal.medium.com/symfony-manage-json-api-requests-saving-a-lot-of-time-d6fae1e5ee60?source=rss-8d2b88f87b92------2">Continue reading on Medium »</a></p></div>]]></description>
            <link>https://ideneal.medium.com/symfony-manage-json-api-requests-saving-a-lot-of-time-d6fae1e5ee60?source=rss-8d2b88f87b92------2</link>
            <guid isPermaLink="false">https://medium.com/p/d6fae1e5ee60</guid>
            <category><![CDATA[web-development]]></category>
            <category><![CDATA[api]]></category>
            <category><![CDATA[php]]></category>
            <category><![CDATA[symfony]]></category>
            <category><![CDATA[programming]]></category>
            <dc:creator><![CDATA[Ideneal]]></dc:creator>
            <pubDate>Tue, 16 Jun 2020 05:01:01 GMT</pubDate>
            <atom:updated>2020-06-16T05:01:01.072Z</atom:updated>
        </item>
        <item>
            <title><![CDATA[How to connect a HOC with React and Redux]]></title>
            <description><![CDATA[<div class="medium-feed-item"><p class="medium-feed-image"><a href="https://levelup.gitconnected.com/how-to-connect-hoc-with-react-and-redux-2b3bce6a7dbf?source=rss-8d2b88f87b92------2"><img src="https://cdn-images-1.medium.com/max/2600/0*qtk29oApnX1_x3RW" width="3898"></a></p><p class="medium-feed-snippet">Create a loading component using HOCs while fetching data from your server and have fun!</p><p class="medium-feed-link"><a href="https://levelup.gitconnected.com/how-to-connect-hoc-with-react-and-redux-2b3bce6a7dbf?source=rss-8d2b88f87b92------2">Continue reading on Level Up Coding »</a></p></div>]]></description>
            <link>https://levelup.gitconnected.com/how-to-connect-hoc-with-react-and-redux-2b3bce6a7dbf?source=rss-8d2b88f87b92------2</link>
            <guid isPermaLink="false">https://medium.com/p/2b3bce6a7dbf</guid>
            <category><![CDATA[software-development]]></category>
            <category><![CDATA[javascript]]></category>
            <category><![CDATA[react]]></category>
            <category><![CDATA[programming]]></category>
            <category><![CDATA[redux]]></category>
            <dc:creator><![CDATA[Ideneal]]></dc:creator>
            <pubDate>Tue, 21 Jan 2020 01:01:13 GMT</pubDate>
            <atom:updated>2020-01-26T17:05:02.548Z</atom:updated>
        </item>
        <item>
            <title><![CDATA[PHPUnit — Mocking the real file system]]></title>
            <description><![CDATA[<div class="medium-feed-item"><p class="medium-feed-image"><a href="https://medium.com/swlh/phpunit-mocking-the-real-file-system-f6da6d1802cf?source=rss-8d2b88f87b92------2"><img src="https://cdn-images-1.medium.com/max/2600/0*zkDzmR2Vimi3BraG" width="3964"></a></p><p class="medium-feed-snippet">How to use a stream wrapper for a virtual file system and have fun!</p><p class="medium-feed-link"><a href="https://medium.com/swlh/phpunit-mocking-the-real-file-system-f6da6d1802cf?source=rss-8d2b88f87b92------2">Continue reading on The Startup »</a></p></div>]]></description>
            <link>https://medium.com/swlh/phpunit-mocking-the-real-file-system-f6da6d1802cf?source=rss-8d2b88f87b92------2</link>
            <guid isPermaLink="false">https://medium.com/p/f6da6d1802cf</guid>
            <category><![CDATA[programming]]></category>
            <category><![CDATA[testing]]></category>
            <category><![CDATA[test]]></category>
            <category><![CDATA[phpunit]]></category>
            <category><![CDATA[php]]></category>
            <dc:creator><![CDATA[Ideneal]]></dc:creator>
            <pubDate>Tue, 15 Oct 2019 08:16:36 GMT</pubDate>
            <atom:updated>2019-10-16T12:49:41.639Z</atom:updated>
        </item>
        <item>
            <title><![CDATA[SOLID Principles Simplified with Examples in PHP]]></title>
            <description><![CDATA[<div class="medium-feed-item"><p class="medium-feed-image"><a href="https://levelup.gitconnected.com/solid-principles-simplified-php-examples-based-dc6b4f8861f6?source=rss-8d2b88f87b92------2"><img src="https://cdn-images-1.medium.com/max/2600/0*BYjprma5eQpGV08e" width="5184"></a></p><p class="medium-feed-snippet">SOLID is an acronym for the first five object-oriented design (OOD) principles and is a series of guidelines that developers can use to&#x2026;</p><p class="medium-feed-link"><a href="https://levelup.gitconnected.com/solid-principles-simplified-php-examples-based-dc6b4f8861f6?source=rss-8d2b88f87b92------2">Continue reading on Level Up Coding »</a></p></div>]]></description>
            <link>https://levelup.gitconnected.com/solid-principles-simplified-php-examples-based-dc6b4f8861f6?source=rss-8d2b88f87b92------2</link>
            <guid isPermaLink="false">https://medium.com/p/dc6b4f8861f6</guid>
            <category><![CDATA[code-quality]]></category>
            <category><![CDATA[php-development]]></category>
            <category><![CDATA[software-development]]></category>
            <category><![CDATA[php]]></category>
            <category><![CDATA[solid]]></category>
            <dc:creator><![CDATA[Ideneal]]></dc:creator>
            <pubDate>Fri, 23 Aug 2019 14:39:10 GMT</pubDate>
            <atom:updated>2019-09-16T14:30:51.919Z</atom:updated>
        </item>
        <item>
            <title><![CDATA[Why using hash based urls in your React SPA will save you more time than you think?]]></title>
            <link>https://itnext.io/why-using-hash-based-urls-in-your-react-spa-will-save-you-more-time-than-you-think-a21e2c560879?source=rss-8d2b88f87b92------2</link>
            <guid isPermaLink="false">https://medium.com/p/a21e2c560879</guid>
            <category><![CDATA[coding]]></category>
            <category><![CDATA[javascript]]></category>
            <category><![CDATA[react]]></category>
            <dc:creator><![CDATA[Ideneal]]></dc:creator>
            <pubDate>Wed, 24 Jul 2019 08:01:01 GMT</pubDate>
            <atom:updated>2019-07-28T11:55:54.626Z</atom:updated>
            <content:encoded><![CDATA[<figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/0*40la6c_a7sAP-HkM" /><figcaption>Photo by <a href="https://unsplash.com/@casparrubin?utm_source=medium&amp;utm_medium=referral">Caspar Camille Rubin</a> on <a href="https://unsplash.com/?utm_source=medium&amp;utm_medium=referral">Unsplash</a></figcaption></figure><p>When you are developing your Single Page Application there is a moment when you need design the url structure too.</p><p>Usually developers use 2 types of url…</p><h3>Normal Url</h3><p>This is the url that we are used to seeing, with normal path, like this:</p><pre>https://myspa.com/app/login<br>https://myspa.com/app/dashboard</pre><p>This kind of url has the advantage of SEO, but you have to create much back-end endpoint as many as application routes in order to avoid the 404 error code message while reloading the url or pass the url to other people.</p><h3>Hash Based Url</h3><p>This url has a unique application endpoint /app/ controlled by one single back-end action and multiple hashtags managed by Javascript that define the SPA routes.</p><pre>https://myspa.com/app/<strong>#/login</strong><br>https://myspa.com/app/<strong>#/dashboard</strong></pre><p>In this case we just need to create one single controller for the app. Doing that the front-end team will only use the back-end API, separating the front-end and back-end logic and keeping <strong><em>clear the whole structure</em></strong>! To the other hand the SEO is more difficult to manage (but this is relative when we are talking about SPA).</p><h3>Implement Hash Based Url in React</h3><p>In order to implement the <em>hash based url</em> in our SPA with React I choose to use <a href="https://github.com/supasate/connected-react-router">Connected React Router</a> library.</p><p>This library can:</p><ul><li>Synchronize router state with redux store through uni-directional flow</li><li>Support both React Router v4 and v5</li><li>Support functional component hot reloading while preserving state</li><li>Dispatching of history methods (push, replace, go, goBack, goForward) works for both <a href="https://github.com/gaearon/redux-thunk">redux-thunk</a> and <a href="https://github.com/yelouafi/redux-saga">redux-saga</a></li><li>And much more!</li></ul><p>So first of all we need to install it</p><pre>yarn add connected-react-router</pre><p>After that we need to create a reducer with the specific key <strong>router</strong></p><pre>// reducers.js<br>import { combineReducers } from &#39;redux&#39;<br>import { connectRouter } from &#39;connected-react-router&#39;<br><br>export default (history) =&gt; combineReducers({<br>  router: connectRouter(history),<br>  ... // rest of your reducers<br>})</pre><p>Now we add the related middleware so we can dispatch history actions</p><pre>// configureStore.js<br>...<br>import { createHashHistory } from &#39;history&#39;<br>import { applyMiddleware, compose, createStore } from &#39;redux&#39;<br>import { routerMiddleware } from &#39;connected-react-router&#39;<br>import createRootReducer from &#39;./reducers&#39;<br>...<br><strong>export const history = createHashHistory({<br>    hashType: &#39;slash&#39;,<br>    getUserConfirmation: (message, callback) =&gt; callback(window.confirm(message))<br>});</strong><br><br>export default function configureStore(preloadedState) {<br>  const store = createStore(<br>    createRootReducer(history), // root reducer with router state<br>    preloadedState,<br>    compose(<br>      applyMiddleware(<br>        routerMiddleware(history), // for dispatching history actions<br>        // ... other middlewares ...<br>      ),<br>    ),<br>  )<br><br>  return store<br>}</pre><p>history package provides 3 different methods for creating a history object, depending on your environment.</p><ul><li>createBrowserHistory is for use in modern web browsers that support the <a href="http://diveintohtml5.info/history.html">HTML5 history API</a> (see <a href="http://caniuse.com/#feat=history">cross-browser compatibility</a>)</li><li>createMemoryHistory is used as a reference implementation and may also be used in non-DOM environments, like <a href="https://facebook.github.io/react-native/">React Native</a> or tests</li><li>createHashHistory is for use in legacy web browsers</li></ul><p>We are using <em>createHashHistory</em> instead of <em>createBrowserHistory </em>in order to use hash based urls<em>.</em></p><p>Finally retrieve the ConnectedRouter and pass it the created history.</p><pre>// index.js<br>...<br>import { Provider } from &#39;react-redux&#39;<br>import { Route, Switch } from &#39;react-router&#39; // react-router v4/v5<br>import { ConnectedRouter } from &#39;connected-react-router&#39;<br>import configureStore, { history } from &#39;./configureStore&#39;<br>...<br>const store = configureStore(/* provide initial state if any */)<br><br>ReactDOM.render(<br>  &lt;Provider store={store}&gt;<br>    &lt;<strong>ConnectedRouter history={history}</strong>&gt; { /* place ConnectedRouter under Provider */ }<br>      &lt;&gt; { /* your usual react-router v4/v5 routing */ }<br>        &lt;Switch&gt;<br>          &lt;Route exact path=&quot;/&quot; render={() =&gt; (&lt;div&gt;Match&lt;/div&gt;)} /&gt;<br>          &lt;Route render={() =&gt; (&lt;div&gt;Miss&lt;/div&gt;)} /&gt;<br>        &lt;/Switch&gt;<br>      &lt;/&gt;<br>    &lt;/ConnectedRouter&gt;<br>  &lt;/Provider&gt;,<br>  document.getElementById(&#39;react-root&#39;)<br>)</pre><p>Enjoy! 😉</p><p>Thanks for reading and if you liked this post feel free to leave a comment and don’t forget to clap! 😃</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=a21e2c560879" width="1" height="1" alt=""><hr><p><a href="https://itnext.io/why-using-hash-based-urls-in-your-react-spa-will-save-you-more-time-than-you-think-a21e2c560879">Why using hash based urls in your React SPA will save you more time than you think?</a> was originally published in <a href="https://itnext.io/">ITNEXT</a> on Medium, where people are continuing the conversation by highlighting and responding to this story.</p>]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Securing Authentication in a SPA Using JWT Token — The coolest way]]></title>
            <link>https://ideneal.medium.com/securing-authentication-in-a-spa-using-jwt-token-the-coolest-way-ab883bc372b6?source=rss-8d2b88f87b92------2</link>
            <guid isPermaLink="false">https://medium.com/p/ab883bc372b6</guid>
            <category><![CDATA[javascript]]></category>
            <category><![CDATA[react]]></category>
            <category><![CDATA[vuejs]]></category>
            <category><![CDATA[authentication]]></category>
            <category><![CDATA[security]]></category>
            <dc:creator><![CDATA[Ideneal]]></dc:creator>
            <pubDate>Mon, 08 Jul 2019 16:40:29 GMT</pubDate>
            <atom:updated>2019-07-08T16:40:29.607Z</atom:updated>
            <content:encoded><![CDATA[<h3>Securing Authentication in a SPA Using JWT Token — The coolest way</h3><h4>In this post we will explore the right way to use JWT token in a Single Page Application and HOW to manage it in order to avoid security issues. Read on!</h4><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/0*dPVMxSPboPCBVjSi" /><figcaption>Photo by <a href="https://unsplash.com/@kaitlynbaker?utm_source=medium&amp;utm_medium=referral">Kaitlyn Baker</a> on <a href="https://unsplash.com/?utm_source=medium&amp;utm_medium=referral">Unsplash</a></figcaption></figure><p>Single Page Applications are become the standard of the web development architecture. One of the best methods to authenticate a user is by using JWT web token, so first of all, let’s see a general view of what is it and when you should use it!</p><h3>What’s JWT token?</h3><p>JSON Web Token (JWT) is an open standard (<a href="https://tools.ietf.org/html/rfc7519">RFC 7519</a>) that defines a compact and self-contained way for securely transmitting information between parties as a JSON object. This information can be verified and trusted because it is digitally signed. JWTs can be signed using a secret (with the <strong>HMAC</strong> algorithm) or a public/private key pair using <strong>RSA</strong> or <strong>ECDSA</strong>.</p><p>It consists of three parts separated by dots (.), which are:</p><ul><li><strong>Header</strong>: A JSON(Base64 encoded) that has info about algorithm used(like HS256, RSA) and so on.</li><li><strong>Payload</strong>: A JSON(Base64 encoded) that has info about the user.</li><li><strong>Signature</strong>: a signed string generated by the header algorithm by joining the encoded header, the encoded payload and a secret.</li></ul><p>Therefore, a JWT typically looks like the following:</p><p>header.payload.signature</p><h3>JWT Tokens vs Cookies</h3><p>As a stateless Single Page Application, after you login (via username/password, OAuth 2, etc), for every future API request, the server must need to know who you are in order to perform authenticated/authorized actions.<strong><em> <br>The are 2 main ways to identify a client in your REST API:</em></strong></p><ul><li><strong><em>Session Cookie</em></strong></li><li><strong><em>Auth Token</em></strong></li></ul><p><strong><em>Both of them offer many pros and cons:</em></strong></p><h4>Session Cookie</h4><p><strong>Usage Pros</strong></p><ul><li>They are stored by the browser with a related expires date and associated domain.</li><li>If the user closes the browser, the cookie will be deleted and he is no longer logged in.</li><li>Using the HttpOnly option the browser javascript cannot read it.</li><li>Using the Secure option the cookie will be stored only if the request has been sent over a secure channel (HTTPS).</li><li>Using the SameSite option it will not send with cross-site requests. This protect your API from CSRF attack.</li></ul><p><strong>Usage Cons</strong></p><ul><li>The SameSite option is not supported by all browsers, so you cannot completely trust it.</li><li>If the cookie is not created by HttpOnly option it is vulnerable to the XSS attack, so an attacker could steal user session cookie.</li><li>The cookie will be sent over cross-site requests and to avoid CSRF attack you should use the CSRF token for each request.</li><li>It store a session inside the server.</li></ul><h4>JWT Token</h4><p><strong>Usage Pros</strong></p><ul><li>Server has just to verify the token validity in order to authenticate a user, it doesn’t need to ask to a database.</li><li>It works the same for both native mobile apps and browser clients.</li><li>If you store the token inside the local storage you don’t worry about CSRF attack.</li><li>Browser javascript has the full control over it.</li></ul><p><strong>Usage Cons</strong></p><ul><li>Browser javascript has the full controll over it 😆 you have to store your token anywhere (local storage, cookies, memory, …) but if your application is vulnerable to XSS an attacker could steal the user token and impersonate him.</li><li>If you store the token into cookies it is also vulnerable to CSRF attack.</li></ul><p>At this point the main issue using JWT is where to store it in order to avoid the XSS and CSRF attacks.</p><h3>Storing JWT Token: The Cookie and Local Storage Approach</h3><p>I was inspired by <a href="https://medium.com/u/1aeb2b5a3653">Peter Locke</a> post <a href="https://medium.com/lightrail/getting-token-authentication-right-in-a-stateless-single-page-application-57d0c6474e3">https://medium.com/lightrail/getting-token-authentication-right-in-a-stateless-single-page-application-57d0c6474e3</a> where he explains how to store token into 2 cookies but mine approach is a little bit different.</p><p>The main idea is to split the JWT token into 2 parts, but instead of using 2 cookies and after that use CSRF token for each request I store Header and Payload into the local storage and the Signature into a Session Cookie with HttpOnly option set to true.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/801/1*IV9pFJks8Ukb0jcB9wClrQ.png" /></figure><p>The server has to rebuild the token before validate it, so the approach became the following:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/818/1*UYTI6QzLWLuj5z6XPIn5vg.jpeg" /></figure><p>In this way the CSRF attack is prevented by the part of token saved into the Local Storage, and XSS attack is limited by the Session Cookie which is not accessible via browser javascript.</p><p>This solution solves all security issues, avoid to create a CSRF token for each request and maintain a stateless application.</p><p>If you liked this post feel free to leave a comment about it and don’t forget to clap!</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=ab883bc372b6" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[How to handle JSON requests using forms on Symfony 4 and getting a clean code]]></title>
            <description><![CDATA[<div class="medium-feed-item"><p class="medium-feed-image"><a href="https://ideneal.medium.com/how-to-handle-json-requests-using-forms-on-symfony-4-and-getting-a-clean-code-67dd796f3d2f?source=rss-8d2b88f87b92------2"><img src="https://cdn-images-1.medium.com/max/2600/0*xuxuTqLPoc_tlGbu" width="5472"></a></p><p class="medium-feed-snippet">The cleanest way to deal with JSON form request</p><p class="medium-feed-link"><a href="https://ideneal.medium.com/how-to-handle-json-requests-using-forms-on-symfony-4-and-getting-a-clean-code-67dd796f3d2f?source=rss-8d2b88f87b92------2">Continue reading on Medium »</a></p></div>]]></description>
            <link>https://ideneal.medium.com/how-to-handle-json-requests-using-forms-on-symfony-4-and-getting-a-clean-code-67dd796f3d2f?source=rss-8d2b88f87b92------2</link>
            <guid isPermaLink="false">https://medium.com/p/67dd796f3d2f</guid>
            <category><![CDATA[javascript]]></category>
            <category><![CDATA[development]]></category>
            <category><![CDATA[php]]></category>
            <category><![CDATA[symfony]]></category>
            <category><![CDATA[jquery]]></category>
            <dc:creator><![CDATA[Ideneal]]></dc:creator>
            <pubDate>Tue, 02 Jul 2019 09:11:42 GMT</pubDate>
            <atom:updated>2019-07-02T09:11:42.360Z</atom:updated>
        </item>
        <item>
            <title><![CDATA[Symfony 4 — A good way to deal with exceptions for REST API]]></title>
            <link>https://ideneal.medium.com/symfony-4-a-good-way-to-deal-with-exceptions-for-rest-api-afd8b615c923?source=rss-8d2b88f87b92------2</link>
            <guid isPermaLink="false">https://medium.com/p/afd8b615c923</guid>
            <category><![CDATA[symfony]]></category>
            <category><![CDATA[symfony4]]></category>
            <category><![CDATA[php]]></category>
            <category><![CDATA[api]]></category>
            <dc:creator><![CDATA[Ideneal]]></dc:creator>
            <pubDate>Wed, 19 Jun 2019 13:29:26 GMT</pubDate>
            <atom:updated>2019-06-19T13:29:26.092Z</atom:updated>
            <content:encoded><![CDATA[<h3>Symfony 4 — A good way to deal with exceptions for REST API</h3><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/0*7wDITUE3eRy0Jdo_" /><figcaption>Photo by <a href="https://unsplash.com/@nateggrant?utm_source=medium&amp;utm_medium=referral">Nate Grant</a> on <a href="https://unsplash.com/?utm_source=medium&amp;utm_medium=referral">Unsplash</a></figcaption></figure><p>There are many ways to handle exceptions and put them in your response, especially if you are developing a REST API for your project.</p><p>In most cases, when you are creating an API interface, you could need to design a JSON response structure in order to use the same format for all clients, like the following:</p><pre>{<br>  &quot;message&quot;: &quot;&quot;,<br>  &quot;data&quot;: [],<br>  &quot;errors&quot;: {}<br>}</pre><p>There are two types of errors: the <strong>simple message error</strong> that is usually given by the exception message and it will be display in the <em>message</em> key and the <strong>fields errors</strong> that is represented by a field error and it will be display in <em>errors </em>key<em>. </em>For instance in a form submitted response, <em>errors</em> represents the related input form errors, like the following:</p><pre>{<br>  &quot;<strong>message</strong>&quot;<strong>:</strong> &quot;Invalid form&quot;,<br>  &quot;<strong>data</strong>&quot;<strong>: </strong>{},<br>  &quot;<strong>errors</strong>&quot;<strong>: </strong>{<br>    &quot;<strong>email</strong>&quot;: [<br>      &quot;This is not a valid email&quot;<br>    ]<br>  }<br>}</pre><p>This structure is most useful when we are dealing with an <strong>api</strong>. You can create a response like that simply creating an ApiResponse as the following:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/754396fa2e3354f131dc2d168b3ab328/href">https://medium.com/media/754396fa2e3354f131dc2d168b3ab328/href</a></iframe><p>It’s easy! But troubles come when we need to handle exceptions as a API response.</p><h3>Handle exceptions</h3><p>First of all lets create an <em>EventListener</em> to handle exceptions and change them for our purpose.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/8ffd69d85597d2cc5aac733e13379cae/href">https://medium.com/media/8ffd69d85597d2cc5aac733e13379cae/href</a></iframe><p>Now you need to register it as a service and tell to Symfony that it is a <em>listener</em> on <em>kernel.exception</em> by setting <strong>tag</strong>.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/ca069c8ca33f134a11f42f7b7481c5f0/href">https://medium.com/media/ca069c8ca33f134a11f42f7b7481c5f0/href</a></iframe><p>If you are a careful reader you could have noticed that <em>$errors</em> is an empty array. Which we need to do now is to transform fields errors, like form errors, into associative array. Here <a href="https://symfony.com/doc/current/components/serializer.html#normalizers"><strong>Normalizer</strong></a> component comes to help us to transform any data, in this case exceptions, into an array.</p><h3>Normalize form exceptions</h3><p>At this point we have to create a Normalizer for a form exception, but first lets create a FormException 😄</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/45c4675e4e66d19badbea8dc0215db68/href">https://medium.com/media/45c4675e4e66d19badbea8dc0215db68/href</a></iframe><p>Now that we have a FormException lets create the related normalizer…</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/79c12c3490a4292a0e7fc83c216a5bda/href">https://medium.com/media/79c12c3490a4292a0e7fc83c216a5bda/href</a></iframe><p>we need to register it as <em>serializer.normalizer </em>in <em>service.yaml </em>file</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/a5e60a103463147e6eeba1c6e0633f89/href">https://medium.com/media/a5e60a103463147e6eeba1c6e0633f89/href</a></iframe><h3>Normalizer factory</h3><p>Like form exceptions we could have other custom field exceptions such as a violation list after validating an object, so we need to create a Factory to get the right Normalizer for the passed exception.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/39abb3b11cfcb97cbdc0bbab5c4d80f2/href">https://medium.com/media/39abb3b11cfcb97cbdc0bbab5c4d80f2/href</a></iframe><p>To pass registered normalizers to NormalizerFactory construct method Symfony provides a shortcut to inject all services tagged with a specific tag</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/7174316485b166cc8d7e382eaea3704a/href">https://medium.com/media/7174316485b166cc8d7e382eaea3704a/href</a></iframe><p>Now all we need is to update the ExceptionListerner…</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/1f98694563c535a4dd83e4fe6b2286d4/href">https://medium.com/media/1f98694563c535a4dd83e4fe6b2286d4/href</a></iframe><p>…and the game is done! 😃<br>We have finally handled exceptions and transform them to an API response!</p><p>I hope this article helps you to solve this issue. <br>If you solve it in other way or just you want to let me know what do you think about it, leave me a comment 😃</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=afd8b615c923" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[How to export Symfony 4 environment variables into front-end application with Encore]]></title>
            <link>https://ideneal.medium.com/how-to-export-symfony-4-environment-variables-into-front-end-application-with-encore-ed45463bee5a?source=rss-8d2b88f87b92------2</link>
            <guid isPermaLink="false">https://medium.com/p/ed45463bee5a</guid>
            <category><![CDATA[symfony4]]></category>
            <category><![CDATA[symfony]]></category>
            <category><![CDATA[javascript]]></category>
            <dc:creator><![CDATA[Ideneal]]></dc:creator>
            <pubDate>Sun, 26 May 2019 13:36:18 GMT</pubDate>
            <atom:updated>2019-05-26T13:36:18.625Z</atom:updated>
            <content:encoded><![CDATA[<figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/0*YsD7LabpYI5uPF7R" /><figcaption>Photo by <a href="https://unsplash.com/@sapegin?utm_source=medium&amp;utm_medium=referral">Artem Sapegin</a> on <a href="https://unsplash.com/?utm_source=medium&amp;utm_medium=referral">Unsplash</a></figcaption></figure><p>As you probably know if you are developing a well design project you might need to use Webpack Encore which is the simpler way to integrate Webpack in your Symfony application. In some cases you might need to use some environment variables from your back-end to your front-end logic, for instance for an api key.</p><p>To solve this problem I’ve used the <a href="https://github.com/motdotla/dotenv"><strong>dotenv</strong></a> library, so the first step is to install it.</p><pre>yarn add dotenv</pre><p>After that load dotenv in your <strong>webpack.config.js</strong> file, import the library and use the Encore’s method <em>configureDefinePlugin</em> to define the new environment variables:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/76c7ecbe34010740ea4225c71f68fb4e/href">https://medium.com/media/76c7ecbe34010740ea4225c71f68fb4e/href</a></iframe><p>Now you can access to the api key in your front-end code by the process environment variable:</p><pre>const BEST_API_KEY = process.env.BEST_API_KEY; </pre><p>Enjoy 😉</p><p>Did you solve this issue in another way? Leave me a comment 😃</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=ed45463bee5a" width="1" height="1" alt="">]]></content:encoded>
        </item>
    </channel>
</rss>