简体   繁体   中英

use the variable returned by cakephp controller

I am using cakephp 2.10.24. I need to use the variable returned from the controller in a php file(not a view), so I used the dispatch function to make a Cakephp request. The data is displayed by itself and I'm not able to execute some code after the dispatch call.

I tried adding to the controller but it didn't work

$this->autoRender = false;
$this->layout = false;
$this->autoLayout = false;

the controller action:

$this->autoRender = false;
$this->layout = false;
$this->autoLayout = false;

$this->response->body(json_encode(array(
    'key' => 0,
    'message' => 'Invalid request.'
)));

$this->response->send();
$this->_stop();

php file:

<?php

echo ('i\'m test file <br />');

include 'app/webroot/index.php';

$request = new CakeRequest('/controller/action/param');
$response = new CakeResponse(array('type' => 'application/json'));

echo $results = $Dispatcher->dispatch(
    $request ,
    $response,
    array('return' => 'vars')
);


//some codes not running
var_dump($response);
print_r(json_decode($response));
?>

Got the results but still auto printing the result on the screen.

test file:

$dispatcher = new Dispatcher();
$response = new CakeResponse();
$results = $dispatcher->dispatch(new CakeRequest('/Controlller/Action/params'),$response);
print_r("response: ".$response);

action in the controller:

$this->autoRender=false;
$this->autoLayout=false;
$this->response->body($result);
$this->response->send();

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM