Describes how pipes are actually used from a code perspective. The description of the pipeline, there is more space on the Internet to introduce, check yourself. This blog is about using pipes to process names for unified purposes.

Background: There are a lot of introductions to using pipes that you can find so far. Most of them are just introductions and guides, and few really go into the code. According to the introduction, the use of pipes also has certain obstacles, here to share a detailed code example on the use of pipes, for reference only. This introduction is a real use of their own process code excerpts, personally tested, real usable. Don’t spray if you don’t like it.

I. Controller

Router section

Route::get('/pipe'['as'= >'pipe'.'uses'= >'PipeController@index']);
Copy the code

Control code


      

namespace App\Http\Controllers;

use App\Pipes\LeftWords;
use App\Pipes\RightWords;
use App\Pipes\BothSidesWords;
use Illuminate\Http\Request;
use Illuminate\Pipeline\Pipeline;
use App\User;
use Illuminate\Support\Str;
use Illuminate\Support\Facades\Hash;

class PipeController extends Controller
{
    /* Define the pipe * * first step * second step * third step * */
    protected $pipes = [
        LeftWords::class,
        RightWords::class,
        BothSidesWords::class,
    ];
    / / home page
    public function index(Request $request){
        $name = $request->input('name');
        // $name = Str::random(10);

        return app(Pipeline::class)
            ->send($name)
            ->through($this->pipes)
            ->then(function ($content) {
                return User::create([
                    'name'= >$content.'email'=>Str::random(10).'@gmail.com'.'password'=>Hash::make('password'),]); }); }}Copy the code

Two, the pipeline part

The directory structure is as follows:

│ ├─app │ ├─Http │ ├─ PHP │ ├ _... │ │ │ ├ ─ Models │ │... │ │ │ ├ ─ Pipes │ │ │ BothSidesWords. PHP │ │ │ LeftWords. PHP │ │ │ RightWords. PHP │ │ │ │ │ └ ─ Contracts │ │ PipeContracts.phpCopy the code
  1. interfaceCode path toapp/Pipes/Contracts/Pipe.phpThe code below looks like this:
    
            
    namespace App\Pipes\Contracts;
    
    use Closure;
    
    interface PipeContracts
    {
        public function handle($body.Closure $next);
    }
    
    Copy the code
  2. The code for the classes of the three pipesLeftWords.phpThe code of
    
            
    namespace App\Pipes;
    
    use App\Pipes\Contracts\PipeContracts;
    use Closure;
    
    class LeftWords implements PipeContracts{
        public function handle($body.Closure $next)
        {
            // TODO: Implement handle() method.
    
            $body = 'left-'.$body;
    
            return $next($body); }}Copy the code

    LeftWords.phpThe code of

    
            
    namespace App\Pipes;
    
    use App\Pipes\Contracts\PipeContracts;
    use Closure;
    
    class RightWords implements PipeContracts{
        public function handle($body.Closure $next)
        {
            // TODO: Implement handle() method.
    
            $body = $body.'-right';
    
            return $next($body); }}Copy the code

    BothSidesWords.phpThe code of

    
            
    namespace App\Pipes;
    
    use App\Pipes\Contracts\PipeContracts;
    use Closure;
    
    class BothSidesWords implements PipeContracts{
        public function handle($body.Closure $next)
        {
            // TODO: Implement handle() method.
    
            $body = '['.$body.'] ';
    
            return $next($body); }}Copy the code

Here we use the pipe’s default method, Handle, and you can customize the method name. Define myHandleMethod as the handler name as follows.

return app(Pipeline::class)
	       ->send($name)
	       ->through($this->pipes)
	       ->via('myHandleMethod')
	       ->then(function ($content) {
	           return User::create([
	               'name'= >$content.'email'=>Str::random(10).'@gmail.com'.'password'=>Hash::make('password'),]); });Copy the code

Once you’ve defined it this way, change your interface and change your implementation class.

3. Explanation of results

Go to http://localhost/pipe? After name= LISA, the obtained results can be successfully printed. Data in the User table is saved successfully. Procedure

{
"name": "[left-lisa-right]"."email": "[email protected]"."updated_at": "The 2020-09-05 T05: every 000000 z"."created_at": "The 2020-09-05 T05: every 000000 z"."id": 15
}
Copy the code