PHP code example of jimb / restfilter

1. Go to this page and download the library: Download jimb/restfilter library. Choose the download type require.

2. Extract the ZIP file and open the index.php.

3. Add this code to the index.php.
    
        
<?php
require_once('vendor/autoload.php');

/* Start to develop here. Best regards https://php-download.com/ */

    

jimb / restfilter example snippets



use League\Fractal\Manager;

// 新建一个全局管理类
// 这个管理类可以是单一的,放在全局的
$fractal = new Manager();





ague\Fractal\Manager;
use League\Fractal\Resource\Collection;
use Jimb\RestFilter\CommonTransformer;

// 新建一个全局管理类
$fractal = new Manager();

$companys = [
    [
        'id' => '1',
        'name' => 'jianbin',
        'jgdaima' => 'WBERBKJKJEWR',
        'date' => '1998',
        'adresss' => '广州的某某小吃店',
        'email' => '[email protected]',
    ],
    [
        'id' => '2',
        'name' => 'jimb55',
        'jgdaima' => 'FHEWIH3274WENR234NE',
        'date' => '1994',
        'adresss' => '北方的最高雪山',
        'email' => '[email protected]',
        'myinfo' => [
            'name' => 'jimb55',
            'githubPage' => 'github://jimb55',
            'age' => '22',
            'like' => 'i down no'
        ]
    ],
    [
        'id' => '3',
        'name' => '糖糖',
        'jgdaima' => 'FHEWIH3274WENR234NE',
        'date' => '1994',
        'adresss' => '东方最热的温泉',
        'email' => '[email protected]',
        'myinfo' => [
            'name' => 'jjjj',
            'githubPage' => 'hello://world',
            'age' => '22',
            'like' => 'i 222 no'
        ]
    ]
];

$resource = new Collection($companys, new CommonTransformer);

// 返回
echo $fractal->createData($resource)->toJson();




eague\Fractal\Manager;
use League\Fractal\Resource\Item;
use Jimb\RestFilter\CommonTransformer;

// 新建一个全局管理类
$fractal = new Manager();

$companys = [
    'id' => '1',
    'name' => 'jianbin',
    'jgdaima' => 'WBERBKJKJEWR',
    'date' => '1998',
    'adresss' => '广州的某某小吃店.',
    'email' => '[email protected]',
];


//单项
$resource = new Item($companys, new CommonTransformer);

// 返回
echo $fractal->createData($resource)->toJson();



e __DIR__ . "/customTransformer.php";

use Jimb\RestFilter\FractalAdapter;
$companys = [...];
echo FractalAdapter::getInstance() -> collection()-> toJson($companys);
//单项
//echo FractalAdapter::getInstance() -> item()-> toJson($companys);


namespace Jimb;

use Jimb\RestFilter\CommonTransformer;

/**
 * CommonTransformer 过滤转化器
 * Class CommonTransformer
 */
class customTransformer extends CommonTransformer
{
    //覆盖父类的 $filedName 为过滤参数的参数名
    protected $filedName = "custom";

    /**
     * @param $model
     * @return array
     */
    public function transform($model)
    {

        // $model是一个item 就是你要过滤的返回数据
        // 在过滤前你可以改变 $model 的任何结构
        // 但注意不要因为结构改变了导致$filed 字段找不到值,是重构结构了才会过滤的

        return parent::transform([
            'id' => $model["id"]."+".$model["name"],
            'name' => "my name is ".$model["name"],
        ]);
    }
}


return app('RestFilter') -> collection() ->toJson(Product::orderBy ( 'id', 'desc' )-> get() -> toArray());