localhost/restful/auth/posts/1
我的代码就像这样
@List($objnm, $objid) = explode('/', $_GET['url']);
if ($objnm == "posts")
{
$posts = $db->query('SELECT * FROM post');
echo json_encode($posts, JSON_PRETTY_PRINT);
if($objid == "1")
{
echo 'susah bnget';
$id = $db->query('SELECT * FROM post where id=1');
echo json_encode($id, JSON_PRETTY_PRINT);
}
}
答案 0 :(得分:0)
例如,您可以这样做:
$url = $_GET['url'];
$chunks = preg_split("#\/#u", $url, -1, PREG_SPLIT_NO_EMPTY);
$count = count($chunks);
$lastChunk = $chunks[$count-1];
// Detect: use findAll or FindOneById
if (is_numeric($lastChunk)){
$id = $lastChunk;
$entityName = $chunks[$count-2];
} else {
$id = null;
$entityName = $chunks[$count-1];
}
// Create sql query string
$sql = "SELECT * FROM";
switch ($entityName) {
case "posts":
$sql .= ' post';
break;
case "something_else":
$sql .= ' something_else';
break;
default:
throw new Exception("$entityName doesn't expected");
}
if (!is_null($id)) {
$sql .= " WHERE id = $id";
}
$result = $db->query($sql);
echo json_encode($result, JSON_PRETTY_PRINT);