Apex Software Platform
Request Routing (http / cli)
All incoming requests to Apex are handled either via HTTP or CLI (command line). Regardless of request origin, the app class remains the main / central class which helps facilitate the request and response.
Http / Middleware Controllers
How the HTTP requests are routed depends directly on the first segment of the URI, and the http / middleware adapters installed within the /src/core/service/http_requests/ directory. If there is a adapter named the same as the first segment of the UIR, the request will be passed off to it. Otherwise the request will be passed to the default "http.php" adapter and treated as a page of the public web site.
For example, if accessing /admin/users/create, the request will be handled by the "admin.php" adapter. If accessing /image/product/53/thumb.jpg the request will be handled by the "image.php" adapter. All requests for which the first segment of the URI does not match a specific adapter will be handled by the default "http.php" adapter and treated as a page of the public web site.
Views Directory Structure
Many of the requests will output a view to the web browser (administration panel, public web site, etc.), and the view displayed also depends directly on the URI being accessed. All views are stored within the /views/tpl/ directory, and Apex will simply display the .tpl file relative to the URI. For example, if accessing the /admin/users/create URI, the view at /views/tpl/admin/users/create.tpl will be displayed.
The one exception to this is the public web site, where the view displayed is the URI relative to the /views/tpl/public/ directory. For example, if accessing the /services URI, the view located at /views/tpl/public/services.tpl will be displayed. If no .tpl file exists at the correct location, the correct 404.tpl view will be displayed.
Views PHP Code
On top of the .tpl files, all views also have an associated .php file located within the /views/php/ directory at the same relative location. Any PHP code placed in these files is automatically executed when the view is displayed, and is specific to that view. Please recall the Apex Request Variables within the app class, as they will be used often within these PHP files.
Create Http / Middleware Adapter
At times you will need to create your own HTTP adapter, allowing all incoing requests to be handled in a certain way. For example, you may want all requests to the /invoice/ URI to be routed through a different adapter. To do this you would createa new adapter, for example if the package you're developing is "mycart" in terminal you would type:
This will create a new PHP file at /src/core/service/http_requests/invoice.php, and will be packaged with the "mycart" package. Simply
modify this file as desired, and add a
With the above example, if you visit the /invoice/85542TH34 URI within your web browser, it will output "Invoice ID: 85542TH34".
Through the /apexscript), you can develop and execute custom CLI commands by creating a "cli" component for each command. For example, if developing a package named "mycard", and you want a custom CLI command named "archive_orders", you could create a cli component within terminal with:
This will create a new PHP file at /src/mycard/cli/archive_orders.php, and simply add a
The above will execute the
Need a Professional?
Need development work by the creator of Apex? E-mail [email protected] for a free consultation.
Subscribe to the low traffic mailing list to stay updated on Apex.