<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <channel>
    <title>DEV Community: A.Khouilid</title>
    <description>The latest articles on DEV Community by A.Khouilid (@abdelkbirkhoui1).</description>
    <link>https://dev.to/abdelkbirkhoui1</link>
    <image>
      <url>https://media2.dev.to/dynamic/image/width=90,height=90,fit=cover,gravity=auto,format=auto/https:%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Fuser%2Fprofile_image%2F393307%2Fec8bc902-946f-4389-aab7-46403d7c5ee5.jpg</url>
      <title>DEV Community: A.Khouilid</title>
      <link>https://dev.to/abdelkbirkhoui1</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/abdelkbirkhoui1"/>
    <language>en</language>
    <item>
      <title>Laravel middleware: My notes</title>
      <dc:creator>A.Khouilid</dc:creator>
      <pubDate>Fri, 30 Oct 2020 16:25:55 +0000</pubDate>
      <link>https://dev.to/abdelkbirkhoui1/laravel-middleware-my-notes-28c7</link>
      <guid>https://dev.to/abdelkbirkhoui1/laravel-middleware-my-notes-28c7</guid>
      <description>&lt;h2&gt;
  
  
  &amp;gt; Create Middleware
&lt;/h2&gt;

&lt;p&gt;&lt;strong&gt;Middleware provides a convenient mechanism for filtering HTTP requests entering your application. For example, Laravel includes a middleware that verifies the user of your application is authenticated. If the user is not authenticated, the middleware will redirect the user to the login screen. However, if the user is authenticated, the middleware will allow the request to proceed further into the application.&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;To create a new middleware, use the make:middleware Artisan command:&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;&lt;strong&gt;php artisan make:middleware NameOfMiddlwere&lt;/strong&gt;&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;After we create a new middleware, we can put our condition inside its method: &lt;/p&gt;

&lt;p&gt;For example, a user wants to access his profile, so we need to check if he is login or not, for that we use middleware.&lt;/p&gt;

&lt;p&gt;we create a method check if $_SESSION['is_login'] = true;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;namespace App\Http\Middleware;

use Closure;

class CheckLogin
{

    public function handle($request, Closure $next)
    {
        if (isset($_SESSION['is_login']) {
             if ($_SESSION['is_login'] == false) {
               return redirect('/');
             }
        }

        return $next($request);
    }
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;As we see, we get the request and check if the user login or not, if he not we redirect him into the home page, otherwise, we allow him to access his profile. (we use a callback function 'closure' )&lt;/p&gt;

&lt;h2&gt;
  
  
  &amp;gt; Assigning Middleware To Routes
&lt;/h2&gt;

&lt;blockquote&gt;
&lt;p&gt;if we want to assign middleware to a specific route, First we should assign the middleware a key in our app/Http/Kernel.php file.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;then we can use our middleware like this:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;Route::get('admin/profile', function () {
    //
})-&amp;gt;middleware('theKey');
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h2&gt;
  
  
  &amp;gt; Middleware Parameters
&lt;/h2&gt;

&lt;p&gt;Let's imagine that before we allow any user to access a specific route, we need to check if he's an Admin first.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;public function handle($request, Closure $next, $type)
    {
        if (isset($_SESSION['user_type'])) {

            if ($_SESSION['user_type'] == $type) {
               //Redirect...
            }
        }

        return $next($request);
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;And we can put the parameter like this...&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;Route::get('admin/profile', function () {
    //
})-&amp;gt;middleware('theKey:Admin');
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



</description>
      <category>laravel</category>
    </item>
    <item>
      <title>Don't repeat yourself principle ( DRY )</title>
      <dc:creator>A.Khouilid</dc:creator>
      <pubDate>Sun, 23 Aug 2020 18:48:51 +0000</pubDate>
      <link>https://dev.to/abdelkbirkhoui1/don-t-repeat-yourself-principle-dry-6g5</link>
      <guid>https://dev.to/abdelkbirkhoui1/don-t-repeat-yourself-principle-dry-6g5</guid>
      <description>&lt;p&gt;someone said :&lt;br&gt;
' They say good programmer writes 100 lines of code per day but a great one deletes 50. Do you agree? Let's become the latter by following the Don't Repeat Yourself principle, because the best code is no code at all '.&lt;/p&gt;

&lt;p&gt;when I made my first search about DRY I find this :&lt;br&gt;
Don't repeat yourself is a principle of software development aimed at reducing repetition of software patterns, replacing it with abstractions or using data normalization to avoid redundancy. The DRY principle is stated as "Every piece of knowledge must have a single, unambiguous, authoritative representation within a system".&lt;/p&gt;

&lt;p&gt;So what this mean?  and how can we use it? &lt;br&gt;
in the Article, I try to write a simple explication.&lt;br&gt;
Don't repeat yourself principle ( DRY ) has been formulated by Andy Hunt and Dave Thomas in their book The Pragmatic Programmer.&lt;/p&gt;

&lt;p&gt;DRY is a principle we should use every day in our code, because it makes it simple to read and understand, by never write a two function that will do the same stuff.&lt;/p&gt;

&lt;p&gt;Before you write your code, you should think two or three steep forward, this way will help you get the big image for you All code, and in this way will find yourself apply DRY.&lt;/p&gt;

&lt;p&gt;Conclusion&lt;/p&gt;

&lt;p&gt;DRY is one the principal of CLEAN CODE,&lt;br&gt;
make sure to always search for improving your code, until it is easy to read by others because one day you will be the other.&lt;/p&gt;

</description>
      <category>javascript</category>
      <category>php</category>
      <category>functional</category>
    </item>
  </channel>
</rss>
