<?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: Nwanze franklin</title>
    <description>The latest articles on DEV Community by Nwanze franklin (@franko4don).</description>
    <link>https://dev.to/franko4don</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%2F45474%2F80c3ccd3-0648-4288-b3de-bbb12e3ce353.jpeg</url>
      <title>DEV Community: Nwanze franklin</title>
      <link>https://dev.to/franko4don</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/franko4don"/>
    <language>en</language>
    <item>
      <title>Paypal in React Native WebView</title>
      <dc:creator>Nwanze franklin</dc:creator>
      <pubDate>Sat, 27 Apr 2019 16:05:52 +0000</pubDate>
      <link>https://dev.to/franko4don/paypal-in-react-native-4emd</link>
      <guid>https://dev.to/franko4don/paypal-in-react-native-4emd</guid>
      <description>&lt;p&gt;React native is one of the most popular and widely used Javascript cross platform mobile development framework. It is based on ReactJS thus making it easy for developers who are already good with ReactJS to delve into it. For more on React Native check here.&lt;/p&gt;

&lt;p&gt;The Process&lt;br&gt;
I was working on a project that needed Paypal integration recently and couldn’t find a stable open source React Native module for it so I came up with an implementation based on WebView. &lt;/p&gt;

&lt;p&gt;Steps to complete before implementation&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Create a Paypal account if you don’t have one already&lt;/li&gt;
&lt;li&gt;Login to developer and create a sandbox account&lt;/li&gt;
&lt;li&gt;Create an app with an already existing sandbox account and get a Client ID&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fr4ieashvj364fvlq6v1v.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fr4ieashvj364fvlq6v1v.png" alt="Creating a new app with an existing sandbox account"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Set up the React Native part&lt;/p&gt;

&lt;p&gt;At this point it is assumed that most part of you app is working and perhaps you need the payment part to complete the flow process.&lt;br&gt;
Since we are using WebView there will be a HTML file which we will design to suit our app user interface and a component to communicate with the WebView back and forth.&lt;/p&gt;

&lt;p&gt;&lt;code&gt;PayPal.js&lt;/code&gt;&lt;/p&gt;


&lt;div class="ltag_gist-liquid-tag"&gt;
  
&lt;/div&gt;


&lt;p&gt;Brief explanation of some functions and variables&lt;/p&gt;

&lt;p&gt;&lt;code&gt;const patchPostMessageFunction&lt;/code&gt; fixes the issue described here.&lt;/p&gt;

&lt;p&gt;&lt;code&gt;handleMessage&lt;/code&gt; function receives the data emitted from Javascript within &lt;code&gt;paypal.html&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;&lt;code&gt;passValues&lt;/code&gt; function sends data from react native to &lt;code&gt;paypal.html&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;&lt;code&gt;sent&lt;/code&gt; key in the state is used to mark when data has been passed from react native to WebView.&lt;/p&gt;

