Inside this article we will see CakePHP 4 How to create custom helper. This tutorial contains all basics about creating a custom helper in cakephp application.
Helpers are those functions and methods which helps to complete a specific task of application. In CakePHP there are many default helpers available as Html, URL, Form, etc. To create a user defined helper in cakephp we need to follow some basic steps.
We need custom helper when we want to create some functions which we can re-use through out in the application.
Learn More –
- CakePHP 4 How To Set Dynamic Page Title Tutorial
- CakePHP 4 How To Use Application Environment Variables
- CakePHP 4 How To Use Named Route Tutorial
- CakePHP 4 How To Use Prefix Routing Tutorial
Let’s get started.
CakePHP 4 Installation
To create a CakePHP project, run this command into your shell or terminal. Make sure composer should be installed in your system.
$ composer create-project --prefer-dist cakephp/app:~4.0 mycakephp
Above command will creates a project with the name called mycakephp.
Custom Helper Basic Steps
In CakePHP helpers are provided by class files. There are few basic steps we need to follow to create custom helper:
- Helper class files should be put inside /src/View/Helper folder.
- Helper class files should be suffixed with Helper keyword. For Example DataHelper.php.
- To load helper we use like this $this->loadHelper(‘Data’) and load into AppView.php from /src/View.
Create Custom Helper
We will create a helper which works with string values. Create a file StringHelper.php file inside /src/View/Helper.
<?php namespace App\View\Helper; use Cake\View\Helper; class StringHelper extends Helper { public function upperCase($string) { return strtoupper($string); } public function mailExample($email) { return str_replace("gmail.com", "example.net", $email); } }
Here, inside this custom helper we have to methods.
- upperCase() which converts string from any case to uppercase.
- mailExample() method Replace gmail.com to example.net of any email address.
Load Helper
By default CakePHP load it’s defined helpers automatically. To load custom helpers we will use AppView.php from /src/View folder.
Open AppView.php and add this line to load helper.
<?php declare(strict_types=1); namespace App\View; use Cake\View\View; class AppView extends View { public function initialize(): void { $this->loadHelper("String"); // Loads StringHelper.php } }
Now, we will able to use String helper into template files.
Create Controller & Template
Open project into terminal and run this bake console command.
$ bin/cake bake controller Site --no-actions
It will create SiteController.php file inside /src/Controller folder. Open file and write this code into it.
<?php declare(strict_types=1); namespace App\Controller; class SiteController extends AppController { public function test() { // } }
Create a folder called Site inside /templates folder. Create a file with name test.php into /templates/Site folder.
We can use helper and it’s methods into template files.
Custom Helper Usage
Open test.php and try these codes into it.
Use: upperCase()
<?php
$string = "Sample string";
echo $this->String->upperCase($string);
//Output: SAMPLE STRING
Use: mailExample()
<?php
$email = "sanjaykumar@gmail.com";
echo $this->String->mailExample($email);
//Output: sanjaykumar@example.net
We hope this article helped you to learn about CakePHP 4 How To Create Custom Helper Tutorial in a very detailed way.
Online Web Tutor invites you to try Skillshike! Learn CakePHP, Laravel, CodeIgniter, Node Js, MySQL, Authentication, RESTful Web Services, etc into a depth level. Master the Coding Skills to Become an Expert in PHP Web Development. So, Search your favourite course and enroll now.
If you liked this article, then please subscribe to our YouTube Channel for PHP & it’s framework, WordPress, Node Js video tutorials. You can also find us on Twitter and Facebook.