准备工作
application oute.php
Route::get('api/:version/product/by_category', 'api/:version.Product/getAllInCategory');
Product模型
applicationapimodelProduct.php,省略部分代码
class Product extends BaseModel
{
//...
public static function getProductsByCategoryID($categoryID)
{
$products = self::where('category_id', '=', $categoryID) -> select();
return $products;
}
}
Product控制器
applicationapicontrollerv1Product.php
class Product
{
//...
public function getAllInCategory($id)
{
(new IDMustBePositiveInt())->goCheck();
$products = ProductModel::getProductsByCategoryID($id);
if ($products->isEmpty()) {
throw new ProductException();
}
$products = $products->hidden(['summary']);
return $products;
}
}