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

API: Link pages to API counterparts #647

Open
g105b opened this issue May 2, 2024 · 0 comments
Open

API: Link pages to API counterparts #647

g105b opened this issue May 2, 2024 · 0 comments
Projects

Comments

@g105b
Copy link
Member

g105b commented May 2, 2024

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.

@g105b g105b added this to Backlog in Overview via automation May 5, 2024
@g105b g105b moved this from Backlog to Planned in Overview May 5, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
Overview
Planned
Development

No branches or pull requests

1 participant