Inside this article we will see the concept i.e CakePHP 4 send parameter values to view templates. We have few rules by the help of which we can set view variables and values.
Article contains classified information about sending view values and to work with. CakePHP provides set() method where we can pass a single value or group of values to templates.
Learn More –
- CakePHP 4 Passing Parameters to Routes and Actions
- CakePHP 4 Query Builder CRUD Functions Tutorial
- CakePHP 4 Remove Column From Table Using Migration
- CakePHP 4 Rollback Migrations To Specific Migration Version
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.
CakePHP 4 Template Rule
When we create any controller and It’s methods. Then by default application understands that it have templates inside it’s associated folder.
If in case we don’t have any template files then we need to set autoRender to false.
Let’s assume we have a controller SiteController.php inside /src/Controller folder. Then according to default behaviour of CakePHP 4, we need to create Site folder inside /templates folder.
Why Site folder? It’s because Site is the name of controller class.
<?php declare(strict_types=1); namespace App\Controller; class SiteController extends AppController { public function aboutUs() { //... } public function myProducts() { //... } }
We create template files inside /templates/Site folder for –
- about_us.php for aboutUs() method
- my_products.php for myProducts() method
Passing Values To Templates
We can control dynamic values from controller and send them to template files. Here, we will see how to send –
- Single value to template
- More than one value to template
Single value to template
$this->set("name", "Sanjay Kumar");
More than one value to template
$this->set([
"email" => "sample@example.net",
"website" => "https://onlinewebtutorblog.com",
"phone_number" => "+91 1234567890"
]);
Let’s use and see in controller –
<?php declare(strict_types=1); namespace App\Controller; class SiteController extends AppController { public function aboutUs() { //... $this->set("name", "Sanjay Kumar"); } public function myProducts() { //... $this->set([ "email" => "sample@example.net", "website" => "https://onlinewebtutorblog.com", "phone_number" => "+91 1234567890" ]); } }
Template: about_us.php
<?php echo $name; ?>
OR
<?= $name ?>
Template: my_products.php
<?php echo $email; ?> OR <?= $email ?>
<?php echo $website; ?> OR <?= $website ?>
<?php echo $phone_number; ?> OR <?= $phone_number ?>
We hope this article helped you to learn about CakePHP 4 Send Parameter Values To View Templates 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.