Skip to content

API: Link pages to API counterparts #647

Open

Description

@g105b

A very common web development technique is to design an API first, and then link it to the "front end" via lots of slow ajax calls.

This technique can still be achieved in WebEngine, but because WebEngine is completely server-side, this can be handled automatically.

For example, the following API endpoints can be created:

  • api/index - index API will list countries
  • api/@country-name e.g. /morocco - list cities within country
  • api/@country-name/@city-name e.g. /morocco/rabat - list places within city
  • api/@country-name/@city-name/@place-name e.g. /morocco/rabat/national-zoo - show details on place

Once the API endpoints are created, the application's data can be used in other applications, but what if we then wanted to provide a web interface to the data, too?

It would make sense to have the pages follow the same URL structure:

  • page/index
  • page/@country-name
  • page/@country-name/@city-name
  • page/@country-name/@city-name/@place-name

These pages will use HTML to structure the content, with data-bind attributes to indicate where the data should be bound.

The PHP should not need to "build" the data up again, because this is already done within the API's PHP logic files. Instead, the go function could be marked with an attribute #[AutoApiPage] or something similar. The presence of this attribute will automatically grab the data that an equivalent API call would respond with, and automatically performs a bind operation to the DocumentBinder. Pages might not have the exact same URL as their API counterparts, so the API endpoint can be passed into the attribute, like #[AutoApiPage("/@country-name/@city-name") for example.

Then, if more manipulation is required to the page, this can be done in the go function as usual.

Submitting a form on a page will trigger a do function. Hooking a do function to the AutoApiPage attribute will call it as a POST request, but if there's multiple do functions on the page - one for updating, one for creating, the HTTP method can be passed in as a parameter like #[AutoApiPage(HttpMethod::PUT)]. Using type juggling, these attribute parameters should be able to be passed in any order.

Activity

  1. added theissue type on Oct 3, 2025
  2. added this to the v6 milestone on Jul 25, 2026
  3. g105b commented on Nov 12, 2026

    @g105b
    MemberAuthor

    That's nice for when there's specific designs to each of the different pages, like place-name, city-name, country-name, index. But when the entire page and URL structure is based fully off the API, there could be a new @ file that describes this - allowing a single HTML page to be populated with data via the API, with PHP just there to fill in the blanks. Needs scoping out.

  4. g105b commented on Nov 18, 2026

    @g105b
    MemberAuthor

    This should be implemented in stages: first extract in-process API execution with a structured result, then add an explicit read-only page attribute, then form actions. The proposed single-template @ file should follow as a separate routing feature using the same execution/binding service.

    On current master (c737ed0):

    • DefaultRouter::page() and ::api() already resolve the same public URI against separate trees. Content negotiation chooses the API; api/ is not necessarily a public URL prefix.
    • LogicExecutor::invoke() supports functions and class methods. Function attributes are currently appended to diagnostic strings after invocation; they do not execute behaviour, and method attributes are not inspected. This needs a real pre-invocation hook for both forms.
    • Dispatcher combines logic execution with streaming, redirects and JSON error completion. Calling another full dispatcher directly risks completing the outer response or leaking API state into the page.

    1. Extract an API execution service

    Introduce something like ApiExecutor::execute(...) -> ApiResult, shared by public API dispatch and linked pages. Resolve API assemblies through the routing machinery, including custom routing, _common logic, dynamic parameters, validation and authorisation. Select API routing explicitly instead of inheriting the browser's HTML Accept header.

    Each execution needs isolated request-specific bindings (Request, Response, Input, DynamicPath, JSONDocument), while deliberately sharing the current identity/session and appropriate application services. Return data, status and response metadata without streaming, exiting or overwriting the page's container state. Public API dispatch renders that result; the page adapter binds it. Add a supported structured-data export to JSONDocument if needed, rather than encoding and decoding JSON to recover its data.

    2. Add #[AutoApiPage] for GET

    An omitted path means the current public URI. An explicit path such as /@country-name/@city-name substitutes values from the page's DynamicPath, then resolves the concrete URI against API routing. Missing parameters and endpoints should produce clear errors.

    Run the adapter immediately before the decorated page go() callback, after binder initialisation, so the callback can customise the populated DOM. Specify ordering relative to go_before and inherited _common callbacks, and prevent unintended repeated execution through inherited declarations.

    Define the binding contract: object fields use bindData, collections use the existing list/template mechanisms, and nested collections need a documented mapping. Define handling for authentication failures, validation errors, redirects and empty/204 results; these must not silently become ordinary page data. Keep HTML content type and translate only intentional API response metadata.

    3. Add form actions separately

    Attributes on selected do_* callbacks can default to POST with an explicit method override. Verify the outer form's CSRF token before mutation, forward body/query/files deliberately, and separate page-control fields such as do from API payload data. Preserve API authorisation checks.

    The dispatcher currently runs do_* followed by go, so define action-result binding and any subsequent GET refresh explicitly: the mutation must execute exactly once. I would favour named path: and method: arguments. If either positional order remains a requirement, normalise permitted argument types in the constructor and reject duplicates/ambiguity.

    4. Scope the single-template @ file independently

    The latest comment describes routing/template selection, whereas the attribute describes callback behaviour. Both can reuse the API executor and binding adapter. The file needs explicit subtree-matching, template-selection and precedence rules before choosing its syntax. Explicit page files should win, and fallback should require a matching API endpoint so unknown URLs still return 404.

    Start with integration coverage comparing public API data and bound HTML for the country/city/place examples. Cover remapped paths, function/method attributes, inherited logic, nested lists, missing routes, authorisation failures, redirects, and isolation of API/page response state. Add CSRF rejection and exactly-once mutation tests with the action phase.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

      Milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions