STDOUT MVC API: Handling Requests Into Responses
This high-performance API elegantly and efficiently translates client (page/resource) requests into server responses using a dialect of MVC paradigm.
How does it work?
This API, like any other web MVC API, implements Model-View-Controller pattern. API logic is bootstrapped by a FrontController class. FrontController's job is to translate client page request (read from PHP superglobals) into a HTTP response based on contents of user-defined xml file where your application is set up. Instead of implementing translation logic itself, it delegates to components dedicated to a particular aspect of this process. These components are of two types:
- Fully defined by API, ran by instantiation:
Fundamental components designed to be atomic (usable by themselves):
- Application: Collects immutable data about your application based on content of user-defined xml file
- Request: Collects immutable data about client request based on contents of PHP superglobals.
- Response: Collects mutable data necessary for response (usually set up by controllers) as well as logic necessary to resolve and display output.
Helper components employed by FrontController:
- PageValidator: binds data collected by Request and Application objects to determine real page requested, path parameters (if any) and requested response format.
- ListenerLocator: locates user defined event listeners from <listeners> tag in folder defined by application > paths > listeners tag
- ControllerLocator: locates user defined Controller from "controller" attribute of matching <route> tag in folder defined by application > paths > controllers tag
- ViewResolverLocator: locates user defined ViewResolver from "class" attribute of matching <format> tag in folder defined by application > paths > resolvers tag
Optional user-defined components whose blueprints are defined by API, implementing Runnable, ran by instatiation and run() method execution:
- Event listeners whose classes are defined as <listener> tag, executed in the order they are set in in <listeners> tag on application lifecycle events:
- ApplicationListener: user-defined classes that extend it append mutable data to Application object via attributes or use already collected data for another purpose.
- RequestListener: user-defined classes that extend it append mutable data to Request object via attributes or use already collected data for another purpose.
- ResponseListener: user-defined classes that extend it append mutable data to Response object via attributes, modify collected data or use it for another purpose.
- Various other dependencies that, unlike listeners, are pretty much mandatory in most use cases:
- Controller: user-defined classes that extend it perform page controller logic such as interrogating models then sending data to Response based on page requested
- ViewResolver: user-defined classes that extend it compile view (response body) into output stream based on response content type and files on disk (eg: templates).
All classes inside API belong to namespace Lucinda\MVC\STDOUT! User defined components need not (and should not) use that namespace.
What are the steps taken to issue response to request?
The order in which FrontController performs its operations is:
- Constructs Application object based on xml file.
- Uses ListenerLocator to locate then instances and runs user-defined ApplicationListener classes in the order they were set in <listeners> tag.
- Constructs Request object based on client request information collected by PHP superglobals.
- Runs API-defined PageValidator to match contents of Request with Application objects and detect requested: page, path parameters, content type.
- Uses ListenerLocator to locate then instances and runs user-defined RequestListener classes in the order they were set in <listeners> tag.
- Constructs Response object based on Application and Request information.
- Uses ControllerLocator to locate then instances and runs user-defined Controller class that matches requested page based on Application, Request and Response objects.
- If response hasn't been disabled via disable() method @ Response:
- Displays response back to caller by running commit() method @ Response object
How can I install and use it?
To learn how to install and use this API, follow this step-by-step guide!