cms
cms copied to clipboard
Can't query a single entry or term on URI (REST-API)
Bug description
When calling the API endpoint for a single entry or term, an exception is thrown and no result is returned.
Error message:
Object of class Statamic\Entries\Entry could not be converted to string
After an in-depth analysis, I found the following:
Resource controllers are used, which already give the complete collection as a parameter in the controller. However, it is assumed in the controller that an ID comes in and then a ::find() query is made on the facade.
This leads to an object to string collision.
Translated with DeepL.com (free version)
How to reproduce
Activate the API for a collection and call the API endpoint for an entry with the ID.
Logs
No response
Environment
Environment
Application Name: Konkav & K16
Laravel Version: 11.24.1
PHP Version: 8.3.11
Composer Version: 2.7.7
Environment: local
Debug Mode: ENABLED
URL: k16-konkav-cms.test
Maintenance Mode: OFF
Timezone: UTC
Locale: de
Cache
Config: NOT CACHED
Events: NOT CACHED
Routes: NOT CACHED
Views: CACHED
Drivers
Broadcasting: log
Cache: file
Database: mysql
Logs: stack / single
Mail: smtp
Queue: sync
Session: file
Statamic
Addons: 7
Sites: 5 (Konkav - DE, Konkav - EN, K16 - DE, K16 - EN, K16 - PL)
Stache Watcher: Enabled
Static Caching: Disabled
Version: 5.26.0 PRO
Statamic Addons
aryehraber/statamic-uuid: 2.3.0
jonassiewertsen/statamic-documentation: 2.0.0
morethingsdigital/personio: dev-main
morethingsdigital/statamic-nextjs: 0.0.1
studio1902/statamic-peak-browser-appearance: 3.5.0
studio1902/statamic-peak-seo: 8.16.0
studio1902/statamic-peak-tools: 6.3.2
Installation
Existing Laravel app
Additional details
I create a PR that fixes the problem.
Which endpoint are you seeing the error on?
@duncanmcclean Sorry for my late reply, but I've been very busy the last few days.
I have now looked at the whole thing again in detail and also checked my PR again and tried to find out why the tests are failing due to my changes.
Specifically, it's about the show endpoint of an
- Entry (https://statamic.dev/rest-api#entry)
- Taxonomy Term (https://statamic.dev/rest-api#taxonomy-term)
Currently, the following error is displayed when I call one of the two endpoints
Object of class Statamic\Entries\Entry could not be converted to string
I have patched my changes into my project for testing and called the endpoints there. The manual tests all worked as desired and the endpoints also delivered the correct results.
After some error analysis as to why the tests in the pipeline could not be executed, I noticed the following difference.
For the sake of simplicity, I will use the entry endpoint, but this is also the case with the taxonomy term endpoint.
Two parameters are transferred in the show method of the controller Statamic\Http\Controllers\API\CollectionEntriesController:
$collection$handle(after my change it is called$entry)
If I now execute the request with my changes, I get an instance of Statamic\Entries\Collection for $collection and an instance of Statamic\Entries\Entry for $handle($entry).
For comparison in the test, an instance of Statamic\Entries\Collection is also passed for $collection and a string for $handle($entry).
And this is exactly what the error message currently says. That it is trying to convert an object to a value that is actually a string.
This leads to the discrepancy and also to the fact that my changes work in a real application and the test fails. Personally, I have to say that I don't really have an overview of the test and would like your help.
Can you provide an example of the URL you're making a request to which isn't working?
Sure:
<domain>/api/collections/pages/entries/e040bead-54de-453b-8ae0-896136e94be7
or
<domain>/api/taxonomies/news_categories/terms/brand
Thanks!
Currently, the following error is displayed when I call one of the two endpoints
Hmm, I can't seem to reproduce this error myself. π€
Do you have any custom route model bindings which could be conflicting with Statamic's? What does your AppServiceProvider look like?
Is your sandbox project laravel v11 or laravel v10?
Laravel 11
I have found the problem. Please change the variable in your .env to the following value CP_ROUTE=ββ or add it. If I have a value like βadminβ or βcpβ, the above two API endpoints will work.
In our use case we use Statamic Headless and don't need a frontend, so we decided to make the CMS admin panel directly available on the β/β path.
This issue has not had recent activity and has been marked as stale β by me, a robot. Simply reply to keep it open and send me away. If you do nothing, I will close it in a week. I have no feelings, so whatever you do is fine by me.