如何在开发 RESTful 应用程序时使用 Yii2 调试器?
How to use Yii2 debugger when developing RESTful application?
如指南中所述,我创建了 RESTful 控制器 UserController。
namespace app\controllers;
use yii\rest\ActiveController;
class UserController extends ActiveController
{
public $modelClass = 'app\models\User';
}
当我提出请求时 GET /users
,它起作用了。
但我不知道 Yii2 在幕后执行了哪些查询,也不知道它们会持续多久。
我能否以某种方式使用 Yii2 调试器来调试和分析查询?如果不是,那么替代方案是什么?
在调试器中查看 APIs
的请求
在你的 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 的默认操作,例如创建、更新、查看、索引、删除
在控制器中编写以下代码
/* 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 控制器 UserController。
namespace app\controllers;
use yii\rest\ActiveController;
class UserController extends ActiveController
{
public $modelClass = 'app\models\User';
}
当我提出请求时 GET /users
,它起作用了。
但我不知道 Yii2 在幕后执行了哪些查询,也不知道它们会持续多久。
我能否以某种方式使用 Yii2 调试器来调试和分析查询?如果不是,那么替代方案是什么?
在调试器中查看 APIs
的请求在你的 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 的默认操作,例如创建、更新、查看、索引、删除
在控制器中编写以下代码
/* 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;}