使用cakephp控制器返回的变量

2ledvvac  于 2022-12-17  发布在  PHP
关注(0)|答案(1)|浏览(152)

我使用的是cakephp 2.10.24,我需要使用php文件(不是视图)中控制器返回的变量,所以我使用了dispatch函数发出了一个Cakephp请求,数据是自己显示的,并且在调用dispatch之后我无法执行一些代码。
我尝试添加到控制器,但不起作用

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

控制器动作:

$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文件:

<?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));
?>
cuxqih21

cuxqih211#

得到了结果,但仍然自动打印结果在屏幕上。
测试文件:

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

控制器中的动作:

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

相关问题