php - Return a JSON response in Laravel 4 from within a route model binding -


i'm creating api access blog entries, , rather laravel model binding returning 404 automatically, i'd rather return 200, , json in same format api controller returns.

i use code below, when returning json response works within controllers, can't working within route::bind, instead json output follows:

{"success":true,"data":{"headers":{}}} 

here code within app/global.php

route::group(array('prefix' => '/api/blog'),function() {     route::bind('blog', function($id) {         $blog = blog::find($id);         if (!$blog) {             return response::json(array('status' => false, 'message' => 'blog not found'), 200);         }     });      route::get('index', array('uses' => 'blogcontroller@index'));     route::get('create', array('uses' => 'blogcontroller@create'));     route::get('read/{blog}', array('uses' => 'blogcontroller@read')); }); 

it if try , return empty array in json response so:

return response::json(array(), 200); 

after little more thought, realised define new exception throw, , catch exception type.

route::group(array('prefix' => '/api/blog'),function() {     route::bind('blog', function($id) {         $blog = blog::find($id);         if (!$blog) {             throw new apiexception('blog not found');         }     });     route::get('index', array('uses' => 'blogcontroller@index'));     route::get('create', array('uses' => 'blogcontroller@create'));     route::get('read/{blog}', array('uses' => 'blogcontroller@read')); }); class apiexception extends exception {} app::error(function(apiexception $ex) {     return response::json(array('status' => false, 'message' => $ex->getmessage())); }); 

Popular posts from this blog

c# - ODP.NET Oracle.ManagedDataAccess causes ORA-12537 network session end of file -

matlab - Compression and Decompression of ECG Signal using HUFFMAN ALGORITHM -

utf 8 - split utf-8 string into bytes in python -