0

In my lumen based API, negative responses like entity not found are handled by custom exceptions that are caught by the global exception handler.

Now I'm looking for a non-redundant way to return positive responses.

class ListController extends Controller {

   public function someEndpoint(Request $request, Response $response) {
     if($bad) {
       throw new CustomException("XYZ is bad");
     }
     /* Instead of */ 
     return response()->json("msgStr" => "Entity created");
     /* something like */
     return entityCreatedReponse();
   }
}

What's the lumen way of defining entityCreatedReponse()? I don't want to have it in a base controller.

  • Do want to use your own helper functions ? – Abdes Aug 26 '19 at 19:26
  • I am looking for a both pragmatic and elegant solution, doesn't have to be perfect. How would you define a helper function? –  Aug 26 '19 at 19:36
  • Looks like Controller.php exists just for this purpose. Anyone agree? :) –  Aug 26 '19 at 19:47

1 Answers1

0

You can create a Helpers.php in folder app and put some function in it.

and in your composer add thhis to autoload:

"files": [ 
         "app/Helpers.php"
]

and run :

composer dump-autoload

now u can use any function from helpers in your all app

Abdes
  • 926
  • 1
  • 15
  • 27