element-api
element-api copied to clipboard
404 Not Found — JSON URL / UrlHelper
Description
I'm using the UrlHelper to generate JSON URLs for entries but the generated URL doesn't seem to work.
Steps to reproduce
'projects.json' => function () {
Craft::$app->getResponse()->getHeaders()->set('Access-Control-Allow-Origin', '*');
return [
'elementType' => Entry::class,
'criteria' => [
'section' => 'projects',
],
'paginate' => false,
'transformer' => function (Entry $entry) {
return [
'id' => $entry->id,
'slug' => $entry->slug,
'jsonUrl' => UrlHelper::url("project/{$entry->slug}.json"),
];
},
];
},
'project/<entrySlug>.json' => function ($entrySlug) {
Craft::$app->getResponse()->getHeaders()->set('Access-Control-Allow-Origin', '*');
return [
'elementType' => Entry::class,
'criteria' => [
'section' => 'projects',
'slug' => $entrySlug,
],
'one' => true,
'transformer' => function (Entry $entry) {
return [
'id' => $entry->id,
'title' => $entry->title,
'slug' => $entry->slug,
];
},
];
},
Additional info
- Craft version: 4.3.4
- PHP version: 8.2
- Database driver & version: MySQL
- Plugins & versions: ElementAPI 3.0.1.1
Can you add any detail about how it doesn't work and your particular config? Are the correct URLs being generated? Or are you getting an unexpected URL?