Application Foundation
The initial fully functional controller logic and working UI using that starts your new great application - including sessions, creating, editing and deleting database rows
Controller INDEX action (Staff access)
public function index()
{
$session = $this->request->getSession();
// Namespaced session key (important for reuse)
$sessionKey = 'CodeBlocks.index.filters';
// Reset filters
if ($this->request->getQuery('reset')) {
$session->delete($sessionKey);
return $this->redirect(['action' => 'index']);
}
// Create a filter entity (DTO-style)
$filters = $this->CodeBlocks->newEmptyEntity();
// Read incoming data (POST preferred, fallback to session)
if ($this->request->is(['post', 'put', 'patch'])) {
$data = $this->request->getData();
$session->write($sessionKey, $data);
} else {
$data = $session->read($sessionKey) ?? [];
}
// Apply filters to entity
if (!empty($data)) {
$filters = $this->CodeBlocks->patchEntity($filters, $data);
}
// Build query
$query = $this->CodeBlocks->find()
->contain('CodeBlockTypes')
->orderDesc('CodeBlocks.created')
->limit(20);
// Example filter usage (expand later)
if (!empty($filters->name)) {
$query->where([
'CodeBlocks.name LIKE' => '%' . $filters->name . '%'
]);
}
// Pass to view
$this->set([
'codeBlocks' => $filters,
'rows' => $query->all(),
]);
Controller CREATE action (Staff access)
public function create()
{
$entity = $this->CodeBlocks->newEmptyEntity();
if ($this->request->is('post')) {
$entity = $this->CodeBlocks->patchEntity(
$entity,
$this->request->getData()
);
if ($this->CodeBlocks->save($entity)) {
$this->Flash->success(__('The record has been created successfully.'));
return $this->redirect(['action' => 'index']);
}
$this->Flash->error(__('The record could not be created. Please try again.'));
} else {
$cameFrom = $this->request->referer(true);
}
$this->set([
'entity' => $entity,
'cameFrom' => $cameFrom,
'isCreate' => true,
'pageTitle' => 'Create Code Block',
'pageSubTitle' => 'Define the basic details',
]);
// Reuse Manager template
$this->viewBuilder()->setTemplate('/Manager/CodeBlocks/edit');
Controller VIEW action (Staff access)
public function view($id)
{
try {
$entity = $this->CodeBlocks->get($id, [
// 'contain' => ['RelatedTable'], // future-ready
]);
} catch (\Cake\Datasource\Exception\RecordNotFoundException $e) {
$this->Flash->error(__('Record not found.'));
return $this->redirect(['action' => 'index']);
}
$this->set([
'entity' => $entity,
'pageTitle' => 'View Code Block',
'pageSubTitle' => 'Read-only details',
]);
Controller DUPLICATE action (Staff access)
public function duplicate($id) {
if (!$id) {
$this->Flash->error('Specify ID');
return $this->redirect($this->referer());
} else {
// Fetch the existing entity
$entity = $this->CodeBlocks->get($id);
// Convert the entity to an array, reset the id and other fields that need to be unique
$data = $entity->toArray();
unset($data['id']); // Remove the ID to let CakePHP know it's a new entity
// Create a new entity instance with the duplicated data
$newEntity = $this->CodeBlocks->newEntity($data);
if ($this->CodeBlocks->save($newEntity)) {
$this->Flash->success('Duplicated ' . $newEntity->name);
return $this->redirect(['action' => 'edit', $newEntity->id]);
} else {
$this->Flash->error('Error, cannot duplicate');
return $this->redirect($this->referer());
}
}
Controller EDIT action (Manager access)
public function edit($id)
{
$entity = $this->CodeBlocks->get($id);
if ($this->request->is(['post', 'put', 'patch'])) {
$data = $this->request->getData();
$cameFrom = $data['cameFrom'] ?? null;
unset($data['cameFrom']); // critical
$entity = $this->CodeBlocks->patchEntity($entity, $data);
if ($this->CodeBlocks->save($entity)) {
//$this->Flash->success(__('The record has been updated successfully.'));
$this->Flash->success('Saved successfully.');
return $this->redirect(
$this->safeRedirect($cameFrom, ['action' => 'index'])
);
}
$this->Flash->error(__('The record could not be updated. Please try again.'));
} else {
$cameFrom = $this->request->referer(true);
}
$this->set([
'entity' => $entity,
'cameFrom' => $cameFrom,
'isCreate' => false,
'pageTitle' => 'Edit Code Block',
'pageSubTitle' => 'Update details and configuration',
]);
Controller DELETE action (Manager access)
public function delete($id = null)
{
$entity = $this->CodeBlocks->get($id);
if ($this->CodeBlocks->delete($entity)) {
$this->Flash->warning(__('The record has been deleted.'));
} else {
$this->Flash->error(__('The record could not be deleted. Please try again.'));
}
return $this->redirect(['prefix' => 'Staff', 'action' => 'index']);
Visual INDEX template (Staff access)
<div class="card shadow-sm">
<!-- Card Header -->
<div class="card-header">
<!-- Title + Actions -->
<div class="d-flex align-items-start gap-3">
<div>
<h5 class="mb-1">Title</h5>
<p class="mb-0 text-muted small">Sub-title</p>
</div>
<div class="ms-auto d-flex gap-2 flex-wrap">
<?= $this->Html->link('Create', ['action' => 'create'], ['class' => 'btn btn-sm btn-primary']) ?>
<button
type="button"
class="btn btn-sm btn-outline-secondary"
data-bs-toggle="modal"
data-bs-target="#exampleModalLive">
Popup
</button>
</div>
</div>
<!-- Filters -->
<div class="row g-2 mt-3 pt-3 border-top">
<div class="col-12 col-lg-3">
<?= $this->Form->control('dropdown', [
'options' => ['1' => '1', '2' => '2'],
'empty' => 'Choose …',
'label' => false,
'class' => 'form-select form-select-sm'
]) ?>
</div>
<div class="col-12 col-lg-3">
<?= $this->Form->control('dropdown2', [
'options' => ['1' => '1', '2' => '2'],
'empty' => 'Choose …',
'label' => false,
'class' => 'form-select form-select-sm'
]) ?>
</div>
<div class="col-12 col-lg-3">
<?= $this->Form->control('dropdown3', [
'options' => ['1' => '1', '2' => '2'],
'empty' => 'Choose …',
'label' => false,
'class' => 'form-select form-select-sm'
]) ?>
</div>
<div class="col-12 col-lg-3">
<?= $this->Form->control('dropdown4', [
'options' => ['1' => '1', '2' => '2'],
'empty' => 'Choose …',
'label' => false,
'class' => 'form-select form-select-sm'
]) ?>
</div>
</div>
</div>
<!-- Card Body -->
<div class="card-body p-0">
<table class="table table-hover mb-0">
<thead class="table-light">
<tr>
<th style="width: 1%;">Actions</th>
<th>Name</th>
<th>Description</th>
</tr>
</thead>
<tbody>
<?php foreach ($rows as $each): ?>
<tr>
<td class="text-nowrap">
<?= $this->Html->link('View', ['action' => 'view', $each['id']], ['class' => 'btn btn-sm btn-primary']) ?>
<?= $this->Html->link('Edit', ['prefix' => 'Manager', 'action' => 'edit', $each['id']], ['class' => 'btn btn-sm btn-warning']) ?>
<?= $this->Form->postLink(
'X',
['prefix' => 'Manager', 'action' => 'delete', $each['id']],
['class' => 'btn btn-sm btn-danger', 'confirm' => 'Are you sure?']
) ?>
</td>
<td><?= h($each['name']) ?></td>
<td><?= h($each['description']) ?></td>
</tr>
<?php endforeach; ?>
</tbody>
</table>
</div>
</div>
<div class="modal fade" id="exampleModalLive" tabindex="-1" aria-hidden="true">
<div class="modal-dialog">
<div class="modal-content">
<div class="modal-header">
<h5 class="modal-title">Help</h5>
<button type="button" class="btn-close" data-bs-dismiss="modal"></button>
</div>
<div class="modal-body">
Popup
</div>
<div class="modal-footer">
<button type="button" class="btn btn-secondary" data-bs-dismiss="modal">
Close
</button>
</div>
</div>
</div>
</div>
Visual CREATE template (Staff access)
<div class="card shadow-sm">
<!-- Card Header -->
<div class="card-header">
<!-- Title + Actions -->
<div class="d-flex align-items-start gap-3">
<div>
<h5 class="mb-1">Title</h5>
<p class="mb-0 text-muted small">Sub-title</p>
</div>
<div class="ms-auto d-flex gap-2 flex-wrap">
<?= $this->Html->link('Create', ['action' => 'create'], ['class' => 'btn btn-sm btn-primary']) ?>
<button
type="button"
class="btn btn-sm btn-outline-secondary"
data-bs-toggle="modal"
data-bs-target="#exampleModalLive">
Popup
</button>
</div>
</div>
<!-- Filters -->
<div class="row g-2 mt-3 pt-3 border-top">
<div class="col-12 col-lg-3">
<?= $this->Form->control('dropdown', [
'options' => ['1' => '1', '2' => '2'],
'empty' => 'Choose …',
'label' => false,
'class' => 'form-select form-select-sm'
]) ?>
</div>
<div class="col-12 col-lg-3">
<?= $this->Form->control('dropdown2', [
'options' => ['1' => '1', '2' => '2'],
'empty' => 'Choose …',
'label' => false,
'class' => 'form-select form-select-sm'
]) ?>
</div>
<div class="col-12 col-lg-3">
<?= $this->Form->control('dropdown3', [
'options' => ['1' => '1', '2' => '2'],
'empty' => 'Choose …',
'label' => false,
'class' => 'form-select form-select-sm'
]) ?>
</div>
<div class="col-12 col-lg-3">
<?= $this->Form->control('dropdown4', [
'options' => ['1' => '1', '2' => '2'],
'empty' => 'Choose …',
'label' => false,
'class' => 'form-select form-select-sm'
]) ?>
</div>
</div>
</div>
<!-- Card Body -->
<div class="card-body p-0">
<table class="table table-hover mb-0">
<thead class="table-light">
<tr>
<th style="width: 1%;">Actions</th>
<th>Name</th>
<th>Description</th>
</tr>
</thead>
<tbody>
<?php foreach ($rows as $each): ?>
<tr>
<td class="text-nowrap">
<?= $this->Html->link('View', ['action' => 'view', $each['id']], ['class' => 'btn btn-sm btn-primary']) ?>
<?= $this->Html->link('Edit', ['prefix' => 'Manager', 'action' => 'edit', $each['id']], ['class' => 'btn btn-sm btn-warning']) ?>
<?= $this->Form->postLink(
'X',
['prefix' => 'Manager', 'action' => 'delete', $each['id']],
['class' => 'btn btn-sm btn-danger', 'confirm' => 'Are you sure?']
) ?>
</td>
<td><?= h($each['name']) ?></td>
<td><?= h($each['description']) ?></td>
</tr>
<?php endforeach; ?>
</tbody>
</table>
</div>
</div>
<div class="modal fade" id="exampleModalLive" tabindex="-1" aria-hidden="true">
<div class="modal-dialog">
<div class="modal-content">
<div class="modal-header">
<h5 class="modal-title">Help</h5>
<button type="button" class="btn-close" data-bs-dismiss="modal"></button>
</div>
<div class="modal-body">
Popup
</div>
<div class="modal-footer">
<button type="button" class="btn btn-secondary" data-bs-dismiss="modal">
Close
</button>
</div>
</div>
</div>
</div>
Visual VIEW template (Staff access)
<div class="card shadow-sm mb-4">
<!-- Header -->
<div class="card-header">
<div class="d-flex align-items-start gap-3">
<!-- Back icon + title -->
<div class="d-flex align-items-center gap-2">
<?= $this->Html->link(
'←',
['action' => 'index'],
[
'class' => 'btn btn-sm btn-outline-secondary',
'escape' => false,
'title' => 'Back to list',
]
) ?>
<div>
<h5 class="mb-1">
<?= h($entity->name) ?>
</h5>
<p class="mb-0 text-muted small">
Record #<?= $entity->id ?>
</p>
</div>
</div>
<!-- Actions -->
<div class="ms-auto d-flex gap-2 flex-wrap">
<?= $this->Html->link(
'Edit',
['prefix' => 'Manager', 'action' => 'edit', $entity->id],
['class' => 'btn btn-sm btn-primary']
) ?>
<?= $this->Form->postLink(
'Delete',
['prefix' => 'Manager', 'action' => 'delete', $entity->id],
[
'class' => 'btn btn-sm btn-danger',
'confirm' => 'Are you sure you want to delete this record?'
]
) ?>
<button
type="button"
class="btn btn-sm btn-outline-info"
data-bs-toggle="modal"
data-bs-target="#exampleModalLive">
Help
</button>
</div>
</div>
</div>
</div>
<div class="row g-4">
<!-- Left column -->
<div class="col-12 col-lg-6">
<div class="card">
<div class="card-header">
<h6 class="mb-0">Details</h6>
</div>
<div class="card-body">
<?= $this->Text->autoParagraph(h($entity->description)) ?>
</div>
</div>
</div>
<!-- Right column -->
<div class="col-12 col-lg-6">
<div class="card">
<div class="card-header">
<h6 class="mb-0">Associated Data</h6>
</div>
<div class="card-body p-0">
<table class="table table-hover mb-0">
<thead class="table-light">
<tr>
<th>Column 1</th>
<th>Column 2</th>
<th>Column 3</th>
</tr>
</thead>
<tbody>
<?php foreach (range(1, 4) as $each): ?>
<tr>
<td class="text-nowrap">
Row #<?= $each ?>
<?= $this->Html->link(
'Edit',
['action' => 'edit', $each],
['class' => 'btn btn-sm btn-outline-primary ms-2']
) ?>
<?= $this->Form->postLink(
'X',
['action' => 'delete', $each],
[
'class' => 'btn btn-sm btn-outline-danger ms-1',
'confirm' => 'Really delete?'
]
) ?>
</td>
<td>Info</td>
<td>Goes here…</td>
</tr>
<?php endforeach; ?>
</tbody>
</table>
</div>
</div>
</div>
</div>
<!-- Help Modal -->
<div class="modal fade" id="exampleModalLive" tabindex="-1" aria-hidden="true">
<div class="modal-dialog">
<div class="modal-content">
<div class="modal-header">
<h5 class="modal-title">Help</h5>
<button type="button" class="btn-close" data-bs-dismiss="modal"></button>
</div>
<div class="modal-body">
Popup
</div>
<div class="modal-footer">
<button type="button" class="btn btn-secondary" data-bs-dismiss="modal">
Close
</button>
</div>
</div>
</div>
</div>
Visual EDIT template (Manager access)
<?= $this->Form->create($entity, [
'class' => 'needs-validation',
'novalidate' => true
]) ?>
<?= $this->Form->hidden('cameFrom', ['value' => $cameFrom]) ?>
<div class="card shadow-sm mb-4">
<!-- Header -->
<div class="card-header">
<div class="d-flex align-items-start gap-3">
<!-- Back icon + title -->
<div class="d-flex align-items-center gap-2">
<?= $this->Html->link(
'←',
['prefix' => 'Staff', 'action' => 'index'],
[
'class' => 'btn btn-sm btn-outline-secondary',
'title' => 'Back to list',
]
) ?>
<div>
<h5 class="mb-1"><?= h($pageTitle) ?></h5>
<p class="mb-0 text-muted small"><?= h($pageSubTitle) ?></p>
</div>
</div>
<!-- Actions -->
<div class="ms-auto d-flex gap-2 flex-wrap">
<?php if (!$isCreate): ?>
<?= $this->Html->link(
'View',
['prefix' => 'Staff', 'action' => 'view', $entity->id],
['class' => 'btn btn-sm btn-primary']
) ?>
<?php endif; ?>
<button
type="button"
class="btn btn-sm btn-outline-info"
data-bs-toggle="modal"
data-bs-target="#exampleModalLive">
Help
</button>
</div>
</div>
</div>
</div>
<div class="row g-4">
<!-- Left column -->
<div class="col-12 col-lg-6">
<div class="card">
<div class="card-header">
<h6 class="mb-0">Basic Information</h6>
</div>
<div class="card-body">
<div class="mb-3 row">
<?= $this->Form->label('name', 'Name', ['class' => 'col-sm-3 col-form-label']) ?>
<div class="col-sm-9">
<?= $this->Form->control('name', [
'label' => false,
'class' => 'form-control',
]) ?>
</div>
</div>
<fieldset class="mb-3">
<legend class="col-form-label col-sm-3 pt-0">Radios</legend>
<div class="ms-sm-3">
<div class="form-check">
<?= $this->Form->radio('radio', ['option1' => 'First radio'], [
'hiddenField' => false,
'class' => 'form-check-input'
]) ?>
</div>
<div class="form-check">
<?= $this->Form->radio('radio', ['option2' => 'Second radio'], [
'hiddenField' => false,
'class' => 'form-check-input'
]) ?>
</div>
</div>
</fieldset>
<div class="mb-3">
<?= $this->Form->checkbox('checkbox', ['hiddenField' => false]) ?>
<?= $this->Form->label('checkbox', 'Example checkbox') ?>
</div>
<div class="mb-3">
<?= $this->Form->checkbox('checkbox2', ['hiddenField' => false]) ?>
<?= $this->Form->label('checkbox2', 'Example checkbox 2') ?>
</div>
<?= $this->Form->button(
$isCreate ? 'Create' : 'Save Changes',
['class' => 'btn btn-primary w-100']
) ?>
</div>
</div>
</div>
<!-- Right column -->
<div class="col-12 col-lg-6">
<div class="card">
<div class="card-header">
<h6 class="mb-0">Description</h6>
</div>
<div class="card-body">
<?= $this->Form->control('description', [
'type' => 'textarea',
'class' => 'form-control tinyMce',
'rows' => 8,
'label' => false,
]) ?>
</div>
</div>
</div>
</div>
<?= $this->Form->end() ?>
<!-- Help Modal -->
<div class="modal fade" id="exampleModalLive" tabindex="-1" aria-hidden="true">
<div class="modal-dialog">
<div class="modal-content">
<div class="modal-header">
<h5 class="modal-title">Help</h5>
<button type="button" class="btn-close" data-bs-dismiss="modal"></button>
</div>
<div class="modal-body">
Popup
</div>
<div class="modal-footer">
<button type="button" class="btn btn-secondary" data-bs-dismiss="modal">
Close
</button>
</div>
</div>
</div>
</div>
Visual DOWNLOAD CSV template (Staff access)