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

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

问题描述

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

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

namespace appcontrollers;

use yii
estActiveController;

class UserController extends ActiveController
{
    public $modelClass = 'appmodelsUser';
}

当我发出请求 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 for APIs 中查看请求

  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' => 'yiidebugModule',
        'allowedIPs' => ['your_ip_address'], // accessible to this ip address only
    ];

    $config['bootstrap'][] = 'gii';
    $config['modules']['gii'] = [
        'class' => 'yiigiiModule',
    ];
}

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天全站免登陆