CakePHP View

In CakePHP, views are responsible for creating and showing the output of an application. It means everything that the user sees on the screen is called View. For viewing the content, we have to write the data on the view and the template folder.

App View

AppView is the default application’s View class. So AppView itself extends by this keyword “use Cake\View\View” class included in CakePHP, and it is described in src/View/AppView.php as follows:

<?php
    namespace App\View;
    use Cake\View\View;
    class AppView extends View
    { 
 }
?> 

We can use our own AppView to load helpers that will be used for every View rendered in the application. CakePHP provides an initialize() method that is called at the end of a View’s constructor.

<?php
  namespace App\View;
  use Cake\View\View:
  class AppView extends View
   { 
     Public function initialize()
        {
          // it will load the html Helper in view
          $this -> loadHelper(‘Html’);
         }
 }
?> 

The function of the view layer

· Views – It is a templates file that is responsible for presenting the structure of an application

· Elements - Small, reusable bits of code rendered inside views

· Layouts - Template wrapper code. Most views are delivered inside of a layout

· Helpers - Classes that encapsulate view logic that can be used in many areas of the application like date pattern or currency pattern.

VIEW TEMPLATE

The view layer of CakePHP described as how are you handling your users. Most of the time, your views may be degenerate the use of HTML/XHTML documents to browsers, but you may also need to respond through the use of an isolated application with the use of a JSON, or output a CSV file for a user.

View template files have a default extension of .ctp(CakePHP Template), and it uses alternate PHP syntax for control statement and output. These files contain the logic required to generate the data received from the controller in a presentation format that is ready for the user.

Steps for creating view template File:

1) Click on the www folder of the local server, and it will show your CakePHP folder.

cakephp view

2) Click on the src folder where you will see the template folder.

cakephp view 1

3) Then in the template folder, you have to create a folder whose name is the same as the name of the controller.

cakephp view 2

4) Here we have created a template folder name “Second”, which same as our controller.

cakephp view 3

Now in the Second folder, you have to create a .ctp file and write your message, which you want to see in your template file of the browser.

Note: You have to save your .ctp file with the name of the action/method which you want to call.

Like

Here is the action file nameis fun3.ctp

<p> WELCOME TO THE Tutorials & Examples. IT IS AN ONLINE LEARNING TUTORIAL FOR IT PROFESSIONAL. 

Here we have created a controller with the name SecondController.php.

<?php
 namespace App\Controller;
 use App\Controller\AppController;
 class SecondController extends AppController
 {
 Public function fun3()
 {
 }
 }
 ?> 

We will run this program by type on the url of local server like as localhost/CakePHP3.8/Second/fun3

Here CakePHP3.8 is the folder name inside your local server folder, and second is the controller, while fun3 is the action or method that will run on the server.

This is the output:

cakephp view 4



ADVERTISEMENT
ADVERTISEMENT