Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/xml/15.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/api/5.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Xml Laravel-为API调整现有方法_Xml_Api_Laravel_Laravel 5.1 - Fatal编程技术网

Xml Laravel-为API调整现有方法

Xml Laravel-为API调整现有方法,xml,api,laravel,laravel-5.1,Xml,Api,Laravel,Laravel 5.1,我有一个与Laravel的当前项目,现在需要创建一些API,以便客户可以与我们的系统进行通信。但我不确定最好的方法是什么 我应该如何用拉雷维尔的方式来处理这样的事情?由于功能已经在工作,我不确定是否应该以某种方式验证和调整他们的请求,使其与当前工作的代码相适应,或者创建另一个单独的函数来处理请求 对于验证,我希望使用FormRequest,或者在需要时扩展我自己的类。在验证失败时,我希望能够返回一个XML响应。我已经有了生成XML的函数,但是需要一种方法,用messages()方法中定义的自定义

我有一个与Laravel的当前项目,现在需要创建一些API,以便客户可以与我们的系统进行通信。但我不确定最好的方法是什么

我应该如何用拉雷维尔的方式来处理这样的事情?由于功能已经在工作,我不确定是否应该以某种方式验证和调整他们的请求,使其与当前工作的代码相适应,或者创建另一个单独的函数来处理请求

对于验证,我希望使用FormRequest,或者在需要时扩展我自己的类。在验证失败时,我希望能够返回一个XML响应。我已经有了生成XML的函数,但是需要一种方法,用messages()方法中定义的自定义错误消息返回XML


任何指示都将不胜感激。谢谢

所以,完全公开:我在处理xml和API时创建了一些有用的包:

它们允许您自动将传入的xml转换为数组,并将其合并到请求对象中,以便在需要时使用FormRequests对其进行验证。您还可以使用xml,甚至是请求应用程序的首选格式(json或xml)进行响应

最后,您可以在
render
方法中处理
app/Exceptions/Handler.php
中的错误,如下所示:

$response = [
    'status' => 'ERROR',
    'message' => 'Sorry, there was a problem completing your request.',
    'data' => [],
    'errors' => []
];
$httpCode = 400;

// Validation exception, for when the user doesn't fill something out correctly
if ($exception instanceof \Illuminate\Validation\ValidationException) {
    $response['message'] = 'A validation error occured.';
    $response['errors'] = $exception->errors();
    $httpCode = 415;
}

// Method not allowed (GET, POST, PUT, PATCH, DELETE)
if ($exception instanceof \Symfony\Component\HttpKernel\Exception\MethodNotAllowedHttpException) {
    $response['message'] = 'HTTP method not allowed.';
    $httpCode = 405;
}

// Clean up the no query found message (findOrFail())
if ($exception instanceof \Illuminate\Database\Eloquent\ModelNotFoundException) {
    $response['message'] = "Sorry, we couldn't find what you're looking for. Please try again.";
    $httpCode = 404;
}

// When the user makes a request to a route with the wrong http verb
if ($exception instanceof \Illuminate\Http\Exceptions\HttpResponseException) {
    $response['message'] = "Oops, it looks like there was an error interacting with that resource.";
    $httpCode = 405;
}


return response()->preferredFormat($response, $httpCode);

现在,每当您的应用程序遇到常见的Laravel异常时,它都会返回json或xml格式的api响应(取决于您请求的客户机喜欢哪种格式)。

您的大部分逻辑是在控制器、模型还是其他位置?您对现有代码有任何自动测试吗?我有一个服务层,所以我有控制器调用的服务类。因此,逻辑在这些服务类中,它们从控制器接收请求对象。我目前没有自动测试。