Repository navigation
Description
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 countriesapi/@country-namee.g. /morocco - list cities within countryapi/@country-name/@city-namee.g. /morocco/rabat - list places within cityapi/@country-name/@city-name/@place-namee.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/indexpage/@country-namepage/@country-name/@city-namepage/@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
g105b commentedon Nov 12, 2026
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.g105b commentedon Nov 18, 2026
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.Dispatchercombines 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,_commonlogic, dynamic parameters, validation and authorisation. Select API routing explicitly instead of inheriting the browser's HTMLAcceptheader.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 toJSONDocumentif needed, rather than encoding and decoding JSON to recover its data.2. Add
#[AutoApiPage]for GETAn omitted path means the current public URI. An explicit path such as
/@country-name/@city-namesubstitutes values from the page'sDynamicPath, 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 togo_beforeand inherited_commoncallbacks, 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 asdofrom API payload data. Preserve API authorisation checks.The dispatcher currently runs
do_*followed bygo, so define action-result binding and any subsequent GET refresh explicitly: the mutation must execute exactly once. I would favour namedpath:andmethod: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 independentlyThe 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.