使用 $this->form 附加一个数组

问题描述 投票:0回答:2

我在log.phtml中有一个数组,即:

<?php
//application/logmessage/log.phtml//

require_once 'Zend/Log.php';
require_once 'Zend/Log/Writer/Stream.php';

class Logger {

/**
 * Array variable store full file back trace information.
 *
 * @access protected
 */
protected $backTrace = array();

/**
 * Array variable store full message information.
 *
 * @access protected
 */
protected $messageInfo = array();

/**
 * Constructor: loads the debug and error logs.
 *
 */
public function __construct( $type, $msg ) {

    $mock = new Zend_Log_Writer_Mock;
    $logger = new Zend_Log( $mock );
    $logger->$type( $msg );

    // Get full message information.
    array_push( $this->messageInfo, $mock->events[0] );

    // Get full information of file, from where the message come.
    array_push( $this->backTrace, debug_backtrace() );

    // Set all required informationn in their respective variables. 
    $messageText = $this->messageInfo[0]["message"];
    $priority = $this->messageInfo[0]["priorityName"];
    $backTraceFile = $this->backTrace[0][0]["file"];
    $backTraceLine = $this->backTrace[0][0]["line"];

    $logArray = array( "Message" => $messageText, "Priority" => $priority, 
                   "Line" => $backTraceLine, "File" => $backTraceFile );

    }
}
?>

现在我想将 $logArray 数组与表单一起显示,我的表单文件如下:

<?php 
//application/views/scripts/miscellaneous/index.phtml//

//require_once('../application/logmessage/log.phtml');

    echo $this->form;
?>

我怎样才能以形式显示日志数组....?

我怎样才能将 $logArray() 从“ application/logmessage/log.phtml ”返回到 “应用程序/视图/脚本/杂项/index.phtml”

php zend-framework
2个回答
0
投票

将此代码添加到您的记录器中:

public $logArray;

也将最后一行更改为

$this->logArray = array( "Message" => $messageText, "Priority" => $priority, 
               "Line" => $backTraceLine, "File" => $backTraceFile );

}

现在你可以从

view
controller
form
等访问$logArray。例如我们在
controller
中调用Logger并将其发送到
view

/** Controller **/
$logger = new Logger($msg, $type);
$this->view->logArray = $loagger->logArray;

/** View **/
print_r($this->logArray);

不要忘记用自定义助手替换

print_r
以创建漂亮的用户界面

echo $this->myCustomHelper($this->logArray);

-1
投票

我访问http://framework.zend.com/manual/en/zend.registry.html,我唯一添加的是:

<?php

//application/logmessage/log.phtml//



require_once 'Zend/Log.php';
require_once 'Zend/Log/Writer/Stream.php';

class Logger {

    /**
     * Array variable store full file back trace information.
     *
     * @access protected
     */
    protected $backTrace = array();

    /**
     * Array variable store full message information.
     *
     * @access protected
     */
    protected $messageInfo = array();

    /**
     * Constructor: loads the debug and error logs.
     *
     */
    public function __construct( $type, $msg ) {

        $mock = new Zend_Log_Writer_Mock;
        $logger = new Zend_Log( $mock );
        $logger->$type( $msg );
        //var_dump($mock->events[0]);

        // Get full message information.
        array_push( $this->messageInfo, $mock->events[0] );

        // Get full information of file, from where the message come.
        array_push( $this->backTrace, debug_backtrace() );

        // Set all required informationn in their respective variables. 
        $messageText = $this->messageInfo[0]["message"];
        $priority = $this->messageInfo[0]["priorityName"];
        $backTracePath = $this->backTrace[0][0]["file"];
        $backTraceLine = $this->backTrace[0][0]["line"];

        $logArray = array( "Message" => $messageText, "Priority" => $priority, 
                   "Line" => $backTraceLine, "Path" => $backTracePath );

        // Pass the array for display.
        Zend_Registry::set('logArray', $logArray);
    }
}
?>

以及

中的一些代码
<?php
//application/views/scripts/miscellaneous/index.phtml//


    echo $this->form;

    $logArray = Zend_Registry::get('logArray');
    print_r($logArray);

?>

效果非常好。

© www.soinside.com 2019 - 2024. All rights reserved.