CodeIgniter Forums
$this->responseCreated() method not found - Printable Version

+- CodeIgniter Forums (https://forum.codeigniter.com)
+-- Forum: CodeIgniter 4 (https://forum.codeigniter.com/forumdisplay.php?fid=28)
+--- Forum: CodeIgniter 4 Support (https://forum.codeigniter.com/forumdisplay.php?fid=30)
+--- Thread: $this->responseCreated() method not found (/showthread.php?tid=90887)



$this->responseCreated() method not found - spreaderman - 05-18-2024

I was listening this to tutorial about shield and api: https://www.youtube.com/watch?v=0sdbsbOW8wA&list=PLT9miexWCpPW6Z_OXNv2d78cqCrZaPrVG&index=31

At 10:05 the author uses $this->respondCreated(). I am using CI 4.5.1 I cannot see that method. I also do not see it in the documentation. Was that some previous version of am I missing something?

Thank you for your help!


RE: $this->responseCreated() method not found - datamweb - 05-18-2024

Hi,

Method respondCreated() is still provided by Codeigniter 4.5.1. You can see from the link below.

https://github.com/datamweb/CodeIgniter4/blob/8fc89854cc85c3b002ce2b26d5de56f6fa131166/system/API/ResponseTrait.php#L143-L157

Make sure your controller extends from ResourceController. Like below:

PHP Code:
<?php

namespace App\Controllers;

use 
CodeIgniter\RESTful\ResourceController;

class 
Home extends ResourceController
{
    public function index()
    {
        return $this->respondCreated();
    }
}
?>

Or enter ResponseTrait manually:

PHP Code:
<?php

namespace App\Controllers;
use 
CodeIgniter\API\ResponseTrait;

class 
Home extends BaseController
{
    use ResponseTrait;

    public function index()
    {
        return $this->respondCreated();
    }




RE: $this->responseCreated() method not found - kenjis - 05-18-2024

Read https://codeigniter.com/user_guide/outgoing/api_responses.html#respondCreated