CakePHP 4 Common Helpers

CakePHP 4 Common Helpers

Here some common helpers used in CakePHP project.

Form Helper

Form Creation:

<?= $this->Form->create($documents); ?>


Form with file upload:

<?= $this->Form->create($documents, ['type' => 'file']); ?>


Input:

<?= $this->Form->control('name', ['class' => 'form-control','required' => false]); ?>


Input label change:

<?= $this->Form->control('dob', ['label' => 'Date of Birth','class' => 'form-control','required' => false]); ?>


Input remove label:

<?= $this->Form->control('dob', ['label' => false,'class' => 'form-control','required' => false]); ?>


Input placeholder:

<?= $this->Form->control('dob', ['placeholder' => 'Date of Birth','class' => 'form-control','required' => false]); ?>


Input options [eg: user_id] - data from database relationship:

<?= $this->Form->control('user_id', ['options' => $users]); ?>


Input options - custom data:

<?php echo $this->Form->control('size', [
	'options' => ['s' => 'Small', 'm' => 'Medium', 'l' => 'Large'],
	'empty' => 'Select size',
	'class' => 'form-control',
	'required' => false
]);?>


Input checkbox:

<?= $this->Form->control('published', ['type' => 'checkbox']); ?>


Input year with minimum and maximum (current) year:

<?= $this->Form->control('year', [
	'min' => 1985, 
	'max' => date('Y'), 
	'class' => 'form-control',
	'required' => false
]); ?>

or

<?= $this->Form->control('year', [
    'label' => 'Date of birth',
    'min' => date('Y') - 70,
    'max' => date('Y') - 18,
	'class' => 'form-control',
	'required' => false
]); ?>


Input email:

<?= $this->Form->control('email', ['type' => 'email', 'class' => 'form-control','required' => false]); ?>


Input hide:

<?= $this->Form->hidden('title', ['class' => 'form-control','required' => false]); ?>


Input Textarea (usually if DB is configured as text, it will automatically generate textbox:

<?= $this->Form->textarea('notes', ['escape' => false, 'class' => 'form-control','required' => false]); ?>


Input time picker

<?= $this->Form->control('operation_time', [
	'type' => 'time',
    'value' => '12:30:00',
	'class' => 'form-control',
	'required' => false,
	'label' => 'Operation Time'
]); ?>

or

<?= $this->Form->time('operation_time', [
    'value' => '12:30:00',
	'class' => 'form-control',
	'required' => false,
	'label' => 'Operation Time'
]); ?>


Input date picker:

<?= $this->Form->control('dob', [
	'type' => 'date',	
    'value' => date('dd/mm/yyyy'),
	'class' => 'form-control',
	'required' => false,
	'label' => 'Date of Birth'
]); ?>


Input radio button:

<?= $this->Form->radio('gender', ['Male', 'Female']); ?>


Input file select:

<?= $this->Form->control('attachment', ['type' => 'file', 'class' => 'form-control','required' => false]); ?>


Label only:

<?= $this->Form->label('name'); ?>


HTML Helper

Include CSS:

<?= $this->Html->css('bootstrap.css') ?>


Include script:

<?= $this->Html->script('bootstrap.bundle.min.js'); ?>


Include CSS/script to load at the end of the page:

<?= $this->Html->script('bootstrap.bundle.min.js', ['block' => 'scriptBottom']); ?>

//Others codes. 
//At the end of the page, load the following script

<?= $this->fetch('scriptBottom') ?>


Include element:

<?= $this->element('menu'); ?>


Insert image:

<?= $this->Html->image('logo.png', ['alt' => 'Logo', 'class' => '']); ?>


Link:

<?= $this->Html->link('Dashboard',
    ['controller' => 'Dashboards', 'action' => 'index', '_full' => true]
); ?>


Link [External]:

<?php echo $this->Html->link('XAMPP','https://www.apachefriends.org', ['target'=>'_blank']); ?>


Button [Bootstrap]:

<?= $this->Html->link('Dashboard',
    ['controller' => 'Dashboards', 'action' => 'index', '_full' => true],
	['class' => 'btn btn-primary']
); ?>


Button [Bootstrap & Font Awesome icon]

<?= $this->Html->link(__('<i class="fas fa-list"></i> Reset'), ['action' => 'add'], ['class' => 'btn btn-primary', 'escape' => false]) ?>


Video player:

<video controls disablePictureInPicture controlsList="nodownload" name="media" height="500px" width="100%">
<source src="/<?= ('../../files/movies/video/' . $vod->video_dir . '/' . $movie->video) ?>" type="audio/mpeg">
<track label="English" kind="subtitles" srclang="en" src="/<?= ('../../files/movies/sub/' . $vod->sub_dir . '/' . $movie->sub) ?>" default>
</video>


Number Helper

Number to readable format:

<?php echo $this->Number->toReadableSize(1321205.76); ?>


Formatting floating-point number:

<?= $this->Number->precision(456.91873645, 2); ?>


Formatting percentage:

<?= $this->Number->toPercentage(45.691873645); ?>


Formatting Currency/ Before and after value:

<?= $this->Number->format('123456.7897', [
    'places' => 2,
    'before' => 'RM ',
    'after' => ' Cent'
]); ?>


Text Helper

Text Truncation

<?php 
use Cake\Utility\Text; //load at the beginning of file
?>

<?= Text::truncate(
    $this->Text->autoParagraph($document->summary),
    30,
    [
        'ellipsis' => '...',
        'exact' => false
    ]
); ?>


URL

Request URL

<?php
use Cake\Routing\Router; //load at the beginning of file
?>

<?php
    echo Router::url(null, true);
    echo Router::url(null, false);
    echo $this->request->getUri();
    echo $this->request->getUri()->getPath();
    echo $this->request->getRequestTarget();
    echo Router::url("/", true);
    echo Router::url("/", false);
?>




[will be update...]