CakePHP Controllers

cakephp

In CakePHP, a controller is a class that handles requests from the user and determines how to respond. A controller typically performs one or more actions in response to a request, and each action is a method of the controller class.

Here’s an example of a simple controller in CakePHP:

<?php

namespace App\Controller;

use Cake\Controller\Controller;

class PostsController extends Controller
{
    public function view($id)
    {
        $post = $this->Posts->get($id);
        $this->set(compact('post'));
    }
}

This controller has a single action called view, which takes an id parameter and retrieves a post with the corresponding ID from the database. The view action then sets the post variable, which can be accessed in the view template and used to display the post to the user.

For more information about controllers in CakePHP, you can refer to the documentation at https://book.cakephp.org/4/en/controllers.html.