标签:f4v UNC mail support method ret 首页 标记 结构
从代码的角度介绍管道的实际使用方式。有关管道的说明,网上已有较多的篇幅介绍,自行查阅。
本篇博客是使用管道处理名字, 实现统一处理的目的。
<?php
namespace App\Http\Controllers;
use App\Pipes\RemoveBadWords;
use App\Pipes\RemoveScriptTags;
use App\Pipes\ReplaceLinkTags;
use Illuminate\Http\Request;
use Illuminate\Pipeline\Pipeline;
use App\User;
use Illuminate\Support\Str;
use Illuminate\Support\Facades\Hash;
class PipeController extends Controller
{
    /* 定义管道
     *
     * 用纯文本替换链接标记;
     * 用 “*” 替换敏感词;
     * 从内容中完全删除脚本标记
     * */
    protected $pipes = [
        RemoveBadWords::class,
        RemoveScriptTags::class,
        ReplaceLinkTags::class,
    ];
    // 首页
    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‘),
                ]);
            });
    }
}
目录结构如下:
├─app
│  │  User.php
│  ├─Http
│  │  ...
│  │
│  ├─Models
│  │  ...
│  │
│  ├─Pipes
│  │  │  RemoveBadWords.php
│  │  │  RemoveScriptTags.php
│  │  │  ReplaceLinkTags.php
│  │  │
│  │  └─Contracts
│  │          Pipe.php
interface的代码app/Pipes/Contracts/Pipe.php下的代码如下:<?php
namespace App\Pipes\Contracts;
use Closure;
interface Pipe
{
    public function handle($content, Closure $next);
}
RemoveBadWords.php的代码<?php
namespace App\Pipes;
use App\Pipes\Contracts\Pipe;
use Closure;
class RemoveBadWords implements Pipe{
    public function handle($content, Closure $next)
    {
        // TODO: Implement handle() method.
        $content = ‘left-‘.$content;
        return $next($content);
    }
}
RemoveScriptTags.php的代码<?php
namespace App\Pipes;
use App\Pipes\Contracts\Pipe;
use Closure;
class RemoveScriptTags implements Pipe{
    public function handle($content, Closure $next)
    {
        // TODO: Implement handle() method.
        $content = $content.‘-right‘;
        return $next($content);
    }
}
ReplaceLinkTags.php的代码<?php
namespace App\Pipes;
use App\Pipes\Contracts\Pipe;
use Closure;
class ReplaceLinkTags implements Pipe{
    public function handle($content, Closure $next)
    {
        // TODO: Implement handle() method.
        $content = ‘[‘.$content.‘]‘;
        return $next($content);
    }
}
这里我们使用管道默认的方法handle,你可以自定义方法名。像下面这样定义customMethodName为处理方法名称。
return app(Pipeline::class)
	       ->send($name)
	       ->through($this->pipes)
	       ->via(‘customMethodName‘)
	       ->then(function ($content) {
	           return User::create([
	               ‘name‘ => $content,
	               ‘email‘=>Str::random(10).‘@gmail.com‘,
	               ‘password‘=>Hash::make(‘password‘),
	           ]);
	       });
你这样定义后,修改你的interface,同时修改你的实现类即可。
能成功打印出获取的结果。User表内部,有数据保存成功。
{
"name": "[left-F4VYGWbHLi-right]",
"email": "iMPQr6F7ri@gmail.com",
"updated_at": "2020-09-05T02:43:15.000000Z",
"created_at": "2020-09-05T02:43:15.000000Z",
"id": 13
}
标签:f4v UNC mail support method ret 首页 标记 结构
原文地址:https://www.cnblogs.com/hxsen/p/13618912.html