CI4 Filter before method throw for page unauthorized |
I'm able to filter request to my rest api using ci4 filter.
But the problem is, I dont understand how to throw the error for showing page unauthorized like in http message. PHP Code: class Authorization implements FilterInterface {
Hello,
I use HTTP Responses to manage my response. For example, if my website has a maintenance mode activated, I will send this : $this->response->setStatusCode(503); $this->response->setHeader('Retry-After', '3600'); $this->response->setBody('<h1>Maintenance mode activated.</h1>'); return $this->response->send(); You can have more informations about this at this page : https://codeigniter4.github.io/userguide...ponse.html Also, for REST API, I think you can use API Response Trait : https://codeigniter4.github.io/userguide...onses.html I never used API Response, but you can use something like that : return $this->respond($data, 401, $description); OR return $this->failUnauthorized($description); Hope this help you !
(04-09-2020, 11:05 PM)jean5769 Wrote: Hello, I already try that in my filter. Its not working properly. It is sending the right code which is 401, but it also sent the body too. That code implement new different response, not the one that used and shared by the controller. I need to say again that this is Filter, not a controller, or response trait.
And what about this code ?
$response = \Config\Services::response(); $response->setStatusCode(401); $response->send(); die(); You send the response and you kill your script, so there is no body.
Returning a response object works as well.
PHP Code: $response = service('response');
(04-09-2020, 06:53 PM)404NotFound Wrote: I'm able to filter request to my rest api using ci4 filter. |
Welcome Guest, Not a member yet? Register Sign In |