yiisoft/yii2-dev
Yii 2 is a modern, high-performance PHP framework with secure defaults and flexible architecture. Works out of the box, scales from small apps to large systems, and is backed by extensive guides and API reference. Requires PHP 7.4+ (best on 8).
Installation:
composer require yiisoft/yii2-dev
Ensure your composer.json includes the package under require-dev if using it only for development.
First Use Case:
yii\debug\Module for interactive debugging tools (e.g., database queries, logs, memory usage).Yii::$app->set('enableProfiling', true) and view results in the debug toolbar.Where to Look First:
http://your-app.dev/?debug=1 (if enabled in config/web.php).config/web.php for pre-configured debug tools (e.g., modules['debug']).Interactive Debugging:
yii\debug\LoggerTarget in config/web.php:
'components' => [
'log' => [
'targets' => [
'debug' => [
'class' => 'yii\debug\LoggerTarget',
'categories' => ['yii\db\*'],
'levels' => ['error', 'warning'],
],
],
],
],
Profiling:
config/web.php:
'components' => [
'profiling' => [
'class' => 'yii\debug\Profiler',
'settings' => [
'triggers' => [
'yii\db\Command::execute' => ['yii\debug\DbTrigger'],
],
],
],
],
Error Handling:
yii\web\ErrorHandler:
'components' => [
'errorHandler' => [
'errorAction' => 'site/error',
'exceptionAction' => 'site/error',
],
],
Conditional Debugging:
YII_ENV:
if (YII_ENV_DEV) {
$this->module->enabled = true;
}
Custom Debug Panels:
yii\debug\Panel to create custom panels (e.g., for monitoring third-party services):
class MyServicePanel extends \yii\debug\Panel {
public $name = 'My Service';
public $icon = 'cloud';
public $defaultPos = 10;
public function collect() {
return ['status' => 'ok', 'last_check' => time()];
}
public function getData() {
return $this->collect();
}
}
config/web.php:
'modules' => [
'debug' => [
'panels' => [
'myService' => 'app\debug\panels\MyServicePanel',
],
],
],
Asset Bundling:
yii\web\AssetBundle for debug-specific assets (e.g., CSS/JS for debug tools):
class DebugAssets extends AssetBundle {
public $sourcePath = '@vendor/yiisoft/yii2-dev/assets';
public $css = ['debug.css'];
}
Performance Overhead:
if (!YII_ENV_DEV) {
Yii::$app->set('enableProfiling', false);
}
yii\debug\Module in CLI applications (it’s web-only).Cache Invalidation:
php yii cache/flush
Key Conflicts:
$cache->set('app1_' . $key, $value);
Xdebug Integration:
php.ini for Xdebug:
xdebug.mode=debug
xdebug.start_with_request=yes
yii\debug\XdebugPanel for IDE integration.Log Filtering:
LoggerTarget:
'targets' => [
'debug' => [
'categories' => ['yii\db\*', 'app\models\*'],
'levels' => ['error', 'warning', 'info'],
],
],
Custom Error Actions:
yii\web\ErrorAction:
class CustomErrorAction extends \yii\web\ErrorAction {
public function run() {
if (YII_ENV_DEV) {
return parent::run();
}
return $this->controller->render('error');
}
}
config/web.php:
'components' => [
'errorHandler' => [
'errorAction' => 'site/custom-error',
],
],
Custom Panels:
yii\debug\Panel for domain-specific insights (e.g., queue status, external API calls).Profiling Triggers:
yii\debug\Profiler:
'profiling' => [
'settings' => [
'triggers' => [
'app\services\MyService::doWork' => ['yii\debug\DbTrigger'],
],
],
],
Asset Overrides:
config/web.php:
'modules' => [
'debug' => [
'assetBundle' => 'app\assets\DebugAssets',
],
],
Environment-Specific Configs:
config/params-local.php for environment-specific debug settings:
return [
'enableProfiling' => YII_ENV_DEV,
'debugPanels' => ['db', 'memory', 'time'],
];
How can I help you explore Laravel packages today?