Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Allow API to be queried by path / url #1297

Open
ArronKing opened this issue Jan 22, 2025 · 0 comments
Open

Allow API to be queried by path / url #1297

ArronKing opened this issue Jan 22, 2025 · 0 comments

Comments

@ArronKing
Copy link

Context:

  • I’m using Statamic as a headless CMS and have a collection with a nested Link Fieldtype that provides URLs in its API response (e.g., link_url: "/example/path").
  • The issue is that the API doesn’t natively support querying by URL across collections. This makes resolving entries by path challenging, especially when the target could belong to various collections (pages, news, etc.).
  • Currently, the workaround involves enabling URL as a filter and querying like:
    /api/collections/pages/entries?filter[url:is]=/example/path.

However, this approach breaks or requires further calls when the URL is from another collection.

Ideal Outcome:

  • We should be able to use the link_url value directly to resolve content dynamically, maintaining clean and user-friendly URLs.
  • For example, a URL like /content/nested-example/another-level/example-page in my app would map to /nested-example/another-level/example-page in Statamic. The app would fetch the associated entry without needing prior knowledge of the collection or entry type.

Proposed solution:
Something along the lines of route I added to Statamic:
routes/api.php

Route::get('pages/{path}', [PageController::class, 'show'])->where('path', '.*')->name('statamic.api.pages.show');

PageController.php

<?php

namespace App\Http\Controllers\Api;

use Statamic\Http\Controllers\API\ApiController;
use Statamic\Support\Str;
use Statamic\Facades\Entry;
use Statamic\Http\Resources\API\EntryResource;
use Statamic\Exceptions\NotFoundHttpException;

class PageController extends ApiController
{
    public function show($path)
    {
        $this->abortIfDisabled();

        $path = Str::start($path, '/');
        $path = Str::chopEnd($path, '/');

        /** @var \Statamic\Structures\Page */
        $page = Entry::findByUri($path);
        throw_unless($page, new NotFoundHttpException);
        $entry = $page->entry();

        $this->abortIfUnpublished($entry);

        return app(EntryResource::class)::make($entry);
    }
}

Extra information:

The Statamic team kindly added this PR to help with this issue: statamic/cms#11375 - which is aimed for v6. Personally, my ideal solution would be both of these improvements to the API. We'd have enough information on the link to be able to handle it correctly and we would be able to use the URLs users create in Statamic within external apps.

Keen to hear thoughts / how people are handling this at the moment.

Thanks for reading!

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

No branches or pull requests

1 participant