HelloWorldController
extends AppController
in package
HelloWorld Controller
This controller demonstrates a basic KMP plugin controller with:
- Standard CRUD operations
- Authorization integration
- View rendering with Bootstrap templates
- Flash messaging
- Proper error handling
The HelloWorld controller serves as a template for creating new controllers in KMP plugins. It shows the standard patterns and best practices.
Authorization
Access to controller actions is controlled by HelloWorldPolicy. The controller declares which actions require authorization checking:
- index: Requires view permission
- add: Requires create permission
- edit: Requires update permission
- delete: Requires delete permission
Usage Pattern
All controllers in KMP plugins should:
- Extend AppController for consistent behavior
- Use authorization for security
- Load necessary models in initialize()
- Follow CakePHP conventions
- Use Flash messages for user feedback
Table of Contents
Constants
- VIEW_DATA_EVENT = 'KMP.plugins.callForViewData'
- VIEW_PLUGIN_EVENT = 'KMP.plugins.callForViewCells'
Properties
- $HelloWorldItems : HelloWorldItemsTable
- $isCsvRequest : bool
- $pluginViewCells : array<string|int, mixed>
Methods
- add() : Response|null|void
- Add Action - Create New Item
- beforeFilter() : Response|null|void
- Pre-action filter for application-wide processing.
- delete() : Response|null|void
- Delete Action - Remove Item
- edit() : Response|null|void
- Edit Action - Update Existing Item
- index() : Response|null|void
- Index Action - List View
- initialize() : void
- Initialize Controller
- isCsvRequest() : bool
- Check if current request is for CSV export.
- switchView() : Response
- Switch between mobile and desktop view modes.
- view() : Response|null|void
- View Action - Detail View
- authorizeCurrentUrl() : void
- Authorize the current URL/action via Authorization component.
- organizeViewCells() : array<string|int, mixed>
- Organize view cells by type and display order.
Constants
VIEW_DATA_EVENT
public
string
VIEW_DATA_EVENT
= 'KMP.plugins.callForViewData'
Event for plugin view data enhancement
VIEW_PLUGIN_EVENT
public
string
VIEW_PLUGIN_EVENT
= 'KMP.plugins.callForViewCells'
Event for plugin view cell registration
Properties
$HelloWorldItems
public
HelloWorldItemsTable
$HelloWorldItems
$isCsvRequest
protected
bool
$isCsvRequest
= false
Whether current request is for CSV export (.csv extension)
$pluginViewCells
protected
array<string|int, mixed>
$pluginViewCells
= []
View cells from plugins for current request
Methods
add()
Add Action - Create New Item
public
add() : Response|null|void
Handles the creation of new hello world items.
Authorization
Requires 'add' permission via HelloWorldPolicy
Route
GET/POST /template/hello-world/add
Return values
Response|null|void —Redirects on successful add, renders view otherwise
beforeFilter()
Pre-action filter for application-wide processing.
public
beforeFilter(EventInterface $event) : Response|null|void
Handles: CSV detection, plugin validation, navigation history, view cell loading, and Turbo Frame detection.
Parameters
- $event : EventInterface
-
The beforeFilter event
Return values
Response|null|voiddelete()
Delete Action - Remove Item
public
delete([string|null $id = null ]) : Response|null|void
Handles deletion of hello world items.
Authorization
Requires 'delete' permission via HelloWorldPolicy
Route
POST /template/hello-world/delete/{id}
Parameters
- $id : string|null = null
-
The item ID
Tags
Return values
Response|null|void —Redirects to index
edit()
Edit Action - Update Existing Item
public
edit([string|null $id = null ]) : Response|null|void
Handles updating existing hello world items.
Authorization
Requires 'edit' permission via HelloWorldPolicy
Route
GET/POST /template/hello-world/edit/{id}
Parameters
- $id : string|null = null
-
The item ID
Tags
Return values
Response|null|void —Redirects on successful edit, renders view otherwise
index()
Index Action - List View
public
index() : Response|null|void
Displays a list of hello world items. In a real controller, this would paginate records from a database table. This template version shows static data as an example.
Authorization
Requires 'index' permission via HelloWorldPolicy
Route
GET /template/hello-world
Return values
Response|null|void —Renders the view
initialize()
Initialize Controller
public
initialize() : void
This method is called before any action is executed. Use it to:
- Load additional models
- Configure components
- Set up authorization rules
- Initialize helpers
isCsvRequest()
Check if current request is for CSV export.
public
isCsvRequest() : bool
Return values
boolswitchView()
Switch between mobile and desktop view modes.
public
switchView() : Response
Stores preference in session and redirects to appropriate interface. Mobile redirects to viewMobileCard, desktop to profile.
Return values
Response —Redirect response
view()
View Action - Detail View
public
view([string|null $id = null ]) : Response|null|void
Displays details for a single hello world item.
Authorization
Requires 'view' permission via HelloWorldPolicy
Route
GET /template/hello-world/view/{id}
Parameters
- $id : string|null = null
-
The item ID
Tags
Return values
Response|null|void —Renders the view
authorizeCurrentUrl()
Authorize the current URL/action via Authorization component.
protected
authorizeCurrentUrl() : void
Tags
organizeViewCells()
Organize view cells by type and display order.
protected
organizeViewCells(array<string|int, mixed> $viewCells) : array<string|int, mixed>
Unused - view cells organized in ViewCellRegistry
Parameters
- $viewCells : array<string|int, mixed>
-
Flat array of view cell configurations
Return values
array<string|int, mixed> —Organized array grouped by type and sorted by order