Skip to content
CODE THE PIXEL
CakePHP

CakePHP 4 Common Helpers

Asyraf Wahi Anuar Updated 11 min read

CakePHP 4 helper and examples.

This tutorial may be outdated

It was last reviewed 3 years ago. Some steps may differ in newer versions.

Tech stack

  • CakePHP 4.x

Here are some common helpers used in the CakePHP project.

Form Helper

Form Creation:

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

Form with file upload:

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

Input:

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

Input label change:

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

Input remove label:

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

Input placeholder:

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

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

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

Input options - custom data:

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

Input checkbox:

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

Input year with minimum and maximum (current) year:

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

or

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

Input email:

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

Input hide:

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

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

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

Input time picker

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

or

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

Input date picker:

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

Input radio button:

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

Input file select:

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

Label only:

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

HTML Helper

Include CSS:

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

Include script:

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

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

PHP
<?= $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:

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

Insert image:

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

Link:

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

Link [External]:

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

Button [Bootstrap]:

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

Button [Bootstrap & Font Awesome icon]

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

Video player:

PHP
<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
<?php echo $this->Number->toReadableSize(1321205.76); ?>

Formatting floating-point number:

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

Formatting percentage:

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

Formatting Currency/ Before and after value:

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

Hours ago:

PHP
<?php
	use Cake\I18n\Time;
	$time = new Time($user->last_login);
	$result = $time->timeAgoInWords([
		'accuracy' => 'hour']);
	echo $result;
?>

Text Helper

Text Truncation

PHP
<?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
<?php
use Cake\Routing\Router; //load at the beginning of file
?>

<?php
    echo Router::url(null, true); //output: http://localhost/folder/articles/view/11
    echo Router::url(null, false); //output: /folder/articles/view/11
    echo $this->request->getUri(); //output: http://localhost/articles/view/11
    echo $this->request->getUri()->getPath(); //output: /articles/view/11
    echo $this->request->getRequestTarget(); //output: /articles/view/11
    echo Router::url("/", true); //output: http://localhost/folder/
    echo Router::url("/", false); //output: /folder/
?>

Get parameter from URL using query string parameters request

PHP
<?php
use Cake\Routing\Router;
?>
<?php 
//if URL: codethepixel.com/documents?page=2&limit=10
//to get the limit value:
$limit = $this->request->getQuery('limit');
?>

CLEAR CACHE

Terminal
bin/cake cache clear_all

[will be updated...]

Did this tutorial help?

200 readers found this helpful

Comments

No comments yet. Questions about this tutorial are welcome here: the answer helps the next reader too.

Leave a comment

Markdown works: `code`, ``` blocks, **bold**, links.

First comments are reviewed before they appear.

CakePHP

Using Bahasa Melayu Table Names in CakePHP

Learn how to use Bahasa Melayu table names like "pengguna" in CakePHP while following the framework’s naming conventions. Includes tips for using the Bake tool and avoiding pluralization issues.

5 min read

Tools & Software

Formatting PHP in VSCode Using Prettier and PHP Intelephense

Learn how to set up Visual Studio Code (VSCode) for automatic PHP file formatting using the Prettier and PHP Intelephense extensions. This step-by-step tutorial will help you maintain a clean and organised codebase, enhancing your PHP development experience.

2 min read