&lt;p&gt;&lt;code&gt;paypal.html&lt;/code&gt;&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;&amp;lt;!DOCTYPE html&amp;gt;
&amp;lt;html lang=""&amp;gt;
  &amp;lt;head&amp;gt;
    &amp;lt;meta charset="utf-8" /&amp;gt;
    &amp;lt;meta http-equiv="X-UA-Compatible" content="IE=edge" /&amp;gt;
    &amp;lt;meta name="viewport" content="width=device-width, initial-scale=1" /&amp;gt;
    &amp;lt;title&amp;gt;Paypal&amp;lt;/title&amp;gt;
    &amp;lt;script src="https://www.paypal.com/sdk/js?client-id={clientID}"&amp;gt;&amp;lt;/script&amp;gt;

    &amp;lt;style&amp;gt;
        html, body{
            width: 100%;
            height: 100%;
            margin: 0;
            padding: 0;
            font-family: 'Segoe UI', Tahoma, Geneva, Verdana, sans-serif;
            background: linear-gradient(180deg, #3D40AD 0%, #6B6ED7 100%);
        }
        .container{
            height: 100%;
            display: flex;
            margin-left: 20px;
            margin-right: 20px;
            overflow-y: scroll;
            justify-content: center;
            align-items: center;
        }
        p{
            color: white;
            font-size: 16px;
            text-align: justify;
            margin-bottom: 50px;
        }
        #preloaderSpinner{
          display: none;
        }
    &amp;lt;/style&amp;gt;
  &amp;lt;/head&amp;gt;
  &amp;lt;body&amp;gt;
      &amp;lt;div class="container"&amp;gt;
            &amp;lt;div style="justify-content: center; text-align: center"&amp;gt;
                &amp;lt;img width="80px" height="80px" src="logo.png"/&amp;gt;
                &amp;lt;p&amp;gt;
                &amp;lt;/p&amp;gt;
                &amp;lt;div id="paypal-button-container"&amp;gt;&amp;lt;/div&amp;gt;
            &amp;lt;/div&amp;gt;
      &amp;lt;/div&amp;gt;


    &amp;lt;script&amp;gt;
      function payWithPayPal(amount, orderID) {
        paypal
          .Buttons({

            createOrder: function(data, actions) {
                return new Promise(function(resolve, reject){
                  resolve(orderID);
              });
            },
            onApprove: function(data, actions) {
              window.postMessage(JSON.stringify({
                    reference: data.orderID, 
                    message: 'Transaction Successful',
                    status: 'success'
                }));
            }
          })
          .render("#paypal-button-container");
      }
      document.addEventListener("message", function(data) {
        var details = JSON.parse(data.data);
        document.querySelector('p').innerText = "You are about to fund your wallet with USD "+details.amount+" on XYZ. Click on any of the payment options to proceed. Your account will be credited instantly after payment."
        payWithPayPal(details.amount, details.orderID);
      })

    &amp;lt;/script&amp;gt;
  &amp;lt;/body&amp;gt;
&amp;lt;/html&amp;gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;

&lt;p&gt;&lt;code&gt;document.addEventListener("message", function(data))&lt;/code&gt; part listens for message emitted from react native and receives the data sent. This data is then parsed back to JSON, used to prepare a display message and setup PayPal SDK as well. &lt;/p&gt;

&lt;p&gt;Transactions are initiated from the server end via an API call and a payload containing the orderID from PayPal is returned.&lt;/p&gt;

&lt;p&gt;To initiate a transaction from the backend, PayPal SDKs are very helpful and they can be found here for different languages.&lt;/p&gt;

&lt;h2&gt;
  
  
  Conclusion
&lt;/h2&gt;

&lt;p&gt;React Native WebView Comes in handy for situations like this as well as plotting some complex animated graphs. I would like to hear from you and learn more too.&lt;/p&gt;

</description>
      <category>reactnative</category>
      <category>android</category>
      <category>ios</category>
      <category>mobile</category>
    </item>
    <item>
      <title>Ravot (A realtime dashboard)</title>
      <dc:creator>Nwanze franklin</dc:creator>
      <pubDate>Fri, 25 May 2018 16:02:48 +0000</pubDate>
      <link>https://dev.to/franko4don/ravot-4hk8</link>
      <guid>https://dev.to/franko4don/ravot-4hk8</guid>
      <description>&lt;h1&gt;
  
  
  What I built
&lt;/h1&gt;

&lt;p&gt;A real time dashboard for a company&lt;/p&gt;

&lt;h2&gt;
  
  
  Demo Link
&lt;/h2&gt;

&lt;p&gt;&lt;a href="https://realtime.bluebecks.com/"&gt;https://realtime.bluebecks.com/&lt;/a&gt;&lt;br&gt;
for the purpose of test I am dropping a single admin user login and password&lt;/p&gt;

&lt;p&gt;&lt;a href="https://realtime.bluebecks.com/login"&gt;https://realtime.bluebecks.com/login&lt;/a&gt;&lt;br&gt;
&lt;code&gt;email: franko4don@gmail.com&lt;/code&gt;&lt;br&gt;
&lt;code&gt;password: tomcat&lt;/code&gt;&lt;/p&gt;

&lt;h2&gt;
  
  
  Link to Code
&lt;/h2&gt;

