How to accept sorting and pagination parameters in

2020-06-25 04:07发布

问题:

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() .'}}'; 
  }
}

?>

回答1:

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.