atayahmet/laravel-castable 问题修复 & 功能扩展

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

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

atayahmet/laravel-castable

Composer 安装命令:

composer require atayahmet/laravel-castable

包简介

Clarify types of all inputs

README 文档

README

Build Status Total Downloads License Latest Stable Version

Laravel Castable

Laravel Castable package is a type converter, input filter or sanitizer. It is possible to do all of these operations. Supported POST, RAW DATA, GET requests methods. We started by inspiring the Laravel Eloquent data cast.

Requirements

PHP 5.6, 7.0+ Laravel 5.3 (LTS) or Laravel 5.4 (Current)

Get Started

Firstly, we install package:

$ composer require atayahmet/laravel-castable

and then we need add the service provider to the app.php

Castable\CastableServiceProvider::class

OK, we done.

Let's see how to use the laravel-castable.

Castable types

Types
string
integer
boolean
float
double
real
unset
array
object (stdClass)
collection

Create Castable Form Request class

We created new artisan command that inspired make:request from laravel built in command.

$ php artisan make:cast ContactRequest

New form of the form request class:

<?php

namespace App\Http\Requests;

use Castable\Castable;

class ContactRequest extends Castable
{
    protected $casts = [
        'json' => [
            //
        ],
        'post'  => [
            'name' => 'string',
            'age' => 'integer',
            'student' => 'boolean',
            'interests' => 'collection'
        ],
        'query' => [
            'save' => 'boolean'
        ]
    ];

    /**
     * Determine if the user is authorized to make this request.
     *
     * @return bool
     */
    public function authorize()
    {
        return false;
    }

    /**
     * Get the validation rules that apply to the request.
     *
     * @return array
     */
    public function rules()
    {
        return [
            //
        ];
    }
}

We added four inputs to casts property, status attibute added to query string parameters and age, student and interests attributes added to post parameters.

Raw and converted type of the attributes

Post:

Name Value Type Cast Type
name Ali string string
age 19 string integer
student true string boolean
interests books, computers array collection

Query String:

Name Value Type Cast Type
save true string boolean

To get the above result for:

<?php

ContactController extends Controller {

  public index(ContactRequest $contactRequest)
  {
      $contactRequest->cast()->input();
  }
}

Get a input:

$contactRequest->cast()->input('interests'); // collection

$contactRequest->cast()->input('student') // boolean (true)

$contactRequest->cast()->input('save') // boolean (true)

if request is post raw data:

$contactRequest->cast()->json();

$contactRequest->cast()->json('age');

Get original inputs:

$contactRequest->input();

Get original an input:

$contactRequest->input('student'); // string (true)

Original raw data:

$contactRequest->json();

Add presenter to the inputs

You can add presenter to the all post, query and json inputs. This feature gives you a chance to filter inputs.

Add presenter for post parameters:

public function PostNameAttribute($value, $original)
{
    return ucfirst($value);
}

Add presenter for query string parameters:

public function QuerySaveAttribute($value, $original)
{
    return $value === true ? 1 : 0;
}

Add presenter for json raw data parameters:

public function JsonSaveAttribute($value, $original)
{
    return ucfirst($name);
}

License

This package is open-source software licensed under the MIT license.

统计信息

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

GitHub 信息

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

其他信息

  • 授权协议: MIT
  • 更新时间: 2017-02-24

承接程序开发

PHP开发

VUE

Vue开发

前端开发

小程序开发

公众号开发

系统定制

数据库设计

云部署

网站建设

安全加固