When we build a system, then many times we think about which design pattern is good to use. There are so many design patterns in the PHP Laravel application. The pipeline design pattern is one of them. In this tutorial, we will have a look at what is the pipeline in Laravel.

The Pipeline design pattern is where a complex system is broken into smaller and individual tasks. Each individual task is completely reusable. This allows us to break up monolithic processes into smaller tasks that process data and then pass that data to the next step. Each job within the pipeline should receive and emit the same data. This pipeline allows tasks to be added, removed, or replaced from the pipeline without disturbing the other tasks.

laravel-pipeline-design-pattern

Let's see the pattern of the Pipeline pattern:

<?php

return app(Pipeline::class)
                ->send()
                ->through([
                    
                ])
                ->thenReturn();

 

First, visit this Laravel Pipeline link, then you will understand clearly what is happening here. Look at that, Pipeline has three parts like 

$pipeline->send($request) then $pipeline->through($middleware) and then $pipeline->thenReturn($request). The pipeline send() method accepts and sends the requested data and then using trough(), it will accept the individual task and then it will return the response.

 

Read also: Why And When To Use Interface In Laravel Application

 

In this tutorial, we will see a complete real-life example of the Pipeline design pattern. In this real life Laravel pipeline example, I will create a social share service using the pipeline design pattern.

So first, let's create an abstract class called Share like that:

App\Services\Share.php

<?php 

namespace App\Services;

use Closure;

abstract class Share 
{
    public function handle($request, Closure $next)
    {  
        $builder = $next($request);

        return $this->applyShare($builder);
    }

    protected abstract function applyShare($builder);
}

 

Now create a SocialShareService class like that:

App\Services\SocialShareService.php

<?php

namespace App\Services;

use Illuminate\Pipeline\Pipeline;
use App\Services\ShareTumblr;
use App\Services\ShareFacebook;

class SocialShareService
{   
    public function share($data = [])
    {   
        $response = app(Pipeline::class)
                        ->send($data)
                        ->through([
                            ShareFacebook::class,
                            ShareTumblr::class
                        ])
                        ->thenReturn();

        return $response;
    }
}

 

Look at that, here we do exactly the same thing which I discussed above. Now create ShareFacebook and ShareTumblr class like:

App\Services\ShareFacebook.php

<?php 

namespace App\Services;

use App\Services\Share;

class ShareFacebook extends Share
{
    public function applyShare($data)
    {   
       //code goes here for Facebook Share
    }
}

 

And another one for Tumblr share:

App\Services\ShareTumblr.php

<?php 

namespace App\Services;

use App\Services\Share;

class ShareTumblr extends Share
{
    public function applyShare($data)
    {   
       //code goes here for Tumblr Share
    }
}

 

Now use this in your controller like that to share your auto post on social site:

App\Http\Controllers\PostController.php

<?php

use Illuminate\Http\Request;
use App\Http\Controllers\Controller;
use App\Services\SocialShareService as SocialShare;

class PostController extends Controller
{   
    public function storePost(SocialShare $socialShare, Request $request)
    {   
        $data = [
            'link' => $request->permalink(),
            'message' => $request->title
        ];

        $socialShare->share($data);
    }
}

 

Now the question is, what is the advantage of this design pattern? Look at that, assume we are going to create a new share system like Linkedin share. Just do it:

App\Services\SocialShareService.php

<?php

namespace App\Services;

use Illuminate\Pipeline\Pipeline;
use App\Services\ShareTumblr;
use App\Services\ShareFacebook;
use App\Services\LinkedinShare;

class SocialShareService
{   
    public function share($data = [])
    {   
        $response = app(Pipeline::class)
                        ->send($data)
                        ->through([
                            ShareFacebook::class,
                            ShareTumblr::class,
                            LinkedinShare::class
                        ])
                        ->thenReturn();

        return $response;
    }
}

 

And write your login inside the Linkedin share class like before:

App\Services\LinkedinShare.php

<?php 

namespace App\Services;

use App\Services\Share;

class LinkedinShare extends Share
{
    public function applyShare($data)
    {   
       //code goes here for Facebook Share
    }
}

 

Look at that, we do not do anything inside other class codes and data. We have to just work with our new class and just have to be added this new class in Pipeline.

 

Read also: Laravel Service Repository Pattern Tutorial

 

Conclusion

What is pipeline in laravel? Now hope you know the concept. This is the use case of the laravel pipeline example. In this example, we have seen laravel pipeline with parameters, and also laravel pipeline send multiple parameters. Cause we have passed multiple classes with the through method. Hope this pipeline design pattern in laravel tutorial will help you.