</> Code The Pixel

CakePHP 4 Common Helpers

Asyraf Wahi Anuar - August 18, 2020

Estimated reading time: 4 minutes, 43 seconds

Here are some common helpers used in the 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', 'escapeTitle' => 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'
]); ?>

Hours ago:

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



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); //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
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

bin/cake cache clear_all


[will be updated...]

 


Cite this article (APA 6th Edition)

Popular
CakePHP 4 Print PDF Using CakePDF
May, 17 2020
CakePHP 4 Authentication Using Auth...
May, 14 2020
CakePHP 4 Sending Email
February, 01 2022
CakePHP 4 jQuery Date Time Picker
October, 01 2018
CakePHP 4 Export To CSV
May, 29 2020
CakePHP 4 Authentication Using...
May, 11 2020
CakePHP 4 Find, Sort & Count
June, 02 2020
Share
Sharing Link
Click the icon to share