Controllers

This page is not translated yet. Showing original version of page. You can sign up and help with translating.

Controllers are part of the MVC architecture. They are objects of classes extending from cot\controllers\BaseController and are responsible for processing requests and generating responses. In particular, after taking over the control from applications, controllers will analyze incoming request data, retrieve the necessary data from the database, pass the results to the templating engine, and finally generate outgoing responses.

#1. Actions

Controllers are composed of actions which are the most basic units that end users can address and request for execution. A controller can have one or multiple actions.

Actionsaremethods of the controllerclassstartingwith the prefixaction.

The following example shows a post controller with two actions: view and create:

namespace cot\modules\post\controllers;

use cot\controllers\BaseController;
use cot\exceptions\NotFoundHttpException;

class PostController extends BaseController
{
   public function actionView(): string
   {
        $postId = cot_import('id', 'G', 'INT');
    
       $post = PostRepository::getById($id);
       if ($post === null) {
           throw new NotFoundHttpException;
       }
       $template = cot_tplfile('blog.post');
        $t = new XTemplate($template);
        $t->assign([
            'ID' => $post['id'],
            'TITLE' => htmlspecialchars($post['title']),
            'TEXT' => $post['text'],
        ]);    
        
        $t->parse('MAIN');
        return $t->text('MAIN');
   }
   
   public function actionCreate()
   {
       $title = cot_import('title', 'P', 'TXT');
        $text = cot_import('text', 'P', 'TXT');
        
        Cot::$db->insert(Cot::$db->posts, ['title' => $title, 'text' => $text]);
        $id = Cot::$db->lastInsertId();
        
        cot_redirect(cot_url('post', ['n' => 'post', 'a' => 'view', 'id' => $id], '', true));
   }
}

In the view action (defined by the actionView() method), the code first loads the data according to the requested post ID. If the data is successfully loaded, the code will render it using the template engine. Otherwise, an exception will be thrown.

In the create action (defined by the actionCreate() method), the code retrieves the data from the request and saves it. Then, it redirects the browser to the view action with the ID of the newly created post.

#2. Routes

End users address actions through the so-called routes. A route is a string that consists of the following parts:

- Extension code: GET parameter e;
- Controller ID: GET parameter n. A string that uniquely identifies the controller among all other controllers of the same extension;
- Action ID: GET parameter a. A string that uniquely identifies the action among all other actions of the same controller.

Routes take the following format:

https://your-domain.tld?e=extensionCode&n=ControllerID&a=ActionID

So if a user requests with the URL https://your-domain.tld?e=blog&n=posts&a=view&id=123, the view action in theposts controller of the blog extension will be called.