Link Search Menu Expand Document

Controller

Table of contents

  1. View flavoured Controller
  2. Service flavoured Controller
  3. Console flavoured Controller
  4. WebSocket flavoured Controller
  5. Controller Namespaces
  6. Controller Doc Tag

All controller in Puko Framework mostly auto-generated by pukoconsole following HMVC (Hierarchical Model View Controller) patterns. So, we can directly focus on logic part of our controller. This section of document will describe deeper what is ‘controller’.

Theoretically web application life-cycles is divided into Request and Response. Also, all request by default stored by PHP in $_POST or php://input and responds back with echo. So controller in puko is doing this request and returning responds from native PHP into another experience in very easy way.

For responds that needs web page as output, your controller should extends the View. Otherwise maybe you at the moment only building a backend REST API or GraphQL endpoints that responds with data with JSON format, your controller should extends the Service. But, if you developing another feature without web server and executing PHP scripts directly from operating system terminal, your controller should extends the Console.

Lets digging that one-by-one:

View flavoured Controller

View is the first original feature of puko framework. Its utilize and extends PTE extensions as HTML render engine. Because puko adopting HMVC pattern, you always have a HTML file with same directory structure as the controller and wired automatically in the return statement in the end of controller function. This is why pukoconsole is always bundling generated html file each view type of routes spawned.

Try it:

php puko routes view add "flavoured/views"

Service flavoured Controller

If main use case is API or backend applications, you can use service controller in puko framework. By default, it will convert returned data at the end of function into JSON representational response data. Also, you need ext-json enabled in PHP modules to utilize this functionality.

php puko routes service add "flavoured/apiservices"

`

Console flavoured Controller

Oftentimes we need a PHP script to doing something in background. you can use console controller in puko fraamework. Because it can execute commands separately from web server. So you don’t need to worry about server limitation parameter like timeouts.

php puko routes service add "flavoured/consoleservices"

`

WebSocket flavoured Controller

Coming soon in puko framework version 2.0

You can also manually create the controller :)

Controller Namespaces

  • Controller namespace in Puko framework is PSR-4 compatible.
namespace controller;
namespace controller\inventory;
  • Controller in Puko must extends one of the following: View / Service / Console.
class member extends View {
class member extends Service {
class member extends Console {
  • Exposing response in controller is done by return keyword from function.
public function member() {
    $data['Name'] = 'Didit Velliz';
    $data['Address'] = 'Bandung';

    return $data;
}

Controller Doc Tag

  • Controller in Puko have additional feature named docs tag that works like Annotations in other programming language.
/**
 * #Value Hobby Swimming
 * #Auth bearer true
 */
public function member() {
    $data['Name'] = 'Didit Velliz';
    $data['Address'] = 'Bandung';

    return $data;
}

Notes: you can see docs tag further in Puko Docs Command sections.