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

off*_*ine 4 debugging rest profiler yii2

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

namespace app\controllers;

use yii\rest\ActiveController;

class UserController extends ActiveController
{
    public $modelClass = 'app\models\User';
}
Run Code Online (Sandbox Code Playgroud)

当我提出要求时GET /users,它会起作用.

但是我不知道Yii2在幕后执行了什么查询,我不知道它们能持续多久.

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

moh*_*hit 10

在Debugger中查看API的请求

  1. 在您的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;
    
    Run Code Online (Sandbox Code Playgroud)
  2. 在API文件夹的web/index.php中 -

    defined('YII_DEBUG') or define('YII_DEBUG', true);
    defined('YII_ENV') or define('YII_ENV', 'dev');
    
    Run Code Online (Sandbox Code Playgroud)
  3. 通过以下URL访问调试器 -

    http://localhost/yii2-app/api/web/debug/default/view
    
    Run Code Online (Sandbox Code Playgroud)

要更改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;}
Run Code Online (Sandbox Code Playgroud)

  • 这基本上就是我需要做的所有事情,但我不知道我可以:`localhost/api/debug/default/view`.我的API是独立的应用程序,我不知道我可以像这样访问调试器.谢啦 ! (2认同)