Yii:输出变量用于测试回波和记录不起作用

时间:2016-04-05 07:08:45

标签: php yii

我正在做一个传递给我的Yii项目。由于我对这件事情很陌生,我的第一直觉是输出一大堆价值来了解事物的来源和去向。

为此,我尝试在Controller中使用echo。如:

public function actionCallCommentForm($id='')
{
    $topic=Forum::model()->findByPk($id);

    echo '<script language="javascript">';
    echo 'alert("message successfully sent")';
    echo '</script>';

    $this->renderPartial('_commentform', array(
        'forum'=>$topic,
        'model'=>new Comment,
        'view'=>'view',
    ));
}

然而,这不起作用。而且,如果确实如此,我也不知道如何输出$topic

所以我尝试了记录。基于the answer to this question

public function actionCallCommentForm($id='')
{
    $topic=Forum::model()->findByPk($id);
    Yii::trace(CVarDumper::dumpAsString("TESTING!"));
    Yii::trace(CVarDumper::dumpAsString($topic));
    $this->renderPartial('_commentform', array(
        'forum'=>$topic,
        'model'=>new Comment,
        //'view'=>array('view','id'=>$id),
        'view'=>'view',
    ));
}

runtime/application.log

中检查日志(确实可以输出错误)

答案是The Application Log is shown below in every page.,但我担心不清楚。

还有其他方法吗?

编辑:更多详情:

main.php

<?php
// load the helper functions - jim
require_once( dirname(__FILE__) . '/../components/Helpers.php');

// uncomment the following to define a path alias
// Yii::setPathOfAlias('local','path/to/local-folder');

// This is the main Web application configuration. Any writable
// CWebApplication properties can be configured here.
return array(
    'basePath'=>dirname(__FILE__).DIRECTORY_SEPARATOR.'..',
    'name'=>'Star Connects',

    // preloading 'log' component
    'preload'=>array('log'),

    // autoloading model and component classes
    'import'=>array(
        'application.models.*',
        'application.components.*',
        'application.modules.poll.models.*',
        'application.modules.poll.components.*',
        'ext.multimodelform.MultiModelForm',
        'ext.mail.YiiMailMessage',
    ),

    'aliases' => array(
        'xupload' => 'ext.xupload'
    ),

    'modules'=>array(
        // uncomment the following to enable the Gii tool

        'gii'=>array(
            'class'=>'system.gii.GiiModule',
            'password'=>'manager1',
            // If removed, Gii defaults to localhost only. Edit carefully to taste.
            'ipFilters'=>array($_SERVER['REMOTE_ADDR'],'127.0.0.1','10.0.1.100'),
        ),

        'poll' => array(
            // Force users to vote before seeing results
            'forceVote' => TRUE,
            // Restrict anonymous votes by IP address,
            // otherwise it's tied only to user_id 
            'ipRestrict' => TRUE,
            // Allow guests to cancel their votes
            // if ipRestrict is enabled
            'allowGuestCancel' => FALSE,
        ),

    /*  'message' => array(
                'userModel' => 'User',
                'getNameMethod' => 'getFullName',
                'getSuggestMethod' => 'getSuggest',
        ), */
    ),

    // application components
    'components'=>array(
        'user'=>array(
            // enable cookie-based authentication
            'allowAutoLogin'=>true,
            'class'=>'WebUser',
            'loginRequiredAjaxResponse' => 'YII_LOGIN_REQUIRED',
        ),
        'session'=> array(
            'timeout'=> 1440
        ),
        'partyroles'=>array(
            // enable cookie-based authentication
            'class'=>'WebUser',
        ),
        // uncomment the following to enable URLs in path-format
        /*
        'urlManager'=>array(
            'urlFormat'=>'path',
            'rules'=>array(
                '<controller:\w+>/<id:\d+>'=>'<controller>/view',
                '<controller:\w+>/<action:\w+>/<id:\d+>'=>'<controller>/<action>',
                '<controller:\w+>/<action:\w+>'=>'<controller>/<action>',
            ),
        ),
        /*
        'db'=>array(
            'connectionString' => 'sqlite:'.dirname(__FILE__).'/../data/testdrive.db',
        ), */
        // uncomment the following to use a MySQL database

        'urlManager'=>array(
            'urlFormat'=>'path',
            'rules'=>array(
                'index.php?r=site/login/<msg>'=>'site/login',
            ),
        ),          

        'db'=>array(
            'connectionString' => 'mysql:host=localhost;dbname=dbtest',
            'emulatePrepare' => true,
            'username' => 'root',
            'password' => '', 
            'charset' => 'utf8',
            //'tablePrefix' => 'tbl_', 
        ),

        'errorHandler'=>array(
            // use 'site/error' action to display errors
            'errorAction'=>'site/error',
        ),
        'log'=>array(
            'class'=>'CLogRouter',
            'routes'=>array(
                array(
                    'class'=>'CFileLogRoute',
                    'levels'=>'error, warning',
                ),
                // uncomment the following to show log messages on web pages

                array(
                    'class' => 'CWebLogRoute',
                    'enabled' => YII_DEBUG,
                    'levels' => 'error, warning, trace, notice',
                    'categories' => 'application',
                    'showInFireBug' => true,
                ),
            ),
        ),
        'mail' => array(
            'class' => 'ext.mail.YiiMail',
            'transportType' => 'smtp',
            'transportOptions' => array(
            'host' => '10.236.9.116',
            'username' => '',
            'password' => '',
            #'port' => 0,
                        'encryption'=>'ssl'
            ),
            'viewPath' => 'application.views.mail',
            'logging' => true,
            'dryRun' => false
        )
    ),

    // application-level parameters that can be accessed
    // using Yii::app()->params['paramName']
    'params'=>array(
        // this is used in contact page
        'adminEmail'=>'webmaster@example.com',
    ),
);
项目文件夹中的

index.php

<?php

// change the following paths if necessary
$yii=dirname(__FILE__).'/../../yii-1.1.12/framework/yii.php';
$config=dirname(__FILE__).'/protected/config/main.php';

// remove the following lines when in production mode
defined('YII_DEBUG') or define('YII_DEBUG',true);
// specify how many levels of call stack should be shown in each log message
defined('YII_TRACE_LEVEL') or define('YII_TRACE_LEVEL',3);

require_once($yii);
Yii::createWebApplication($config)->run();

1 个答案:

答案 0 :(得分:1)

您可以尝试使用Yii::trace()

,而不是Yii::log()
public function actionCallCommentForm($id='')
{
    $topic=Forum::model()->findByPk($id);
    Yii::log(CVarDumper::dumpAsString("TESTING!"));
    Yii::log(CVarDumper::dumpAsString($topic));
    $this->renderPartial('_commentform', array(
        'forum'=>$topic,
        'model'=>new Comment,
        //'view'=>array('view','id'=>$id),
        'view'=>'view',
    ));
}

我的日志配置是:

'log'=>array(
    'class'=>'CLogRouter',
    'routes'=>array(
        array(
            'class'=>'CFileLogRoute',
            'levels'=>'info, error, warning', // info is necessary, if you want to use Yii::log() without second parameter
        ),
    ),
),

它将Yii::log()函数传递给runtime/application.log文件的信息写入。

修改

在返回数组的main.php

,您需要拥有以下代码:

'preload'=>array('log'),

如果没有此配置选项,日志记录实用程序将无法正常工作。

编辑2:

使用Yii::log()时没有确定级别的第二个参数,默认级别为info 。在main.php配置文件中没有级别info,因此请将其添加到levels数组中的routes字符串。