Home » Php » php – how to write a function with no view rendered on return CakePHP 3.x

php – how to write a function with no view rendered on return CakePHP 3.x

Posted by: admin February 25, 2020 Leave a comment

Questions:

I am trying to make a function inside of my UsersController.php that a curl request can call, but when return is called, the page not found error is rendered. I was wondering what I can use to stop any renders of a view. With just echo “hello world”, it works, but returns make the page not found view render.

public function upgrade() {
    $this->autoRender = false;  
    return 'hello world';
}

// From another site
$ch = curl_init();

if($ch == false){
    throw new Exception('failed to init');
}

curl_setopt_array($ch, array(
    CURLOPT_URL => 'example.com/users/upgrade',
    CURLOPT_RETURNTRANSFER => true,
    CURLOPT_POST => true,
    CURLOPT_SSL_VERIFYPEER => false,
    CURLOPT_SSL_VERIFYHOST => false
));
$output = curl_exec($ch);
echo $output;

$output is echoing a page not found instead of hello world.

How to&Answers: