sorokin-fm/laravel-request-sanitizer 问题修复 & 功能扩展

解决BUG、新增功能、兼容多环境部署,快速响应你的开发需求

邮箱:yvsm@zunyunkeji.com | QQ:316430983 | 微信:yvsm316

sorokin-fm/laravel-request-sanitizer

Composer 安装命令:

composer require sorokin-fm/laravel-request-sanitizer

包简介

Laravel Request Sanitizer

README 文档

README

We all know that the thinner is controller the better it is. And we wanted to have such controllers:

    public function update(UpdateRequest $request, $id)
    {
        /** @var SomeModel $model */
        $model = SomeModel::find($id);
        $model->fill($request->input());
        $model->save();

        return back()
            ->with('success', __('Some model has been changed'));
    }

Unfortunately, there's a couple of cases, where we just can't directly pass inputs to fill.

For example, some fields are checkboxes. So we need to do a little pre-processing:

$values = $request->input();
$values['checkbox1'] = $values['checkbox1'] ? 1 : 0;
$values['checkbox2'] = $values['checkbox2'] ? 1 : 0;
$model->fill($values);

Another scenario is when we need to upload files:

$values = $request->input();

$file = $request->file('picture');
if ($file) {
    $filename = Transliteration::make(
        pathinfo($file->getClientOriginalName(), PATHINFO_FILENAME),
        ['type' => 'filename', 'lowercase' => true]);

    $filename .= '.' . $file->getClientOriginalExtension();

    $file->move($filePath, $filename);

    $values['picture'] = $filename;
}

$file = $request->file('thumb');
if ($file) {
    $filename = Transliteration::make(
        pathinfo($file->getClientOriginalName(), PATHINFO_FILENAME),
        ['type' => 'filename', 'lowercase' => true]);

    $filename .= '.' . $file->getClientOriginalExtension();

    $file->move($filePath, $filename);

    $values['thumb'] = $filename;
}

$model->fill($values);

As you see, it's not very thin, yes? And the worst - we need to duplicate that code in store and update actions. So what can we do with all of this to make it better?

For - add this package:

compose require "sorokin-fm/laravel-request-sanitizer"

And then, add it to you request class:

use Illuminate\Foundation\Http\FormRequest;
use SorokinFM\RequestSanitizerTrait;

class StoreRequest extends FormRequest

    use RequestSanitizerTrait;
    const SANITIZE_RULES = [
        'enabled' => 'checkbox',
        'picture' => 'file:path/to/store',
    ];

    ...

Now you're ready to use it:

    public function update(UpdateRequest $request, $id)
    {
        /** @var SomeModel $model */
        $model = SomeModel::find($id);
        $model->fill($request->sanitize());
        $model->save();

        return back()
            ->with('success', __('Some model has been changed'));
    }

Of course, if you need to define your own sanitizer, you can implement interface SorokinFM\SanitizerInterface and then specify full class name ( with namespace ) as rule name, for example:

    use RequestSanitizerTrait;
    const SANITIZE_RULES = [
        'enabled' => 'checkbox',
        'picture' => 'file:path/to/store',
        'custom_data' => '\App\Sanitizers\CustomRequestSanitizer',
    ];

And one more thing. You also can use wildcards at field names. It will be useful at localization, when you don't know all names of field:

    use RequestSanitizerTrait;
    const SANITIZE_RULES = [
        'enabled' => 'checkbox',
        'picture:*' => 'file:path/to/store',
        'custom_data' => '\App\Sanitizers\CustomRequestSanitizer',
    ];

统计信息

  • 总下载量: 30
  • 月度下载量: 0
  • 日度下载量: 0
  • 收藏数: 0
  • 点击次数: 0
  • 依赖项目数: 0
  • 推荐数: 0

GitHub 信息

  • Stars: 0
  • Watchers: 0
  • Forks: 0
  • 开发语言: PHP

其他信息

  • 授权协议: Unknown
  • 更新时间: 2019-01-06

承接程序开发

PHP开发

VUE

Vue开发

前端开发

小程序开发

公众号开发

系统定制

数据库设计

云部署

网站建设

安全加固