Controller
Table of contents
Most controllers in the Puko Framework are automatically generated by pukoconsole, following the HMVC (Hierarchical Model-View-Controller) architectural pattern. This allows developers to focus directly on business logic.
Theoretically, a web application’s lifecycle is divided into Request and Response. In Puko, requests are typically accessed via $_POST or php://input, and the framework handles the response back to the client seamlessly.
Flavoured Controllers
The Puko Framework provides several types of controllers to suit different needs.
View Flavoured Controller
The View is the original feature of the Puko Framework. It utilizes the PTE extension as its HTML rendering engine. Because Puko follows the HMVC pattern, each controller has a corresponding HTML file with the same directory structure, which is automatically wired into the return statement of the controller function.
Try it:
php puko routes view add "flavoured/views"
Service Flavoured Controller
If your primary use case is an API or backend application, you can use the Service controller. By default, it automatically converts returned data into a JSON response. Ensure that the ext-json PHP module is enabled to use this functionality.
php puko routes service add "flavoured/apiservices"
Console Flavoured Controller
Oftentimes, you need a PHP script to run in the background. The Console controller is designed for this purpose. Since it executes separately from the web server, you don’t need to worry about server-imposed limitations such as timeouts.
php puko routes console add "flavoured/consoleservices"
WebSocket Flavoured Controller
Coming soon in Puko Framework version 2.0.
Controller Fundamentals
Namespaces
- Controller namespaces in the Puko Framework are PSR-4 compatible.
namespace controller;
// or
namespace controller\inventory;
- All controllers in Puko must extend one of the following classes: View, Service, or Console.
class member extends View {}
class member extends Service {}
class member extends Console {}
Exposing Responses
Responses are exposed in a controller by using the return keyword from a function.
public function member() {
$data['Name'] = 'Didit Velliz';
$data['Address'] = 'Bandung';
return $data;
}
Controller Doc Tags
- Controllers in Puko feature Doc Tags, which function similarly to Annotations in other programming languages.
/**
* #Value Hobby Swimming
* #Auth bearer true
*/
public function member() {
$data['Name'] = 'Didit Velliz';
$data['Address'] = 'Bandung';
return $data;
}
Note: See the Docs Command section for a comprehensive guide to available tags.