craftcms/yii2-adapter
Compatibility adapter that lets Yii2-based code run within Craft CMS by bridging key Yii2 services and conventions. Useful for plugins or libraries that expect a Yii2 environment, easing migration and reuse across Craft projects.
craftcms/yii2-adapter package provides a Yii 2 compatibility layer for Craft CMS 6 packages, enabling legacy Yii 2-based packages to integrate with Craft’s modern PHP/PSR-compliant ecosystem. This is valuable for:
yii\base\* and yii\di\* components to Craft’s environment. Risk: Potential friction if the adapter doesn’t fully align with Craft’s DI container or event system.ServiceProvider or Module system.yii\base\Event) must be mapped to Craft’s Event system or handled via adapters.yii\db\* may conflict with Craft’s craft\db\*; likely requires a shared DB layer or abstraction.| Risk Area | Description | Mitigation Strategy |
|---|---|---|
| Dependency Conflicts | Yii 2 and Craft 6 may pull conflicting versions of monolog, yii\base, etc. |
Use composer.lock pinning or replace directives in composer.json. |
| Performance Overhead | Adapter layer adds indirection (e.g., proxying Yii 2 services through Craft’s DI container). | Benchmark critical paths; consider direct integration for high-traffic components. |
| Event System Gaps | Yii 2’s yii\base\Event may not map cleanly to Craft’s Event or Symfony’s EventDispatcher. |
Implement a custom event bridge or use Craft’s ElementEvent for shared logic. |
| Security Risks | Yii 2 packages may have outdated security practices (e.g., no prepared statements). | Audit dependencies; enforce Craft’s security policies (e.g., CSRF, input validation). |
| Long-Term Viability | Craft 6 may evolve away from Yii 2 compatibility in future versions. | Plan for gradual migration (e.g., rewrite critical components first). |
yii\gii, yii\console) that must be preserved?yii\db, yii\cache) or just a subset?craft\elements) that need Yii 2 integration?craftcms/cms, suggesting it’s tightly coupled with Craft’s internals.composer.json rules to avoid conflicts (e.g., conflict or replace directives).composer require craftcms/yii2-adapter
ServiceProvider:
use craft\services\Services;
use yii\base\Application as YiiApp;
Services::register('yiiAdapter', function () {
return new YiiApp(YiiApp::config());
});
yii\db\ActiveRecord and map to Craft’s Model or Element.Service classes.YiiEventToCraftEvent).Db service for queries to avoid Yii 2’s yii\db\Connection.| Component | Compatibility Notes |
|---|---|
| Yii 2 Models | Can be used via adapter, but Craft’s Element system may not integrate natively. |
| Yii 2 Services | Must be wrapped in Craft Service classes or accessed via the adapter’s facade. |
| Yii 2 Events | Requires custom mapping to Craft’s Event system or Symfony’s EventDispatcher. |
| Yii 2 Console | Possible via yii\console\Controller, but Craft’s CLI may need custom routing. |
| Yii 2 Gii | Unlikely to work without deep integration (Craft 6 doesn’t support Gii by default). |
| Caching | Yii 2’s yii\caching\* may conflict with Craft’s craft\cache\; prefer Craft’s cache. |
composer update may break the adapter.How can I help you explore Laravel packages today?