Weave Code
Code Weaver
Helps Laravel developers discover, compare, and choose open-source packages. See popularity, security, maintainers, and scores at a glance to make better decisions.
Feedback
Share your thoughts, report bugs, or suggest improvements.
Subject
Message

Yii2 Dev Laravel Package

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).

View on GitHub
Deep Wiki
Context7

Getting Started

Minimal Setup

  1. Installation:

    composer require yiisoft/yii2-dev
    

    Ensure your composer.json includes the package under require-dev if using it only for development.

  2. First Use Case:

    • Debugging: Use yii\debug\Module for interactive debugging tools (e.g., database queries, logs, memory usage).
    • Code Profiling: Enable the profiler via Yii::$app->set('enableProfiling', true) and view results in the debug toolbar.
  3. Where to Look First:

    • Debug Toolbar: Accessible via http://your-app.dev/?debug=1 (if enabled in config/web.php).
    • Configuration: Check config/web.php for pre-configured debug tools (e.g., modules['debug']).

Implementation Patterns

Debugging Workflows

  1. Interactive Debugging:

    • Database Queries: Use the "Queries" tab in the debug toolbar to inspect slow queries.
    • Logs: View real-time logs via the "Logs" tab or configure yii\debug\LoggerTarget in config/web.php:
      'components' => [
          'log' => [
              'targets' => [
                  'debug' => [
                      'class' => 'yii\debug\LoggerTarget',
                      'categories' => ['yii\db\*'],
                      'levels' => ['error', 'warning'],
                  ],
              ],
          ],
      ],
      
  2. Profiling:

    • Enable profiling in config/web.php:
      'components' => [
          'profiling' => [
              'class' => 'yii\debug\Profiler',
              'settings' => [
                  'triggers' => [
                      'yii\db\Command::execute' => ['yii\debug\DbTrigger'],
                  ],
              ],
          ],
      ],
      
    • View results in the "Profiling" tab of the debug toolbar.
  3. Error Handling:

    • Customize error pages by configuring yii\web\ErrorHandler:
      'components' => [
          'errorHandler' => [
              'errorAction' => 'site/error',
              'exceptionAction' => 'site/error',
          ],
      ],
      

Integration Tips

  1. Conditional Debugging:

    • Disable debug tools in production by checking YII_ENV:
      if (YII_ENV_DEV) {
          $this->module->enabled = true;
      }
      
  2. Custom Debug Panels:

    • Extend 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();
          }
      }
      
    • Register in config/web.php:
      'modules' => [
          'debug' => [
              'panels' => [
                  'myService' => 'app\debug\panels\MyServicePanel',
              ],
          ],
      ],
      
  3. Asset Bundling:

    • Use 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'];
      }
      

Gotchas and Tips

Pitfalls

  1. Performance Overhead:

    • Debug tools add overhead. Disable them in production:
      if (!YII_ENV_DEV) {
          Yii::$app->set('enableProfiling', false);
      }
      
    • Avoid using yii\debug\Module in CLI applications (it’s web-only).
  2. Cache Invalidation:

    • Debug toolbar caches data (e.g., queries, logs). Clear cache manually if stale data appears:
      php yii cache/flush
      
  3. Key Conflicts:

    • If using multiple apps with shared cache (e.g., Redis), prefix keys to avoid collisions:
      $cache->set('app1_' . $key, $value);
      

Debugging Tips

  1. Xdebug Integration:

    • Configure php.ini for Xdebug:
      xdebug.mode=debug
      xdebug.start_with_request=yes
      
    • Use yii\debug\XdebugPanel for IDE integration.
  2. Log Filtering:

    • Filter logs by category/level in LoggerTarget:
      'targets' => [
          'debug' => [
              'categories' => ['yii\db\*', 'app\models\*'],
              'levels' => ['error', 'warning', 'info'],
          ],
      ],
      
  3. Custom Error Actions:

    • Handle errors gracefully by extending yii\web\ErrorAction:
      class CustomErrorAction extends \yii\web\ErrorAction {
          public function run() {
              if (YII_ENV_DEV) {
                  return parent::run();
              }
              return $this->controller->render('error');
          }
      }
      
    • Register in config/web.php:
      'components' => [
          'errorHandler' => [
              'errorAction' => 'site/custom-error',
          ],
      ],
      

Extension Points

  1. Custom Panels:

    • Extend yii\debug\Panel for domain-specific insights (e.g., queue status, external API calls).
  2. Profiling Triggers:

    • Add custom triggers to yii\debug\Profiler:
      'profiling' => [
          'settings' => [
              'triggers' => [
                  'app\services\MyService::doWork' => ['yii\debug\DbTrigger'],
              ],
          ],
      ],
      
  3. Asset Overrides:

    • Override debug assets in config/web.php:
      'modules' => [
          'debug' => [
              'assetBundle' => 'app\assets\DebugAssets',
          ],
      ],
      
  4. Environment-Specific Configs:

    • Use config/params-local.php for environment-specific debug settings:
      return [
          'enableProfiling' => YII_ENV_DEV,
          'debugPanels' => ['db', 'memory', 'time'],
      ];
      
Weaver

How can I help you explore Laravel packages today?

Conversation history is not saved when not logged in.
Prompt
Add packages to context
No packages found.
davejamesmiller/laravel-breadcrumbs
artisanry/parsedown
christhompsontldr/phpsdk
enqueue/dsn
bunny/bunny
enqueue/test
enqueue/null
enqueue/amqp-tools
milesj/emojibase
bower-asset/punycode
bower-asset/inputmask
bower-asset/jquery
bower-asset/yii2-pjax
laravel/nova
spatie/laravel-mailcoach
spatie/laravel-superseeder
laravel/liferaft
nst/json-test-suite
danielmiessler/sec-lists
jackalope/jackalope-transport