&lt;p&gt;&lt;a href="https://github.com/RealDash/realtime-dashboard"&gt;https://github.com/RealDash/realtime-dashboard&lt;/a&gt;&lt;/p&gt;

&lt;h1&gt;
  
  
  How I built it (what's the stack? did I run into issues or discover something new along the way?)
&lt;/h1&gt;

&lt;ul&gt;
&lt;li&gt;Project Overview&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;We worked on a real time dashboard to display tasks, announcements, important events in the future, which music is currently playing etc. Our dashboard was built using Laravel, Vue and Pusher.&lt;br&gt;
Our intent is to display this dashboard on a TV Screen against a wall in our office.&lt;/p&gt;

&lt;p&gt;The dashboard(live dashboard end) will have the following:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;Upcoming events scheduled by the admin.
An announcement section to display announcement to interns/team members.&lt;/li&gt;
&lt;li&gt;A Scrumy board section.&lt;/li&gt;
&lt;li&gt;Github section to display statistics of our numerous public repositories on Github.&lt;/li&gt;
&lt;li&gt;Bitbucket section.&lt;/li&gt;
&lt;li&gt;The music that is currently playing in the office.&lt;/li&gt;
&lt;li&gt;A clock with the current date and time.&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;After the browser displays the dashboard for the first time, we’ll never refresh the page again. WebSockets and Vue are being leveraged to update the sections. Doing it this way will avoid having to refresh the page and in turn flashing screens.&lt;/p&gt;

&lt;p&gt;Each section is its own Vue component. Laravel’s default scheduler is used to check for scheduled events and fires an event which is broadcasted to pusher when a logic is satified. When data is received, a broadcast event is fired to Pusher.&lt;/p&gt;

&lt;p&gt;This powerful service (Pusher) leverages websockets to carry over server events to clients in real-time. On the client side we used Laravel’s echo. The JavaScript library that makes handling those Pusher events very easy. Still on the client side each Vue component will listen to new events to update on the displayed sections.&lt;/p&gt;

&lt;h1&gt;
  
  
  Team Members
&lt;/h1&gt;

&lt;p&gt;We had three persons who worked on this project. Team members collaborated on Github and project development began on Friday 18th May, 2018 that's after initial commit on 2nd of may, 2018. &lt;/p&gt;

&lt;p&gt;Below are the names of the team members:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;Nwanze Franklin (franko4don): Team Lead&lt;/li&gt;
&lt;li&gt;Timothy Onyiuke (timolinn): Team Member&lt;/li&gt;
&lt;li&gt;Nwankwo Gabriel (gfuns): Team Member&lt;/li&gt;
&lt;/ol&gt;

&lt;h1&gt;
  
  
  How it was built
&lt;/h1&gt;

&lt;p&gt;We started out with the requirement analysis on the system we proposed to design which is a mini task management system with real time feed. We iterated over a few designs and technologies to use then got our specifications. &lt;/p&gt;

&lt;p&gt;After much deliberation we came up with the following &lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Specifications&lt;/li&gt;
&lt;/ul&gt;

&lt;ol&gt;
&lt;li&gt;Laravel, vuejs, Laravel Echo and Pusher will be used for the project, Laravel     for backend, Laravel Echo and Vuejs for front end and Pusher as websocket for realtime updates&lt;/li&gt;
&lt;li&gt;Task management module for admins to create tasks while setting the description
and the scheduled time of start and completion.&lt;/li&gt;
&lt;li&gt;Task Assignment module for users who will be able to pick tasks, work on them and deliver by clicking a button so that it goes in for review&lt;/li&gt;
&lt;li&gt;Music management module which Enables the admin to create and delete music as well as set the current music to play on the live dashboard view with just a click in real time&lt;/li&gt;
&lt;li&gt;Announcment management module which enables admins create company wide announce ments.&lt;/li&gt;
&lt;li&gt;Gist module for users where anyone can drop the latest gist in shortest words as possible&lt;/li&gt;
&lt;li&gt;Event management module enables admin create upcoming events for display on the live dashboard&lt;/li&gt;
&lt;li&gt;Artist module for managing artists that have music on the platform&lt;/li&gt;
&lt;li&gt;Categories management for adding the category of tasks that can be created&lt;/li&gt;
&lt;li&gt;User Module for managing users on the platform by the admin, the admin can delete a user as well as view a user&lt;/li&gt;
&lt;li&gt;Profile module that enables a user manage his/her profile&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;*Processes&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;Database analysis&lt;/li&gt;
&lt;li&gt;Version control integration with github&lt;/li&gt;
&lt;li&gt;Creation of migrations and seeders&lt;/li&gt;
&lt;li&gt;Template integration for dashboard(Gentelella alella)&lt;/li&gt;
&lt;li&gt;Simple Autodeploy setup from github to test server&lt;/li&gt;
&lt;li&gt;Design of live feed dashboard with HTML and CSS&lt;/li&gt;
&lt;li&gt;Creation of routes and controllers as well as adding functionalities&lt;/li&gt;
&lt;li&gt;Pusher integration on the backend&lt;/li&gt;
&lt;li&gt;Laravel Echo integration for frontend&lt;/li&gt;
&lt;li&gt;Vuejs setup with laravel mix&lt;/li&gt;
&lt;li&gt;Breaking down html and css designs of live feed dashboard into vue components&lt;/li&gt;
&lt;li&gt;adding functionality to components and setting up listeners for pusher events&lt;/li&gt;
&lt;li&gt;Testing of Each module with fake data&lt;/li&gt;
&lt;li&gt;Fixing of discovered bugs&lt;/li&gt;
&lt;li&gt;Deployment&lt;/li&gt;
&lt;/ol&gt;

&lt;h1&gt;
  
  
  Additional Resources/Info
&lt;/h1&gt;

&lt;p&gt;&lt;a href="https://blog.pusher.com/writing-realtime-apps-with-laravel-5-and-pusher/"&gt;https://blog.pusher.com/writing-realtime-apps-with-laravel-5-and-pusher/&lt;/a&gt;&lt;br&gt;
&lt;a href="https://vuejs.org/"&gt;https://vuejs.org/&lt;/a&gt;&lt;br&gt;
&lt;a href="https://laravel.com/docs/5.5"&gt;https://laravel.com/docs/5.5&lt;/a&gt;&lt;br&gt;
All this is placeholder text. Use this format or any other format of your choosing to best describe your creation.&lt;/p&gt;

&lt;h1&gt;
  
  
  Conclusion
&lt;/h1&gt;

&lt;p&gt;Thanks to dev.to and pusher for this opportunity to explore real time apps, This app will later be used for the new technology hub (&lt;a href="https://binaryhills.org"&gt;https://binaryhills.org&lt;/a&gt;) we are setting up for managing tasks and daily activities. &lt;/p&gt;

</description>
      <category>pushercontest</category>
    </item>
    <item>
      <title>Deep dive into middlewares in Laravel </title>
      <dc:creator>Nwanze franklin</dc:creator>
      <pubDate>Wed, 13 Dec 2017 22:48:00 +0000</pubDate>
      <link>https://dev.to/franko4don/deep-dive-into-middlewares-in-laravel-doo</link>
      <guid>https://dev.to/franko4don/deep-dive-into-middlewares-in-laravel-doo</guid>
      <description>&lt;p&gt;Deep dive into middlewares in Laravel&lt;/p&gt;

&lt;p&gt;What is a Laravel middleware?&lt;/p&gt;

&lt;p&gt;It is a feature in Laravel which provides a mechanism for filtering HTTP requests entering your application. This allows you to hook into Laravel request processing work flow to perform some kind of logic that decides how your application works.&lt;/p&gt;

&lt;p&gt;What would you use middleware for?&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Protecting your routes&lt;/li&gt;
&lt;li&gt;Setting headers on HTTP responses&lt;/li&gt;
&lt;li&gt;Logging requests to your application&lt;/li&gt;
&lt;li&gt;Sanitizing incoming parameters&lt;/li&gt;
&lt;li&gt;Enable site-wide maintenance mode&lt;/li&gt;
&lt;li&gt;Manipulating responses generated by your application&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;How to create a custom middleware?&lt;/p&gt;

&lt;p&gt;Creating a middleware in Laravel is as simple as running the following command&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;php artisan make:middleware &amp;lt;MiddlewareName&amp;gt;
//MiddlewareName should be replaced with actual name of the middleware
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;

&lt;p&gt;This creates a middleware with the specified name in the middleware folder located in app. &lt;br&gt;
Luckily, Laravel scaffolds the basic things needed to start customizing your middleware.&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;&amp;lt;?php
namespace App\Http\Middleware;
use Closure;
class RedirectIfSuperAdmin
{
    /**
     * Handle an incoming request.
     *
     * @param  \Illuminate\Http\Request  $request
     * @param  \Closure  $next
     * @return mixed
     */
    public function handle($request, Closure $next)
    {
        return $next($request);
    }
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;

&lt;p&gt;Notice the &lt;code&gt;handle&lt;/code&gt; function which accepts two parameters &lt;code&gt;$request&lt;/code&gt;  and &lt;code&gt;$next&lt;/code&gt; . The &lt;code&gt;$request&lt;/code&gt; parameter holds the incoming request URI in your application while &lt;code&gt;$next&lt;/code&gt; parameter is used to pass the request deeper into the application. The logic needed is written within the handle function and that brings us to types of middleware-&lt;code&gt;before middleware&lt;/code&gt; and &lt;code&gt;after middleware&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;&lt;code&gt;Before middleware&lt;/code&gt; as the name suggests handles some set of logic before forwarding the request deeper into the application. On the other hand &lt;code&gt;after middleware&lt;/code&gt; runs after the request has been handled by the application and the response built.&lt;/p&gt;

&lt;p&gt;Before middleware:&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;&amp;lt;?php
namespace App\Http\Middleware;
use Closure;
class RedirectIfSuperAdmin
{
    /**
     * Handle an incoming request.
     *
     * @param  \Illuminate\Http\Request  $request
     * @param  \Closure  $next
     * @return mixed
     */
    public function handle($request, Closure $next)
    {
        //Your logic goes here
        return $next($request);
    }
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;

&lt;p&gt;After middleware:&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;&amp;lt;?php
namespace App\Http\Middleware;
use Closure;
class RedirectIfSuperAdmin
{
    /**
     * Handle an incoming request.
     *
     * @param  \Illuminate\Http\Request  $request
     * @param  \Closure  $next
     * @return mixed
     */
    public function handle($request, Closure $next)
    {
        $response = $next($request);
        //Your logic goes here e.g return redirect('/)

        return $response;
    }
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;

&lt;p&gt;Categories of Middleware&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Global middleware&lt;/li&gt;
&lt;li&gt;Route middleware&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Global middlewares run for every single request that hits the application. Laravel comes with most of these middlewares such as &lt;code&gt;ValidatePostSize&lt;/code&gt;, &lt;code&gt;TrimStrings&lt;/code&gt;,&lt;code&gt;CheckForMaintenanceMode&lt;/code&gt; etc.&lt;/p&gt;

&lt;p&gt;Route middlewares run only on routes they are attached to e.g &lt;code&gt;redirectIfAuthenticated&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;Registering a Middleware&lt;/p&gt;

&lt;p&gt;Any middleware created has to be registered as that is the only way Laravel knows that such exists. To register a middleware simply open the file named &lt;code&gt;kernel.php&lt;/code&gt; which is located inside Http folder like so:&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fd2mxuefqeaa7sj.cloudfront.net%2Fs_154FD755AC9D5ECE9FD43EEB0CC805AC89CA32D2C242B146361CA35EF3505A84_1507618543348_PrtScr%2Bcapture_2.jpg" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fd2mxuefqeaa7sj.cloudfront.net%2Fs_154FD755AC9D5ECE9FD43EEB0CC805AC89CA32D2C242B146361CA35EF3505A84_1507618543348_PrtScr%2Bcapture_2.jpg" alt="kernel.php location on Laravel"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;This file contains list of all registered middlewares that come with Laravel by default. it contains three major arrays which include &lt;code&gt;$middleware&lt;/code&gt; , &lt;code&gt;$middlewareGroups&lt;/code&gt; and &lt;code&gt;$routeMiddleware&lt;/code&gt;&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;&amp;lt;?php
namespace App\Http;
use Illuminate\Foundation\Http\Kernel as HttpKernel;
class Kernel extends HttpKernel
{
    /**
     * The application's global HTTP middleware stack.
     *
     * These middleware are run during every request to your application.
     *
     * @var array
     */
    protected $middleware = [
        \Illuminate\Foundation\Http\Middleware\CheckForMaintenanceMode::class,
        \Illuminate\Foundation\Http\Middleware\ValidatePostSize::class,
        \App\Http\Middleware\TrimStrings::class,
        \Illuminate\Foundation\Http\Middleware\ConvertEmptyStringsToNull::class,
        \App\Http\Middleware\TrustProxies::class,
    ];
    /**
     * The application's route middleware groups.
     *
     * @var array
     */
    protected $middlewareGroups = [
        'web' =&amp;gt; [
            \App\Http\Middleware\EncryptCookies::class,
            \Illuminate\Cookie\Middleware\AddQueuedCookiesToResponse::class,
            \Illuminate\Session\Middleware\StartSession::class,
            // \Illuminate\Session\Middleware\AuthenticateSession::class,
            \Illuminate\View\Middleware\ShareErrorsFromSession::class,
            \App\Http\Middleware\VerifyCsrfToken::class,
            \Illuminate\Routing\Middleware\SubstituteBindings::class,
        ],
        'api' =&amp;gt; [
            'throttle:60,1',
            'bindings',
        ],
    ];
    /**
     * The application's route middleware.
     *
     * These middleware may be assigned to groups or used individually.
     *
     * @var array
     */
    protected $routeMiddleware = [
        'auth' =&amp;gt; \Illuminate\Auth\Middleware\Authenticate::class,
        'auth.basic' =&amp;gt; \Illuminate\Auth\Middleware\AuthenticateWithBasicAuth::class,
        'bindings' =&amp;gt; \Illuminate\Routing\Middleware\SubstituteBindings::class,
        'can' =&amp;gt; \Illuminate\Auth\Middleware\Authorize::class,
        'guest' =&amp;gt; \App\Http\Middleware\RedirectIfAuthenticated::class,
        'throttle' =&amp;gt; \Illuminate\Routing\Middleware\ThrottleRequests::class,
        //the just created middlware
        'superadmin' =&amp;gt; \App\Http\Middleware\RedirectIfSuperAdmin::class, 
    ];
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;

&lt;p&gt;The &lt;code&gt;$middleware&lt;/code&gt; array holds global middlewares which runs for every single HTTP request to the application, so if you want a middleware to run for every request you should register it here. The &lt;code&gt;$middlewareGroups&lt;/code&gt; makes it possible to register middlewares in groups making it easier to attach lots of middlewares to a route by using the group name. The &lt;code&gt;$routeMiddleware&lt;/code&gt; array holds individual registered middlewares.&lt;/p&gt;

&lt;p&gt;Assigning a Middleware&lt;/p&gt;

&lt;p&gt;Once a middleware is registered it can be attached to a route in two major ways&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Through the constructor method in a controller&lt;/li&gt;
&lt;li&gt;Through the route &lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Middleware assignment through constructor&lt;/p&gt;

&lt;p&gt;Middleware assignment via a constructor on a controller gives more flexibility as it offers two important functions &lt;code&gt;except($parameters)&lt;/code&gt; and &lt;code&gt;only($parameters)&lt;/code&gt; which can be used to prevent or allow the middleware to apply to some helper functions in that controller. Without using the helper functions the middleware applies to every single function on that controller.&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;&amp;lt;?php
use Illuminate\Http\Request;

class ForumController extends Controller
{

    public function __construct(){
      /**in this case the middleware named auth is applied
       to every single function within this controller
       */
        $this-&amp;gt;middleware('auth');
    }

    public function viewForum(){

      return view('index');
    }

    public function edit($id){

    }

    public function delete($id){

    }

}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;

&lt;p&gt;With the &lt;code&gt;except&lt;/code&gt; and &lt;code&gt;only&lt;/code&gt; functions we can select which functions the middleware will apply to as shown below:&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;&amp;lt;?php
use Illuminate\Http\Request;

class ForumController extends Controller
{

    public function __construct(){
      /**the authentication middleware here applies to all functions but
       viewForums() and viewForumDetails() and the opposite of this happens
       when you use only()
       */
        $this-&amp;gt;middleware('auth')-&amp;gt;except(['viewForums', 'viewForumDetails']);
    }

    public function viewForums(){

      return view('index');
    }

    public function edit($id){

    }

    public function delete($id){

    }

    public function viewForumDetails(){

    }
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;

&lt;p&gt;Middleware assignment through routes&lt;/p&gt;

&lt;p&gt;Provided a middleware has been registered in can be attached to the route directly as show below:&lt;/p&gt;

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

/**method 2
or using the fully qualified class name like so:
*/
use App\Http\Middleware\CheckAge;

Route::get('admin/profile', function () {
    // action
})-&amp;gt;middleware(CheckAge::class);

//method 3
Route::group(['middleware' =&amp;gt; ['web']], function () {
    //action
});
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;

&lt;p&gt;N:B Middleware groups can be assigned to a route the same way as a single middleware&lt;/p&gt;

&lt;p&gt;Middleware Parameters&lt;/p&gt;

&lt;p&gt;Additional parameters can be passed to a middleware. A typical example is where each user id is assigned to a role and the middleware checks the role of a user to determine if he/she has access to the requested URI. Parameters can be passed to a middleware as shown below:&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;&amp;lt;?php
//First method (Through route)
Route::get('admin/profile', function () {
  //action
})-&amp;gt;middleware('auth:&amp;lt;role&amp;gt;'); //&amp;lt;role&amp;gt; here should be replaced by whatever parameter the user intends to pass.

//Second method (Through a controller)
use Illuminate\Http\Request;

class ForumController extends Controller
{

    public function __construct(){
        $this-&amp;gt;middleware('auth:&amp;lt;role&amp;gt;');
    }
  }
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;

&lt;p&gt;Multiple parameters can be passed to a middleware by separating each parameter by a comma.&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;&amp;lt;?php
Route::get('admin/profile', function () {
  //action
})-&amp;gt;middleware('auth:&amp;lt;role&amp;gt;,&amp;lt;age&amp;gt;,&amp;lt;country&amp;gt;'); //&amp;lt;role&amp;gt;, &amp;lt;age&amp;gt;, &amp;lt;country&amp;gt; here should be replaced by whatever parameters the user intends to pass.
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;

&lt;p&gt;These parameters are passed to the handle function of the middleware after the &lt;code&gt;$next&lt;/code&gt; variable&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;&amp;lt;?php
class RedirectIfSuperAdmin
{
    /**
     * Handle an incoming request.
     *
     * @param  \Illuminate\Http\Request  $request
     * @param  \Closure  $next
     * @return mixed
     */
    public function handle($request, Closure $next, $role, $age, $country)
    {   
        //Logic for the middleware using the parsed parameters
        return $next($request);
    }
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;

&lt;p&gt;Summary&lt;/p&gt;

&lt;p&gt;To create a middleware you go through the following processes&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Create the middleware with artisan command &lt;code&gt;php artisan make:middleware &amp;lt;Middleware Name&amp;gt;&lt;/code&gt;.&lt;/li&gt;
&lt;li&gt;Register the middleware in kernel.php located in the app→Http folder&lt;/li&gt;
&lt;li&gt;Write your logic in the created middleware&lt;/li&gt;
&lt;li&gt; Assign middleware to a route or controller&lt;/li&gt;
&lt;/ul&gt;

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

&lt;p&gt;Laravel middlewares make it easier to protect our routes, sanitize input and do a whole lot of other stuffs without writing so much logic. Checkout the official Laravel documentation &lt;a href="https://laravel.com/docs/5.5/middleware" rel="noopener noreferrer"&gt;here&lt;/a&gt; for more features of middlewares and most importantly practice.&lt;/p&gt;

</description>
      <category>laravel</category>
      <category>php</category>
      <category>programming</category>
      <category>software</category>
    </item>
  </channel>
</rss>
