How getkirby/kirby works
Project Overview and API Purpose
• Kirby is a CMS where the “API” is not a separate “REST server” in the conventional sense, but rather a set of PHP classes (in the src/Api directory) that expose content, models, and collections, and allow the Panel (admin interface) to interact with the core system.
• The API design is responsible for interfacing between HTTP requests (e.g., Ajax calls from the admin panel or frontend requests) and the underlying content management system in src/Cms, as well as integrating business logic (e.g., authentication, file operations, model and blueprint handling).
Main Components Relevant to the API
a. Client Side / Admin Panel Frontend
• Panel’s frontend code is organized in the “panel/src/api” folder along with view components (Vue files) that are used to build the user interface.
• Example modules include auth.js, get.js, files.js, languages.js, pages.js, etc.
• This layer makes HTTP calls (e.g., via AJAX or fetch) to API endpoints.
b. Server-side API Implementation (PHP)
• The “src/Api” directory in the core contains key PHP classes:
• Api.php: Likely acts as the base class or dispatcher for API requests.
• Collection.php and Model.php: These classes encapsulate data structures (such as pages, users, files) exposed by the API.
• Many operations in Kirby (fetching pages, collections of content, and handling blueprints) are routed through these API classes.
c. HTTP Routing and Request Handling
• The “src/Http” folder plays a pivotal role in routing incoming HTTP requests to the appropriate API handlers.
• Router.php, Request.php, and Response.php classes provide the low-level mechanics.
• The API design takes advantage of these modules to parse requests and construct responses (often in JSON format) for the admin panel and external calls.
d. Core CMS and Business Logic
• The API is supported by a robust set of CMS classes found under “src/Cms” which handle content, pages, users, and plugins.
• The API layer acts as a façade over these subsystems, translating HTTP/JSON calls into actions like “open this page”, “save that content”, or “validate user permissions.”
Architectural Patterns and Relationships
• The Presentation Layer: The Panel’s UI components written in Vue (within panel/src) that interact with the API layer.
• The API/Controller Layer: The PHP files in src/Api that expose endpoints and interconnect with business logic.
• The Domain/Business Layer: Kirby’s core modules (src/Cms, src/Http, src/Database, etc.) that process content and configuration.
• The Data/Filesystem Layer: Kirby’s file-based content storage (notice “blueprints”, “config”, and “content” folders) that holds page, language, and user data.
• Requests from the admin panel (client-side API calls) are handled by the HTTP routing and then passed to the API controllers.
• The API controllers then interface with core CMS classes to retrieve or update data.
• Responses flow back via the HTTP response mechanism to the client.
Detailed Focus on the API Design
• A block for “API Handler (src/Api/Api.php)” as the central dispatcher.
• Subordinate API components (e.g., Collection, Model classes) that encapsulate content data.
• Interface between the API layer and the core business logic implemented in “src/Cms” (for operations such as page handling, user authentication, etc.).
• Additionally, note any patterns for extensibility:
• Kirby leverages blueprints and configuration files (see folders in config/ and blueprints in src/Blueprint) to make its API adaptive.
Technologies and Frameworks
• The system is built in PHP (using Composer for dependency management) accompanied by JavaScript/Vue for the Panel.
• No heavy external framework is used; Kirby implements its own lightweight MVC-like structure.
Final Summary
• The flow of an HTTP API call initiated from the admin panel or frontend.
• The handoff at the HTTP Router (src/Http) to the API layer (src/Api).
• The subsequent invocation of core CMS functionality (src/Cms) and retrieval or update of file-based content.
• The data flow back to the client via the HTTP response, completed by the API returning a JSON payload.
• Extension points where plugins or custom blueprints can modify or extend API behavior.