CodeIgniter Forums
Call to undefined method respond() - Printable Version

+- CodeIgniter Forums (https://forum.codeigniter.com)
+-- Forum: CodeIgniter 4 (https://forum.codeigniter.com/forum-28.html)
+--- Forum: CodeIgniter 4 Support (https://forum.codeigniter.com/forum-30.html)
+--- Thread: Call to undefined method respond() (/thread-77252.html)



Call to undefined method respond() - myo - 08-07-2020

Hi,

I created a controller as following. It works fine but when I send API request through Postman I get error "Call to undefined method respond()". What mistake I am making? 

PHP Code:
use App\Controllers\BaseController;
use 
App\Models\MyModel;
use 
CodeIgniter\API\ResponseTrait;

class 
Myclass extends BaseController {

    public function mytask() {
        
        $data 
$this->request->getPost();
        $model = new MyModel;
        $msg $model->save_data($data);
                    
        
if ($this->apicall == "yes") {

            return $this->respond($msg); // API Call
            
        
} else {

            return view('message_view'$msg); // Not API Call, return View

        }
    }





RE: Call to undefined method respond() - natanfelles - 08-07-2020

You forgot to place the trait call:

Code:
class Myclass extends BaseController {
    use ResponseTrait;

public function mytask...