Keeping a Controller Method Simple

Posted: 2016-06-23 12:02:35

When I am done with a controller, done mocking up ideas and ready for some long term code, then I think this is the most information a Controller method should have.

public function updateFoor($id, $foo) {
        try {
            
            $results = $this->someRepoOrServiceClass($Id, $foo);            
            return Response::json(['data' => [], 'message' => "Success doing something"], 202);
        } catch (\Exception $e) {
            $message = sprintf("Error doing something %s", $e->getMessage());
            Log::debug($message);
            
            return Response::json(['data' => [], 'message' => $message], 400);
        }
}

At this point I have someRepoOrServiceClass well tested outside of this Controller. And I do not need to do much to this controller since it is just returning the results or dealing with an error.

Even using Form Request Validation I can create logic and test logic outside of my Controller. Making the moment I come back to my controller just about plugging in these "lego" like pieces.


Tags:

php coding