What does BaseController's initController method do? |
All of my controllers are extending from BaseController, which includes the initController method. I am trying to check for certain parameters in the post data and return a JSON 404 if they are not present. However, I have noticed that the return statement in the initController code is passing the execution to the actual controller method.
I am doing this because, otherwise, I would have to check for the existence of parameters such as user_id and Post_id, and whether there is a row in the users/posts table, in every controller. This would result in repetitive code, so I am refactoring to keep it DRY. Here is an example of the code in the BaseController's initController method: public function initController(RequestInterface $request, ResponseInterface $response, LoggerInterface $logger) { // some code... foreach ($requestData as $key => $value) { // some logic... if (!$this->$key) { return $this->notFoundResponse(); } } // some more code... } And here is an example of the code in an actual controller: class Flags extends BaseController { public function index() { // We have arrived here if the BaseController function returns a notFoundResponse(). } } |
Messages In This Thread |
What does BaseController's initController method do? - by LansoirThemtq - 03-30-2023, 03:57 AM
RE: What does BaseController's initController method do? - by iRedds - 03-30-2023, 06:37 AM
|