About Cognifty Modules

Cognifty modules borrow some ideas from Java® Servlets, but they don't jump ship to the entire Java® world. Cognfity modules and services stand on their own as a proven way to organize your code. This style of module has been adopted and adapted by Code Igniter, Zend Framework, and applications like Magento.

What is a Module?

A module in Cognifty is a collection of services. Services are class files that extend Cgn_Service. These services can be thought of as Java ® Servlets, but with a PHP flair. A lot of systems blindly copy Java APIs method for method, with no consideraition for PHP's "shared nothing" approach to serving Web requests.

The Main Service

The default words for all module, service and event settings are "main". By default, the system will look for a module folder called "main", a service file called "main.php", a classname called "Cgn_Service_Main_Main" and a method event called "mainEvent".

The Service Class

The main class service, again, is called "Cgn_Service_Main_Main" and it extends a base Cognifty service class of "Cgn_Service" ("Cgn_Service_Trusted", for spam filtering). Below is the strucutre of the tutorial module which shows the text of this tutorial.

<?php
class Cgn_Service_Tutorial_Main extends Cgn_Service {
 
	function Cgn_Service_Tutorial_Main () {
	}
 
	function mainEvent(&$req, &$t) {
	}
}

Events

Events are triggered by Web activity from an end-user. Events can loosely by associated with events that are triggered in Javascript, or traditional GUI frameworks when widgets are clicked by the mouse.

An event in cognifty is one class method with the word "Event" appended to it. This avoids any potently holes with running untrusted class methods (from a parent class) via URL exploits.

Web requests to Cognifty go through a process of "ticketing" to determine which M/S/E to execute. One URL can run many M/S/E or just one.

Flagging

Cognifty services and events use the concept of "flagging" over explict code execution. This allows for very flexible codeing without adding much overhead. Your event should be the entirety of your business logic for one request. Some frameworks require you to execute the output portion of the request cycle (the "V" in MVC) in your business logic.

A typical MVC-style PHP Framwork:

function someAction() {
    $this->loadLayout();
    //code snippped
    if($this->getRequest()->getParam('changepass')==1){
        $customer->setChangePassword(1);
    }
    //code snippped
    $this->renderLayout();
}

Not only does constantly calling renderLayout become redundant code, it doesn't allow you to easily change the code en-masse, thus leading to stiff and non-reusable code.

Cognifty defaults to running a specified set of routines for all service events after the event is finished. If you want a different sort of output, all you have to do is set a "flag". Setting flags doesn't force the system to behave in a specified way, it simply urges the system to do so, allowing room for massive, non-breaking changes in the late life-cycle of the application.

Cognifty's approach of "everything is a flag":

function loginRun(&$req, &$t) {
    //codesnipped
    if ($redir != '' ) {
        $this->presenter = 'redirect';
        $t['url'] = $redir;
    } 
}

This code shows how the output layer, the "presenter", can be used to send 301 redirects to the client just as it would be used to render HTML output. The following is a list of pre-defined presentation directive flags:

  • default (parses HTML templates)
  • redirect (redirects the users, saves session messages)
  • native (calls $service->output on your own service)

Cognifty Module Example

To create a new module, simply create a new directory under the "cognifty/modules" directory. For this example we will make a new module for browsing a list of contact addresses. This type of module can be used to display a list of partners, or store locations. This type of module would best benefit an intranet site where employees need quick access to verify addresses, phone numbers, etc.

Start by making a new folder under "cognifty/modules/" called "contacts". Create a new php file under contacts called "main.php".

<?php
class Cgn_Service_Contacts_Main extends Cgn_Service {
 
      public function mainEvent(&$req, &$t) {
          $t['message'] = "Hello, World!";
      }
 
}

That's all you need to do for the first step. You should now be able to see "Hello, World!" on your site at the following URL:

http://example.com/cognifty/index.php/contacts/

The index.php is optional, you can turn it off by copying the "README.htaccess.txt" file to ".htaccess" in your base cognifty directory and settting the "use.rewrite" value to TRUE in boot/local/template.ini