CakePHP Controller

Controller

A Controller, as the name suggests, is a controller that controls the web application by calling the correct action. The controller takes the request from the user through dispatchers like action, which makes sure the right models and views are called. So, it acts as a bridge between models and views.

AppController

The AppController class is the superclass of all controllers, which extends the main Controller class. This class is predefined in src/Controller/AppController.php, and it contains some function that’s shared between all of your controller applications. This class expands CakePHP's controller class.

Controller Actions

All the method of the controller, which is predefined in the controller class, is known as Actions. It is responsible for handling the request and sending an appropriate response to the browser. It represents output in the form of visuals that can be provided to create a responsive browser for the user. Furthermore, the view file is rendered in the controller by defining the action in controllers.

Syntax:

public function action_name()
  { // here we can define variable and statement
  }

Or

 public function index()
  { 
  } 

Here, action_name()  and index () methods are the activities that can be called by the controller in your localhost server.

Here is an example in which we will learn how to create a controller in CakePHP.

Step1. Firstly, we have to go to our localhost server like wamp or xampp of the C directory folder. If in your system have installed a wamp server, then go to www folder and click on the CakePHP folder, which you have created during the installation of CakePHP. If your system has xampp installed, then click on the xampp folder of C directory, where you will see an htdocs folder. In that folder, you will find the name of that folder which you have created during the installation of CakePHP via Composer. In the given below image, you will find the folder name of your project.

 Step2. Now click on the CakePHP folder of your project, then it will show you this page.

click on the CakePHP folder of your project

Step3. Now click on the src folder.

click on the src folder

Step4. After clicking at the src folder, you will find the controller, view, model and template folder.

After clicking at the src folder

Step5. Click on the Controller folder in which you have to create a Controller file with the use of controller name as a suffix in Controller class like FirstsController.php and SecondsController.php. Here we have created many controllers in the CakePHP3.8 folder.

Click on the Controller folder

In this example, we have created our controller with the name of FirstController.php.  

 

Now you have to run this file on the local server like wamp or xampp by writing on the URL like localhost/CakePHP3.8/First/fun

  1. Cakephp3.8 is the folder name of your project
  2. You can write your Controller name either in a small letter or capital letter like First/first is the controller name
  3. fun is the action name that you have to call in your Controller after writing the Controller name in your localhost server.

When you run your Controller in the localhost, then it will show an error “Missing Template” because you have not created a view file for this text on the server.

run this file on the local server

NOTE: - If you don’t want to create a template file, then you can run your controller by the use of this syntax “$this->autoRender=false”. You have to define this syntax in your action method of a controller like this.

FirstController.php

autoRender = false; // this function allows us to run our controller without creation of action file
  echo “welcome to the online Tutorial”;
  }
 } ?> 
method of a controller

Here we have created multiple actions in LastsContoller.php.

class LastsController extends AppController
 {
  public function view($id)
  {
    // Action logic goes here.
  }
  public function index($userId, $recipeId)
  {
    // Action logic goes here.
  }
  public function find($query)
  {
    // Action logic goes here.
  }
 } 

And if we want to run a particular method from two or more method, then we have to pass that method on the localhost server by writing simply 

localhost/CakePHP3.8/Controllername/method name

Use of initialize method 

This method is used to load the component or process in the controller by defining their name. CakePHP provides an initialize() method that called the component at the end of a Controller’s constructor for direct accessing.

class AppController extends Controller
 {
  public function initialize()
  {
    // in this function we can load any component, model, method which is require to define in controller like this
    $this->loadComponent('security');
  }
 }