Welcome Guest, Not a member yet? Register   Sign In
What does BaseController's initController method do?
#1

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().
    }
}
Reply
#2

The initController method is used for initialization.
Any value returned by this method is not processed in any way.

Use filters.
Reply




Theme © iAndrew 2016 - Forum software by © MyBB