Keeping a Controller Method Simple

Written by Alfred Nutile RSS / Original link on Apr. 14, 2017

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.


« Behat Laravel Domain Testing Inside Out - Example of Mixing it up Behat BDD and PHPUnit »