stahiralijan / request-caster
此包在验证成功后拦截 Laravel 表单提交,并提供有用的功能,并将提交的表单数据相应地转换,例如单词的首字母大写、首字母大写、复选框值 "1" 转换为布尔值、JSON 字符串转换为 PHP 数组等。
1.1.0
2018-01-15 10:59 UTC
Requires
- php: >=7.0
- illuminate/support: ~5.1
Requires (Dev)
- php: >=7.0
- illuminate/support: ~5.1
This package is auto-updated.
Last update: 2024-09-14 19:47:42 UTC
README
需求:我仅在 Laravel 5.5 中测试了这个包,请帮助我在旧版本的 Laravel 中测试此包
安装
通过以下命令安装此包
composer require stahiralijan/request-caster
使用方法
让我们从一个例子中学习
您想能够保存提交的数据,但又不希望在控制器方法中造成混乱,如下所示
public function store(UserFormRequest $request) { ... $first_name = ucfirst($request->first_name); // or ucfirst($request->get('first_name') $last_name = ucfirst($request->last_name); // or ucfirst($request->get('last_name') ... $user = User::create([ ... 'first_name' => $first_name, 'last_name' => $last_name, ... ]); ... // after handling model stuff return redirect(route('users.index')) ->with('message'=>"User ({$user->first_name} {$user->last_name}) created!"); }
如您所见,不久之后,您开始思考是否有办法自动化这个过程,这样您的控制器看起来就会既优雅又干净。使用这个包,您可以做到这一点
步骤 1
在您的表单请求(在本例中为 UserFormRequest
)中使用 RequestCaster
特性
... use Stahiralijan\RequestCaster\Traits\RequestCasterTrait; ... class UserFormRequest extends FormRequest { use RequestCasterTrait; ... }
步骤 2
定义需要转换的请求属性
class UserFormRequest extends FormRequest { use RequestCasterTrait; protected $toUCFirstWords = ['first_name','last_name']; // More about this is explained below protected $joinStrings = ['fullname'=>' |first_name,last_name']; ... }
最后
...这就是您需要做的全部,first_name
和 last_name
将自动首字母大写。另外,您不需要担心在验证之前您的表单数据会变脏,因为这些转换将在验证器验证表单数据之后运行。
public function store(UserFormRequest $request) { // first_name and last_name $user = User::create($request->all()); ... // after handling model stuff return redirect(route('users.index')) ->with('message'=>"User ({$request->full_name}) created!"); }
可用的转换/转换
以下转换可用
$toLowerCaseWords
:将strtolower()
应用到所选字段(s)。$toUpperCaseWords
:将strtoupper()
应用到所选字段(s)。$toUCFirstWords
:将ucwords()
应用到所选字段(s)。$toSlugs
:将str_slug()
应用到所选字段(s)。$toIntegers
:将所选字段(s)转换为int
。$toFloats
:将所选字段(s)转换为float
。$toBooleans
:将所选字段(s)转换为bool
。$toArrayFromJson
:将json_decode()
应用到所选字段。$joinStrings
:连接两个或多个字段,并将结果设置在新字段中,键语法:$joinStrings = ['newField' => 'glue|field1,field2,...,fieldn']
可用的方法
collection(array $keys)
返回一个Illuminate\Support\Collection
对象dd()
输出并终止请求中提交的所有字段dump()
输出请求中提交的所有字段
您可以使用此方法获取所有属性的集合(Illuminate\Support\Collection
)
public function store(UserFormReques $request) { $request->collection()->filter(function($item){ ... }); // or $request->collection()->map(function($item){ ... }); }
如何转换
所有属性都很直观,您定义需要转换的属性如下
// Convert the defined attributes to Upper-case $toUpperCaseWords = ['product_code']; // Upper-case the first letter of the words defined below $toUCFirstWords = ['display_name']; // Convert the following attributes into slugs $toSlugs = ['product_name'];
您已经了解了简单用法,现在来看一个特殊的转换/转换器
$joinStrings = ['fullname'=>' |first_name,last_name'];
- 在这里,
fullname
将成为FormRequest
的新属性,在当前上下文中既不在表单中也不在FormRequest
中存在。 - 注意值开头的空格
' '
是两个属性的粘合剂 - 接下来是
|
,它是粘合剂和所需属性之间的分隔符 - 然后您添加需要粘合的属性。
如果 first_name
是 Tahir
而 last_name
是 Jan
,则输出将是 Tahir Jan
,根据上述规则,并且可以通过 $request->fullname
或 $request->get('fullname')
访问