rest - 开发 RESTful 应用程序时如何使用 Yii2 调试器?

标签 rest debugging yii2 profiler

就像在指南中一样,我创建了 RESTful Controller UserController。

namespace app\controllers;

use yii\rest\ActiveController;

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

当我提出请求 GET /users 时,它​​起作用了。

但是我不知道 Yii2 在幕后执行了哪些查询,也不知道它们会持续多久。

我可以以某种方式使用 Yii2 调试器来调试和分析查询吗?如果没有,这个的替代方案是什么?

最佳答案

在调试器中查看 API 请求

  • 在你的 API 配置文件中添加这个 -
    $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 中 -
    defined('YII_DEBUG') or define('YII_DEBUG', true);
    defined('YII_ENV') or define('YII_ENV', 'dev');
    
  • 通过以下 URL 访问调试器-
    http://localhost/yii2-app/api/web/debug/default/view
    

  • 要更改 API 的默认操作,例如 - 创建、更新、查看、索引、删除,请在 Controller 中写入以下代码
    /* 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;}
    

    关于rest - 开发 RESTful 应用程序时如何使用 Yii2 调试器?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36284152/

    相关文章:

    api - 使用 CakePHP 通过 Put 发送 XML

    django - 在 Django rest 框架中转换 `ManyToManyField`

    rest - 为 http 请求/响应指定语言环境

    android - Android 和 App Engine 实际上是如何通信的?

    debugging - ANTLRWorks 调试 - 不同颜色的含义?

    php - 在 NetBeans 中设置 Yii 2 Web 框架编码标准

    python - Conda 和 Visual Studio Code 调试

    debugging - 如何让GDB在 "stepping into"时不打印函数参数值?

    php - 在 Yii 中,您可以用 JSON 而不是 HTML/XML 返回 HTTP 错误响应吗?

    php - yii2 - 从 View 重定向到另一个 View