返回的数组会产生意外的结果

时间:2014-07-06 03:18:07

标签: php arrays modx modx-revolution

我有一个modx革命片段[实际上是ajax处理器],它将一些数据传递给另一个调用函数的片段......

<?php
// processor.formdata

$status = 'success';

$msg = '';

$output = $modx->runSnippet($_POST['snippet'],array(
   'function' => $_POST['function'],
   'formdata' => $_POST
));

$modx->log(modX::LOG_LEVEL_ERROR,'got updateSurchargeData status. '.$output['status']);
$modx->log(modX::LOG_LEVEL_ERROR,'got updateSurchargeData message. '.$output['msg']);

$response_array = array(
    'status' => $output['status'],
    'msg' => $output['msg'],
    );

header('Content-type: application/json');

$output = json_encode($response_array);

return $output;

$ modx-runSnippet运行:

<?php
//AmericanSurcharge

//echo $scriptProperties('form');
//$modx->log(modX::LOG_LEVEL_ERROR,'Running snippet. '.$function);
//$modx->log(modX::LOG_LEVEL_ERROR,'Running snippet. '.implode(',',$formdata));

$output = '';

$core = $modx->getOption('core_path').'components/americansurcharge/model/';

$surcharge = $modx->getService('americansurcharge', 'AmericanSurcharge', $core, $scriptProperties);

if (!$surcharge instanceof AmericanSurcharge){

    return 'Insantiation failed';

}else{

  $scriptProperties['formdata'] = $formdata;

  $output = $surcharge->$scriptProperties['function']($scriptProperties);

}

return $output;

反过来运行这个:

/**
*
* update one surcharge data
* AmericaSurcharge.class.php
*
*/
public function updateSurchargeData($scriptProperties){

    $this->modx->log(modX::LOG_LEVEL_ERROR,'Running updateSurchargeData. '.implode(',',$scriptProperties['formdata']));

    $output = array(
        'status' => 'success',
        'msg' => 'this is the message to return.',
        );

    $this->modx->log(modX::LOG_LEVEL_ERROR,'Finished running updateSurchargeData. '.$output['status']);
    $this->modx->log(modX::LOG_LEVEL_ERROR,'Finished running updateSurchargeData. '.$output['msg']);

    return $output;

}

一切正常&amp;运行成功,但$ output = $ modx-runSnippet返回的值同时返回&#34; A&#34;在每个数组键!

查看来自AmericaSurcharge.class.php的日志我得到了这个:

(ERROR @ /index.php) Finished running updateSurchargeData. success
(ERROR @ /index.php) Finished running updateSurchargeData. this is the message to return.

并紧接着来自processor.formdata:

[2014-07-05 23:10:29] (ERROR @ /index.php) got updateSurchargeData status. A
[2014-07-05 23:10:29] (ERROR @ /index.php) got updateSurchargeData message. A

所以在从AmericaSurcharge.class.php返回$ ouput数组并将其返回到processor.formdata之间的某个地方,值会发生一些变化。

我做错了什么?

1 个答案:

答案 0 :(得分:2)

  

我做错了什么?

您正在使用代码段:)

当调用$modx->runSnippet($snippetName)时,它只返回字符串。不是数组或其他。

看看modScript流程方法:

public function process($properties= null, $content= null) {
    parent :: process($properties, $content);
    if (!$this->_processed) {
        $scriptName= $this->getScriptName();
        $this->_result= function_exists($scriptName);
        if (!$this->_result) {
            $this->_result= $this->loadScript();
        }
        if ($this->_result) {
            if (empty($this->xpdo->event)) $this->xpdo->event = new stdClass();
            $this->xpdo->event->params= $this->_properties; /* store params inside event object */
            ob_start();
            $this->_output= $scriptName($this->_properties);
            $this->_output= ob_get_contents() . $this->_output;
            ob_end_clean();
            if ($this->_output && is_string($this->_output)) {
                /* collect element tags in the evaluated content and process them */
                $maxIterations= intval($this->xpdo->getOption('parser_max_iterations',null,10));
                $this->xpdo->parser->processElementTags(
                    $this->_tag,
                    $this->_output,
                    $this->xpdo->parser->isProcessingUncacheable(),
                    $this->xpdo->parser->isRemovingUnprocessed(),
                    '[[',
                    ']]',
                    array(),
                    $maxIterations
                );
            }
            $this->filterOutput();
            unset ($this->xpdo->event->params);
            $this->cache();
        }
    }
    $this->_processed= true;
    $this->xpdo->parser->setProcessingElement(false);
    /* finally, return the processed element content */
    return $this->_output;
}

但您仍然可以将您的代码段称为功能。像这样:

if($modx->getParser()){
   $snippet= $modx->parser->getElement('modSnippet', $snippetName);
   if ($snippet instanceof modSnippet) {

    $s = $snippet->getScriptName();
    if(!function_exists($s)){
        $snippet->loadScript();
    }
    print_r($s());
   }
}

P.S .: 解决问题的最佳方法是使用真正的基于类的处理器而不是类似处理器的处理器。 优点:

  1. 返回您想要的任何内容,
  2. 一个处理器可以扩展另一个,
  3. 标准化响应格式;