我看到一些API有简单的查询,可以在url中构建.例如,我有产品型号的产品表.
产品表属性:
如何在url中进行查询,如下所示(fields参数表示选择那些指定的字段):
http://example.com/product?fields=id,product_name,price,barcode&sortby=price
或者像这样得到价格等于10.00:
http://example.com/product?fields=id,product_name,price,barcode&price=10.00
我知道在laravel中我们可以检查get参数$request->has()并使用它$request->input('fieldname)来逐个检查和检索值
但我认为应该有更好的方法,或者可能有一个包装函数可以用于所有控制器从url get参数读取查询.
谢谢
我正在使用Laravel 5.3.我有几个API,用户将请求特定的ID.例如,url订阅一个事件
example.com/api/event/{id}/subscribe
通常,如果id不存在,Laravel将返回响应500,并显示错误消息"试图获取非对象的属性"
所以我会在模型'id'id传递的每个控制器中添加检查事件是否存在如下:
$event = Event::find($id)
if ($event) {
// return json data
}
else {
return response()->json([
'status' => 'object not found'
], 404);
}
Run Code Online (Sandbox Code Playgroud)
我的问题是,任何更好的解决方案来全局处理这个问题来检查所请求的对象是否不存在?我目前的解决方案就在这里,但我认为应该有更好的解决方案
我将此代码添加到我的代码中app/Exception/Handler.php,因此每个api请求不存在的对象都将返回带有特定json消息的404.因此API使用者将知道对象ID无效.
public function render($request, Exception $exception)
{
// global exception handler if api request for non existing object id
if ($request->wantsJson() && $exception->getMessage() == 'Trying to get property of non-object') {
return response()->json([
'status' => 'object requested not found'
], 404);
}
return parent::render($request, $exception);
}
Run Code Online (Sandbox Code Playgroud)
提前致谢!
我有一个系统,每个用户只允许为两个设备安装应用程序.当用户在同一设备上卸载并重新安装时,会发生此问题.所以它会生成新的UUID,当应用程序检查Web服务时.
应用程序将发送UUID和登录ID,以检查具有该登录ID的用户是否已安装在两个以上的设备中.我使用真正的iPhone和iPad设备,而不是使用模拟器.我不确定生产环境.目前,使用Apple TestFlight使用AppStore Distribution配置文件分发应用程序.
我使用它生成uuid
let uuid = UIDevice.currentDevice().identifierForVendor!.UUIDString
Run Code Online (Sandbox Code Playgroud)
谢谢.