开发RESTful应用程序时如何使用Yii2调试器? [英] How to use Yii2 debugger when developing RESTful application?

查看:107
本文介绍了开发RESTful应用程序时如何使用Yii2调试器?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

就像指南中一样,我创建了RESTful控制器UserController。

Like in the guide, I have created RESTful controller UserController.

namespace app\controllers;

use yii\rest\ActiveController;

class UserController extends ActiveController
{
    public $modelClass = 'app\models\User';
}

当我发出请求时, GET / users ,它可以工作。

And when I make request GET /users, it works.

但是我不知道Yii2在后台执行什么查询,而且我不知道它们能持续多久。

But I have no idea what queries does Yii2 execute behind the scene, and I do not know how long do they last.

我可以以某种方式使用Yii2调试器来调试和配置查询吗?如果不是,那么还有什么替代方法?

Can I somehow use Yii2 debugger to debug and profile queries ? If not, what is the alternative for this ?

推荐答案

要在Debugger中查看API的请求


  1. 将此添加到您的API配置文件中-

  1. Add this in you API config file -

$config = [
    'id' => 'app-api',
    'basePath' => dirname(__DIR__),    
    'bootstrap' => ['log'],
    ......
    ....
]
if (YII_ENV_DEV) {
    // configuration adjustments for 'dev' environment
    $config['bootstrap'][] = 'debug';
    $config['modules']['debug'] = [
        'class' => 'yii\debug\Module',
        'allowedIPs' => ['your_ip_address'], // accessible to this ip address only
    ];

    $config['bootstrap'][] = 'gii';
    $config['modules']['gii'] = [
        'class' => 'yii\gii\Module',
    ];
}

return $config;


  • 在API文件夹的web / index.php中-

  • In web/index.php of API folder -

    defined('YII_DEBUG') or define('YII_DEBUG', true);
    defined('YII_ENV') or define('YII_ENV', 'dev');
    


  • 通过下面的URL访问调试器-

  • Access debugger by below URL-

    http://localhost/yii2-app/api/web/debug/default/view
    


  • 要更改API的默认操作,例如-创建,更新,查看,索引,删除
    ,请在控制器中编写以下代码

    /* Declare actions supported by APIs (Added in api/modules/v1/components/controller.php too) */
        public function actions(){
            $actions = parent::actions();
            unset($actions['create']);
            unset($actions['update']);
            unset($actions['delete']);
            unset($actions['view']);
            unset($actions['index']);
            return $actions;
        }
    
        /* Declare methods supported by APIs */
        protected function verbs(){
            return [
                'create' => ['POST'],
                'update' => ['PUT', 'PATCH','POST'],
                'delete' => ['DELETE'],
                'view' => ['GET'],
                'index'=>['GET'],
            ];
        }
        public function actionCreate(){echo "in create action";die;}
    

    这篇关于开发RESTful应用程序时如何使用Yii2调试器?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

    查看全文
    登录 关闭
    扫码关注1秒登录
    发送“验证码”获取 | 15天全站免登陆