tsmsogn / datafilter
CakePHP 插件:将回调应用于所有或特定请求数据
dev-master
2016-04-25 02:19 UTC
This package is auto-updated.
Last update: 2024-08-29 04:14:35 UTC
README
将回调应用于所有或特定请求数据
要求
- CakePHP 2.x
安装
将您的应用程序插件目录命名为 Datafilter
。
启用插件
在 2.0 版本中,您需要在您的 app/Config/bootstrap.php 文件中启用插件
<?php CakePlugin::load('Datafilter', array('bootstrap' => false, 'routes' => false)); ?>
用法
在控制器中
<?php App::uses('AppController', 'Controller'); class PostsController extends Controller { public $components = array('Datafilter.Datafilter'); public function foo() { // Applies strtoupper() for all values /* $this->request->data looks like: Array ( [Post] => Array ( [key] => value ) ) */ $this->Datafilter->applyFilter('__ALL__', 'strtoupper'); /* $this->request->data looks like: Array ( [Post] => Array ( [key] => VALUE ) ) */ } public function bar() { // Applies strtoupper() for specific field(s) /* $this->request->data looks like: Array ( [Post] => Array ( [key1] => value1 [key2] => value2 ) ) */ $this->Datafilter->applyFilter('Post.key1', 'strtoupper'); /* $this->request->data looks like: Array ( [Post] => Array ( [key1] => VALUE1 [key2] => value2 ) ) */ } public function baz() { // Applies user function for specific field(s) with {}'s /* $this->request->data looks like: Array ( [Post] => Array ( [key1] => value1 ) [Tag] => Array ( [0] => Array ( [name] => value2 ) [1] => Array ( [name] => value3 ) ) ) */ $this->Datafilter->applyFilter(array('Tag.{n}.name'), function($value) { return 'prefix_' . $value; }); /* $this->request->data looks like: Array ( [Post] => Array ( [key1] => value1 ) [Tag] => Array ( [0] => Array ( [name] => prefix_value2 ) [1] => Array ( [name] => prefix_value3 ) ) ) */ } }
许可证
MIT 许可证 (MIT) tsmsogn