D
D
DorAndor2021-10-15 09:19:42
Slim Framework
DorAndor, 2021-10-15 09:19:42

How to return an error from a controller in Slim 4?

Some rest api is being made.
Authorization by token. We check authorization using middleware
As a result $this->container->get('auth_user'), we have the current user in the container.

For example, there is such code (there will be many methods):

$app->group('/books', function (Group $group) {
        $group->post('/getList', BooksController::class . ':getList')->setName('getList');
        $group->post('/getList2', BooksController::class . ':getList2')->setName('getList2');
});


The user has a role. Certain methods must be available to certain users.
Now in the controller I have a list of methods and the roles to which they are available.
At the beginning of each method, a type check occurs:
if($this->CheckPermissions($request, $response)){
      // действия метода
       $response->getBody()->write("List books");
       return $response;
}else{
       // возврат ошибки доступа
  return $this->PermissonsError($request, $response);
}


How to do a general check?

Answer the question

In order to leave comments, you need to log in

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question