duande1146 2016-04-05 07:08
浏览 58
已采纳

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

I'm working a Yii project passed down to me. As I'm very new to the thing, my first instinct was to do output a whole bunch of values to get a sense as to where things come from and where they go.

So to that end, I tried using echo in the Controller. As in:

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',
    ));
}

This, however, does not work. And I wouldn't know how to output, say, $topic, anyway, if it did.

So I tried logging. Based on 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',
    ));
}

Upon checking the logs though (which does work for outputting errors) in runtime/application.log

The answer says The Application Log is shown below in every page. but I'm afraid that's not clear.

Any other ways to do this?

EDIT: More details:

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 in project folder:

<?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条回答 默认 最新

  • dqxhit3376 2016-04-05 09:24
    关注

    Instead of Yii::trace(), you can try to use 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',
        ));
    }
    

    My configuration for logging is:

    '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
            ),
        ),
    ),
    

    And it writes informations passed by Yii::log() function to runtime/application.log file.

    EDIT:

    in main.php in returning array you need to have this code:

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

    Without this configuration option, logging utility doesn't work.

    EDIT 2:

    When using Yii::log() without second parameter, which determines level, default level is info. In your main.php config file there is no level info, so please add it to levels string in routes array.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥20 西门子S7-Graph,S7-300,梯形图
  • ¥50 用易语言http 访问不了网页
  • ¥50 safari浏览器fetch提交数据后数据丢失问题
  • ¥15 matlab不知道怎么改,求解答!!
  • ¥15 永磁直线电机的电流环pi调不出来
  • ¥15 用stata实现聚类的代码
  • ¥15 请问paddlehub能支持移动端开发吗?在Android studio上该如何部署?
  • ¥20 docker里部署springboot项目,访问不到扬声器
  • ¥15 netty整合springboot之后自动重连失效
  • ¥15 悬赏!微信开发者工具报错,求帮改