close
Phork Manual Table of Contents
Phork Framework User Guide 1.3.4

CoreController

Filepath: phork/php/core/CoreController.class.php

The CoreController class is an extension to CoreControllerLite which builds a page out of several cacheable nodes.

All the controllers must implement the Controller interface found at phork/php/core/interfaces/Controller.interface.php.

Nodes

The default nodes that make up a complete page in this controller are header, errors, alerts, content, and footer in that order. They're defined in the protected $arrNodeOrder property. The content node is determined by the URL. For example if the URL is http://www.example.org/foo/bar/ then the FooController class (which in this case extends CoreController) would have a displayBar() method. If nodes different than the default nodes are required then they can be overridden in FooController.

The content node is a special node that that's run first, and the error node is a special node that's run last. This allows the content node to dictate things like titles and breadcrumbs to be displayed in the header node, and any errors triggered in the others nodes to be displayed in the error node even though it's technically displayed before the content node. This is accomplished using the output buffering features of the display object.

Caching

In order to use the node cache, each display[Node]() method must have a call to includeNodeCache() at the top, and if includeNodeCache() returns false then the content should be built as usual. This is handled automatically by the displayNode() method, which is the easiest way to include a template.

The nodes to cache are defined in the $arrCacheUrls property in the controller. A regular expression check is performed against the current URL and the list of URLs to cache. If a match is found then the data associated with the match is used to define the cache paramaters which include the node(s) to cache, the namespace and the TTL (expiration time in seconds) of the cache.

If full page caching is required the CacheHooks hook is an alternate to node caching.


Node Cache Example

//cache the content node of the manual pages for 300 seconds
protected $arrCacheUrls = array(
    '#(/manual/[^/]+/[^/]+/)#'  => array(
        array(
              'Node' => 'content',
              'Namespace' => null,
              'Expire' => 300
          )
      )
);

Simple Display Example

//the display method called from http://www.example.org/foo/bar/
public function displayBar() {
    
    //the template path is relative to the template directory and excludes the .phtml extension
    $strTemplate = $this->getTemplatePath('path/to/template');
   
    //display the template and pass in the variables that should be accessible within the template
    $this->displayNode('content', $strTemplate, array(
        'strTemplateString' => 'foo',
        'intTemplateInteger' => 42
    ));
}

Event Display Example

//the display method called from http://www.example.org/foo/bar/
public function displayBar() {
    
    //the template path is relative to the template directory and excludes the .phtml extension
    $strTemplate = $this->getTemplatePath('path/to/template');
    
    //register the event to load the page data on cache failure
    AppEvent::register('controller.displaynode', array($this, 'loadBaz'));
   
    //display the template; any variables needed for the display come from loadBaz()
    $this->displayNode('content', $strTemplate);
}

//the method called by the event object in displayNode() if the cache didn't load
public function loadBaz() {

    //get some data from somewhere like a database or an API or an RSS feed
    $arrData = MyModel::loadAll();

    //the variable $arrBazData will be available in the template
    return array(
        'arrBazData' => $arrData
    );
}