mirror of
https://github.com/codeigniter4/CodeIgniter4.git
synced 2025-02-20 11:44:28 +08:00
Added No Content response to API\ResponseTrait
This commit is contained in:
parent
290c9f6905
commit
e20830e1bc
@ -65,6 +65,7 @@ trait ResponseTrait
|
||||
protected $codes = [
|
||||
'created' => 201,
|
||||
'deleted' => 200,
|
||||
'no_content' => 204,
|
||||
'invalid_request' => 400,
|
||||
'unsupported_response_type' => 400,
|
||||
'invalid_scope' => 400,
|
||||
@ -190,6 +191,21 @@ trait ResponseTrait
|
||||
|
||||
//--------------------------------------------------------------------
|
||||
|
||||
/**
|
||||
* Used after a command has been successfully executed but there is no
|
||||
* meaningful reply to send back to the client.
|
||||
*
|
||||
* @param string $message Message.
|
||||
*
|
||||
* @return mixed
|
||||
*/
|
||||
public function respondNoContent(string $message = 'No Content')
|
||||
{
|
||||
return $this->respond(null, $this->codes['no_content'], $message);
|
||||
}
|
||||
|
||||
//--------------------------------------------------------------------
|
||||
|
||||
/**
|
||||
* Used when the client is either didn't send authorization information,
|
||||
* or had bad authorization credentials. User is encouraged to try again
|
||||
|
@ -278,6 +278,15 @@ EOH;
|
||||
$this->assertEquals($this->formatter->format($expected), $this->response->getBody());
|
||||
}
|
||||
|
||||
public function testNoContent()
|
||||
{
|
||||
$controller = $this->makeController();
|
||||
$controller->respondNoContent('');
|
||||
|
||||
$this->assertEquals('No Content', $this->response->getReason());
|
||||
$this->assertEquals(204, $this->response->getStatusCode());
|
||||
}
|
||||
|
||||
public function testNotFound()
|
||||
{
|
||||
$controller = $this->makeController();
|
||||
|
@ -48,6 +48,8 @@ exist for the most common use cases::
|
||||
respondCreated($data);
|
||||
// Item successfully deleted
|
||||
respondDeleted($data);
|
||||
// Command executed by no response required
|
||||
respondNoContent($message);
|
||||
// Client isn't authorized
|
||||
failUnauthorized($description);
|
||||
// Forbidden action
|
||||
@ -179,6 +181,19 @@ Class Reference
|
||||
$user = $userModel->delete($id);
|
||||
return $this->respondDeleted(['id' => $id]);
|
||||
|
||||
.. php:method:: respondNoContent(string $message = 'No Content')
|
||||
|
||||
:param string $message: A custom "reason" message to return.
|
||||
:returns: The value of the Response object's send() method.
|
||||
|
||||
Sets the appropriate status code to use when a command was successfully executed by the server but there is no
|
||||
meaningful reply to send back to the client, typically 204.
|
||||
|
||||
::
|
||||
|
||||
sleep(1);
|
||||
return $this->respondNoContent();
|
||||
|
||||
.. php:method:: failUnauthorized(string $description = 'Unauthorized'[, string $code=null[, string $message = '']])
|
||||
|
||||
:param mixed $description: The error message to show the user.
|
||||
|
Loading…
x
Reference in New Issue
Block a user