使用$ this->表单附加数组

时间:2009-08-12 03:34:40

标签: zend-framework

我在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;
?>

如何显示带有表单的日志数组....?

我如何从“application / logmessage / log.phtml”返回$ logArray() “application / views / scripts / miscellaneous / index.phtml”

2 个答案:

答案 0 :(得分:0)

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

public $logArray;

也用

更改最后一行
$this->logArray = array( "Message" => $messageText, "Priority" => $priority, 
               "Line" => $backTraceLine, "File" => $backTraceFile );

}

现在您可以从viewcontrollerform等访问$ 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 :(得分:-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);

?>

效果很好。