Search code examples
phplaravellumenlumen-5.5

Lumen: Where to define controller reponse helper method


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.


Solution

  • 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