Inside this article we will see CakePHP 4 How To Create Custom Component. This tutorial contains all basics about creating a custom component in cakephp application.
Components are those classes in which functions and methods are for to complete a specific task of application. In CakePHP there are many default components available as Flash, Auth, etc. To create a user defined component in cakephp we need to follow some basic steps.
We need custom component when we want to create some functions which we can re-use through out in the application. User defined Component classes are stored inside /src/Controller/Component folder.
Learn More –
- CakePHP 4 Connect Custom Helper To Database Table
- CakePHP 4 Connect Models with Multiple Databases
- CakePHP 4 CRUD Tutorial with MySQL
- CakePHP 4 CRUD Using Ajax Requests 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.
Create Custom Component
Open project into terminal and run this cake console command into it.
$ bin/cake bake component Custom
It will create CustomComponent.php file inside /src/Controller/Component folder. Open up this file and write this following code into it.
<?php declare(strict_types=1); namespace App\Controller\Component; use Cake\Controller\Component; use Cake\Controller\ComponentRegistry; /** * Custom component */ class CustomComponent extends Component { /** * Default configuration. * * @var array */ protected $_defaultConfig = []; // To add all array elements public function addArrayElements($numbers = array()) { return array_sum($numbers); } // To reverse array elements public function reverseArrayElements($numbers = array()) { return array_reverse($numbers); } }
Now, we can use the methods of this custom component into application. Methods are – addArrayElements() and reverseArrayElements().
To use component first we need to load it. We have two options to load into cakephp application.
- Load by using AppController.php (Global load to use anywhere inside application).
- Load into Specific contoller to use specifically.
Load Custom Component
To load custom component we use loadComponent() method. Inside this method needs to pass component name.
Open AppController.php file from /src/Controller folder. We have loaded it inside initialize() method.
<?php declare(strict_types=1); namespace App\Controller; use Cake\Controller\Controller; class AppController extends Controller { public function initialize(): void { parent::initialize(); $this->loadComponent('RequestHandler'); $this->loadComponent('Flash'); $this->loadComponent("Custom"); // Loading Custom Component } }
Create Controller & Execute Application
Back to terminal and run this command.
$ bin/cake bake controller Site --no-actions
It will create SiteController.php inside /src/Controller folder. Open up this file and write this code into it.
<?php declare(strict_types=1); namespace App\Controller; class SiteController extends AppController { public function initialize(): void { parent::initialize(); $this->autoRender = false; } public function sumNumbers() { // Use method of Custom component echo $this->Custom->addArrayElements([2, 5, 7, 8, 9]); } public function reverseNumbers() { // Use method of Custom component $reversedArray = $this->Custom->reverseArrayElements([805, 125, 461, 111, 321]); print_r($reversedArray); } }
When we execute the above code,
Example #1
echo $this->Custom->addArrayElements([2, 5, 7, 8, 9]);
Returns // 31
Example #2
$reversedArray = $this->Custom->reverseArrayElements([805, 125, 461, 111, 321]);
Returns
// Array ( [0] => 321 [1] => 111 [2] => 461 [3] => 125 [4] => 805 )
We hope this article helped you to learn about CakePHP 4 How To Create Custom Component Tutorial in a very detailed way.
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.