yiisoft/yii2-dev
Yii 2 is a modern, fast, secure, and flexible PHP framework with sensible defaults out of the box. It provides strong foundations for web apps and APIs, with extensive documentation, guides, and class 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.
Note: Verify PHP version compatibility (Yii 2.0.55+ requires PHP 7.4+; templates now target newer PHP versions).
First Use Case:
yii\debug\Module for interactive debugging tools (e.g., database queries, logs, memory usage).
Yii::$app->set('enableProfiling', true) and access the toolbar at http://your-app.dev/?debug=1.filterSelector Closure support in yii\grid\GridView for dynamic filtering:
'filterSelector' => function($model, $attribute) {
return $model->getAttributeFilters()[$attribute] ?? null;
},
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']).View::renderPhpFile() and ErrorHandler::renderFile() for updated parameter isolation (CVE-2026-39850).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',
],
],
ErrorHandler::renderFile() now isolates internal variables to prevent path collisions.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',
],
],
],
GridView Enhancements:
filterSelector Closure support for dynamic filtering:
GridView::widget([
'dataProvider' => $dataProvider,
'filterSelector' => function($model, $attribute) {
return $model->getDynamicFilters()[$attribute] ?? null;
},
]);
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);
Security Fixes:
View::renderPhpFile() and ErrorHandler::renderFile() now isolate internal variables. Ensure custom views/handlers comply with this change to avoid path collisions.PHP Version Compatibility:
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',
],
],
PHPDoc Annotations:
/**
* @param array<string, mixed> $data
* @return array<string, mixed>
*/
public function processData(array $data): array { ... }
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'],
];
GridView Filtering:
filterSelector:
'filterSelector' => function($model, $attribute) {
return $model->getAttributeFilters()[$attribute] ?? null;
},
How can I help you explore Laravel packages today?