# CakePHP 4 Common Helpers

> CakePHP 4 helper and examples.

- URL: https://www.codethepixel.com/cakephp-4-common-helpers
- Author: Asyraf Wahi Anuar (https://www.codethepixel.com/author/asyraf-wahi-anuar)
- Published: 2020-08-18
- Updated: 2023-01-23
- Category: CakePHP
- Tested with: CakePHP 4.x
- Tags: CakePHP, Helper
- Reading time: 11 min

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

```bash
$ bin/cake cache clear_all
```

\[will be updated...\]
