2016-02-11 8 views
10

मैं Laravel 5.एक लार्वा नियंत्रक से AJAX त्रुटियों को वापस कैसे करें?

के साथ एक REST API का निर्माण कर रहा हूँ Laravel 5 में, आप App\Http\Requests\Request उपवर्ग सत्यापन नियमों कि संतुष्ट होना चाहिए से पहले एक विशेष मार्ग कार्रवाई की जाएगी परिभाषित करने के लिए कर सकते हैं। उदाहरण के लिए:

<?php 

namespace App\Http\Requests; 

use App\Http\Requests\Request; 

class BookStoreRequest extends Request { 

    public function authorize() { 
     return true; 
    } 

    public function rules() { 
     return [ 
      'title' => 'required', 
      'author_id' => 'required' 
     ]; 
    } 
} 

एक ग्राहक एक AJAX अनुरोध के माध्यम से संगत रास्ते लोड करता है, और BookStoreRequest पता चलता है कि अनुरोध नियमों को पूरा नहीं है, यह पूर्ण रूप से अपने त्रुटि (रों) वापस आ जाएगी एक JSON वस्तु के रूप में है। उदाहरण के लिए:

{ 
    "title": [ 
    "The title field is required." 
    ] 
} 

हालांकि, Request::rules() विधि केवल मान्य कर सकते हैं इनपुट और भले ही इनपुट मान्य है, त्रुटियों के अन्य प्रकार पैदा कर सकता अनुरोध के बाद पहले से ही स्वीकार किया गया है और नियंत्रक को हस्तांतरित कर दिया। उदाहरण के लिए, मान लें कि नियंत्रक किसी कारण-लेकिन फ़ाइल के लिए एक फ़ाइल के लिए नई किताब जानकारी लिखने के लिए की जरूरत है खोला नहीं जा सकता है:

<?php 

namespace App\Http\Controllers; 

use Illuminate\Http\Request; 

use App\Http\Requests; 
use App\Http\Controllers\Controller; 

use App\Http\Requests\BookCreateRequest; 

class BookController extends Controller { 

    public function store(BookStoreRequest $request) { 

     $file = fopen('/path/to/some/file.txt', 'a'); 

     // test to make sure we got a good file handle 
     if (false === $file) { 
      // HOW CAN I RETURN AN ERROR FROM HERE? 
     } 

     fwrite($file, 'book info goes here'); 
     fclose($file); 

     // inform the browser of success 
     return response()->json(true); 

    } 

} 

जाहिर है, मैं तो बस die() कर सकते थे, लेकिन यह है कि सुपर बदसूरत है। मैं सत्यापन त्रुटि त्रुटियों के समान प्रारूप में अपना त्रुटि संदेश वापस करना पसंद करूंगा। इस तरह:

{ 
    "myErrorKey": [ 
    "A filesystem error occurred on the server. Please contact your administrator." 
    ] 
} 

मैं अपने खुद के JSON ऑब्जेक्ट का निर्माण कर सकता है और वापसी कि-लेकिन निश्चित रूप से Laravel इस देशी रूप का समर्थन करता है।

ऐसा करने का सबसे अच्छा/साफ तरीका क्या है? या क्या लारवेल रीस्ट एपीआई से रनटाइम (वैध समय के विपरीत) त्रुटियों को वापस करने का एक बेहतर तरीका है?

+0

आप केवल 'वापसी प्रतिक्रिया() -> जेसन ([' त्रुटि '=>' आपका कस्टम संदेश '] क्यों नहीं कर सकते हैं;'? –

+0

आप एक कस्टम जेसन प्रतिक्रिया वर्ग – Digitlimit

+0

'रिटर्न प्रतिक्रिया() -> जेसन()' 200 ओके के साथ वापस कर सकते हैं। मैं एक उपयुक्त गैर-200 प्रतिक्रिया कोड (उदाहरण के लिए, 500 आंतरिक सर्वर त्रुटि) का उपयोग करना चाहता हूं। हाँ, मैं भी हाथ-कोड कर सकता था-मैंने अभी माना है कि लार्वेल ने पहले ही ऐसा करने का एक अंतर्निहित, अधिक संरचित तरीका प्रदान किया है। शायद यह एक गलत धारणा है। – greenie2600

उत्तर

13

आप नीचे के रूप में अपने json जवाब में स्थिति कोड सेट कर सकते हैं:

return Response::json(['error' => 'Error msg'], 404); // Status code here 

या बस सहायक फ़ंक्शन का उपयोग करके:

return response()->json(['error' => 'Error msg'], 404); // Status code here 
+0

प्रतिक्रिया प्रतिक्रिया() फ़ंक्शन, $ प्रतिक्रिया नहीं() –

3

आप यह कई मायनों में कर सकते हैं।

पहले, आप एक स्थिति कोड प्रदान करके सरल response()->json() उपयोग कर सकते हैं: यह सुनिश्चित करें कि हर त्रुटि एक json प्रतिक्रिया है

return response()->json(/** response **/, 401); 

या, एक अधिक Complexe रास्ते में, आप के लिए एक अपवाद हैंडलर सेट कर सकते हैं एक विशेष अपवाद पकड़ो और जेसन वापस लौटें।

ओपन App\Exceptions\Handler और निम्न करें:

class Handler extends ExceptionHandler 
{ 
    /** 
    * A list of the exception types that should not be reported. 
    * 
    * @var array 
    */ 
    protected $dontReport = [ 
     HttpException::class, 
     HttpResponseException::class, 
     ModelNotFoundException::class, 
     NotFoundHttpException::class, 
     // Don't report MyCustomException, it's only for returning son errors. 
     MyCustomException::class 
    ]; 

    public function render($request, Exception $e) 
    { 
     // This is a generic response. You can the check the logs for the exceptions 
     $code = 500; 
     $data = [ 
      "error" => "We couldn't hadle this request. Please contact support." 
     ]; 

     if($e instanceof MyCustomException) { 
      $code = $e->getStatusCode(); 
      $data = $e->getData(); 
     } 

     return response()->json($data, $code); 
    } 
} 

इस आवेदन में फेंक दिया किसी भी अपवाद के लिए एक json वापस आ जाएगी। अब, हम एप्लिकेशन/अपवाद में MyCustomException बनाने के लिए, उदाहरण के लिए:

class MyCustomException extends Exception { 

    protected $data; 
    protected $code; 

    public static function error($data, $code = 500) 
    { 
     $e = new self; 
     $e->setData($data); 
     $e->setStatusCode($code); 

     throw $e; 
    } 

    public function setStatusCode($code) 
    { 
     $this->code = $code; 
    } 

    public function setData($data) 
    { 
     $this->data = $data; 
    } 


    public function getStatusCode() 
    { 
     return $this->code; 
    } 

    public function getData() 
    { 
     return $this->data; 
    } 
} 

अब हम सिर्फ MyCustomException या किसी अपवाद MyCustomException का विस्तार एक json त्रुटि वापस जाने के लिए उपयोग कर सकते हैं।

public function store(BookStoreRequest $request) { 

    $file = fopen('/path/to/some/file.txt', 'a'); 

    // test to make sure we got a good file handle 
    if (false === $file) { 
     MyCustomException::error(['error' => 'could not open the file, check permissions.'], 403); 

    } 

    fwrite($file, 'book info goes here'); 
    fclose($file); 

    // inform the browser of success 
    return response()->json(true); 

} 

अब, न केवल अपवाद MyCustomException के माध्यम से फेंक दिया एक json त्रुटि लौटाएगा, लेकिन किसी भी अन्य अपवाद सामान्य रूप में फेंक दिया।

संबंधित मुद्दे