Skip to content

Port to Craft 6 (native, adapter-free) via craft-6-plugin-porting skill - #204

Draft
lukeholder wants to merge 1 commit into
4.xfrom
5.x-via-skill
Draft

lukeholder wants to merge 1 commit into
4.xfrom
5.x-via-skill

Conversation

@lukeholder

Copy link
Copy Markdown
Member

Summary

Ports Element API to Craft 6 as a fully native plugin. It does not require craftcms/yii2-adapter.

  • Requires craftcms/cms ^6.0.0 and PHP ^8.5.
  • Moves to the CraftCms\ElementApi namespace with Laravel-style folders (Http/Controllers, Events, Contracts, Resources, Transformers, Serializers, Support).
  • Native Plugin base class and PluginSettings. The endpoints in config/craft/element-api.php are registered as Laravel routes at boot, using Laravel route syntax (news/{entryId}.json). Endpoint functions can type-hint Illuminate\Http\Request, and int-hinted parameters only match digits.
  • DefaultController is now an invokable ElementApiController. Response caching uses ElementCaches and the dependency-aware cache (still tagged element-api, and invalidated when an element is saved). cache: true uses the cacheDuration setting as its TTL, as before.
  • Response headers are set with ResponseHeaders::add(). API responses no longer set cookies.
  • DataEvent and EVENT_BEFORE_SEND_DATA are replaced by the SendingData Laravel event (clean break, no compatibility layer).
  • Registers the "Element API responses" option in Utilities → Clear Caches.
  • Removes the generateTransformsBeforePageLoad override, the pathParam guard and the Yii PHPStan stub.
  • Adds UPGRADE.md and updates the README and CHANGELOG.

Testing

Tested in a Craft 6.0.0-alpha.20 host with no adapter and no yiisoft/yii2:

  • list, paginated, single (route param), cached, JSON Feed and JSONP endpoints return byte-identical bodies and headers to the same endpoints running on the adapter (Stage 1 of the port)
  • OPTIONS preflight with a CORS header set from an endpoint function, 404 and 500 JSON error responses, no Set-Cookie, X-Robots-Tag: none
  • the response cache is stored, expires after its TTL, and is invalidated when an entry is saved
  • plain, pretty and JSONP encoding compared with yii\helpers\Json on <, &, quotes, slashes and unicode
  • paginate: false, contentType, pretty, resourceKey, meta, pageParam, and the dataArray and jsonApi serializers
  • Request injection into an endpoint function, the deprecated first setting, an invalid elementType
  • the SendingData event, the Clear Caches option, a request from a logged-in admin
  • install, uninstall and reinstall

Not verified / follow-ups

  • PHPStan and ECS were not run. ecs.php, phpstan.neon, the dev dependencies, composer.lock and .github/workflows/ci.yml still target Craft 4/5.
  • Not exercised: a custom transformer class using includes and excludes (the README example), a custom class resource, the live-preview cache bypass, and serving requests from a route cache (route:cache itself ran fine).
  • As of Craft 6.0.0-alpha.20, categories, tags and global sets have no native Craft 6 classes (they only exist in the adapter), so they can't be queried without it yet. The README says so.
  • php artisan config:cache fails in a project that uses endpoint functions, because closures can't be serialized (documented in the README and UPGRADE.md).
  • Only a single site was tested, and Craft 6 is still in alpha.
  • This repo has no 5.x branch, so the PR targets 4.x.

🤖 Generated with Claude Code

… skill

- Requires craftcms/cms ^6.0.0 and PHP ^8.5; no craftcms/yii2-adapter
- Moves to the CraftCms\ElementApi namespace with Laravel-style folders
- Native Plugin and PluginSettings; endpoints from config/craft/element-api.php are registered as Laravel routes
- Invokable ElementApiController; caching uses ElementCaches and the dependency-aware cache, with cacheDuration as the default TTL
- Response headers via ResponseHeaders::add(); API responses no longer set cookies
- DataEvent / EVENT_BEFORE_SEND_DATA replaced by the SendingData event
- Adds UPGRADE.md; updates the README and CHANGELOG

Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant