如何使用接受修身所有REST URI的排序和分页参数? [英] How to accept sorting and pagination parameters in all REST URIs using Slim?

查看:296
本文介绍了如何使用接受修身所有REST URI的排序和分页参数?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我现在用的超薄的PHP框架来创建我的应用程序一个RESTful API。我想所有的URL能够接受排序和分页参数。谁能告诉我做这件事的最佳方式?

此外,有人可以给我提供一些适当的休息URI来呢? (即 http://domain.com/api/类/果/排序= DESC&放大器;结果= 25安培;页= 2

 < PHP需要'修身/ Slim.php';$排序=ASC;
$结果= 10;
$页= 1;$应用=新修身();$ APP-GT&;得到('/酒',()的函数使用($应用程序){
  $排序= $ APP-GT&;请求() - GT; PARAMS('排序');
  $结果= $ APP-GT&;请求() - GT; PARAMS('结果');
  $页= $ APP-GT&;请求() - GT; PARAMS(页面');  getWines();
});$ APP-GT&;得到('/类别',()的函数使用($应用程序){
  $排序= $ APP-GT&;请求() - GT; PARAMS('排序');
  $结果= $ APP-GT&;请求() - GT; PARAMS('结果');
  $页= $ APP-GT&;请求() - GT; PARAMS(页面');  getCategories();
});$ APP-GT&;得到('/子类别,()的函数使用($应用程序){
  $排序= $ APP-GT&;请求() - GT; PARAMS('排序');
  $结果= $ APP-GT&;请求() - GT; PARAMS('结果');
  $页= $ APP-GT&;请求() - GT; PARAMS(页面');  getSubCategories();
});$ APP->运行();功能getWines(){
  $ SQL =SELECT * FROM酒ORDER BY名。 $排序。 限制。 $页。 $结果;
  尝试{
    $ DB =的getConnection();
    $语句= $ DB-GT&;查询($的SQL);
    $葡萄酒= $ stmt->使用fetchall(PDO :: FETCH_OBJ);
    $ DB = NULL;
    回声{酒:。 json_en code($葡萄酒)。 };
  }赶上(PDOException $ E){
    回声{错误:{文:。 $ E-GT&;的getMessage()}}。
  }
}?>


解决方案

有许多办法来解决这个问题,我会建议使用的模板方法模式,让你定义在父类的共同行为,而在子类处理的具体细节。

 抽象类SortPageHandler {
  公共职能getUrlHandler($应用程序)
  {
    $我= $这一点;
    返回功能()使用($应用程序,$我){
      $排序= $ APP-GT&;请求() - GT; PARAMS('排序');
      $结果= $ APP-GT&;请求() - GT; PARAMS('结果');
      $页= $ APP-GT&;请求() - GT; PARAMS(页面');      $ APP-GT&;响应() - GT;写($我 - > getItems($排序,结果$,$页));
    };
  }  抽象的公共职能getItems($排序,结果$,$页);
}类WineHandler扩展SortPageHandler {
  公共职能getItems($排序,结果$,$页)
  {
    //返回葡萄酒
  }}类CategoryHandler扩展SortPageHandler {
  公共职能getItems($排序,结果$,$页)
  {
    //返回类别
  }
}类SubCategoryHandler扩展SortPageHandler {
  公共职能getItems($排序,结果$,$页)
  {
    //返回子类
  }
}

因此​​,父类 SortPageHandler 处理与需要修身的功能和分页和排序的公共部分。每个 getItems()方法是具体到每一个实体。通过在 SortPageHandler 宣布这个方法摘要我们强制所有子类来实现这个功能。

现在修身codeS看起来很干净:

  $应用=新\\修身\\斯利姆();$ wineHandler =新WineHandler();
$ categoryHandler =新CategoryHandler();
$ subCategoryHandler =新SubCategoryHandler();$ APP-GT&;得到('/酒',$ wineHandler-> getUrlHandler($应用程序));
$ APP-GT&;得到('/类别',$ categoryHandler-> getUrlHandler($应用程序));
$ APP-GT&;得到('/子类',$ subCategoryHandler-> getUrlHandler($应用程序));$ APP->运行();

与往常一样,你可以更重构这个code,但它给你的想法如何可以解决。

I am using the Slim PHP framework to create a RESTful API for my app. I would like all URLs to be able to accept parameters for sorting and pagination. Can someone tell me the best way to do this?

Also, can someone provide me with some proper REST URIs to this? (i.e. http://domain.com/api/category/fruit/?sort=DESC&results=25&page=2)

<?php

require 'Slim/Slim.php';

$sort = "ASC";
$results = 10;
$page = 1;

$app = new Slim();

$app->get('/wines',  function () use ($app) {
  $sort = $app->request()->params('sort');
  $results = $app->request()->params('results');
  $page = $app->request()->params('page');

  getWines();
});

$app->get('/categories',  function () use ($app) {
  $sort = $app->request()->params('sort');
  $results = $app->request()->params('results');
  $page = $app->request()->params('page');

  getCategories();
});

$app->get('/sub-categories',  function () use ($app) {
  $sort = $app->request()->params('sort');
  $results = $app->request()->params('results');
  $page = $app->request()->params('page');

  getSubCategories();
});

$app->run();

function getWines() {
  $sql = "select * FROM wine ORDER BY name " . $sort . " LIMIT " . $page . " , $results";
  try {
    $db = getConnection();
    $stmt = $db->query($sql);  
    $wines = $stmt->fetchAll(PDO::FETCH_OBJ);
    $db = null;
    echo '{"wine": ' . json_encode($wines) . '}';
  } catch(PDOException $e) {
    echo '{"error":{"text":'. $e->getMessage() .'}}'; 
  }
}

?>

解决方案

There are many ways to solve this, I would recommend to use the Template Method pattern, so you defined a common behavior in the parent class, and handle the specific details in the child classes.

abstract class SortPageHandler {
  public function getUrlHandler($app) 
  {
    $me = $this;
    return function () use ($app, $me) {
      $sort = $app->request()->params('sort');
      $results = $app->request()->params('results');
      $page = $app->request()->params('page');

      $app->response()->write($me->getItems($sort, $results, $page));
    };
  }

  abstract public function getItems($sort, $results, $page);
}

class WineHandler extends SortPageHandler {
  public function getItems($sort, $results, $page) 
  {
    //return wines
  }

}

class CategoryHandler extends SortPageHandler {
  public function getItems($sort, $results, $page) 
  {
    //return categories
  }
}

class SubCategoryHandler extends SortPageHandler {
  public function getItems($sort, $results, $page) 
  {
    //return sub-categories
  }
}

So the parent class SortPageHandler handles the common part with the function needed for Slim and the pagination and the sorting. Each getItems() method is specific to each entity. By declaring this method abstract in SortPageHandlerwe force all sub-classes to implement this functionality.

Now the Slim codes looks very clean:

$app = new \Slim\Slim();

$wineHandler = new WineHandler();
$categoryHandler = new CategoryHandler();
$subCategoryHandler = new SubCategoryHandler();

$app->get('/wines', $wineHandler->getUrlHandler($app));
$app->get('/categories', $categoryHandler->getUrlHandler($app));
$app->get('/sub-categories', $subCategoryHandler->getUrlHandler($app));

$app->run();

As always, you could refactor this code even more, but it's to give you an idea how this could be solved.

这篇关于如何使用接受修身所有REST URI的排序和分页参数?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