Internal objects


1Overview

When Temma executes a request, it instantiates several objects that are necessary for this processing, and makes them available to controllers and plugins.

These objects are of type:

  • \Temma\Base\Session: user session management (see dedicated documentation).
  • \Temma\Web\Config: configuration information retrieval.
  • \Temma\Web\Request: retrieve and modify data making up the request.
  • \Temma\Web\Response: control of certain response parameters.

2Config

2.1Config : presentation

This object is used to access and modify the site's configuration at the time of the current request.

The configuration object is available in controllers and plugins by writing:

$this->_config

In other objects managed by the dependency injection component, the configuration object is accessible by writing:

$this->_loader->config

2.2Config: read-write attributes

  • appPath : (string) path to the project's root
  • etcPath : (string) path to the project's 'etc' directory
  • logPath : (string) path to the project's 'log' directory
  • tmpPath : (string) path to the project's 'tmp' directory
  • includesPath : (string) path to the project's 'lib' directory
  • controllersPath : (string) path to the project's 'controllers' directory
  • varPath : (string) path to the project's 'var' directory
  • webPath : (string) path to the project's 'www' directory
  • routes : (array) basic routing
  • plugins : (array) list of plugins
  • enableSessions : (bool) indicates whether sessions are enabled
  • rootController : (string) name of the root controller
  • defaultController : (string) name of the default controller
  • proxyController : (string) name of the proxy controller
  • defaultNamespace : (string) default controller namespace
  • defaultView : (string) name of the default view
  • loader : (string) name of the object used as dependency injection component
  • loaderAliases : (?array) associative array containing naming aliases managed by the loader
  • loaderPrefixes : (?array) associative array containing naming prefixes managed by the loader
  • logManager : (null|string|array) name of the name manager(s)
  • logLevels : (null|string|array) log thresholds
  • bufferingLoglevels : (null|string|array) buffered log thresholds

2.3Config: management of extended configurations

The xtra() method is used to read an extended configuration:

// retrieve the entire “x-user” extended configuration
$conf = $this->_config->xtra('user');

// retrieve the “login” key
$login = $this->_config->xtra('user', 'login');

// retrieve “login” key, with default value
$login = $this->_config->xtra('user', 'login', 'admin');

The setXtra() method is used to define a key in an extended configuration:

// set the “login” key in the “x-user” extended configuration
$this->_config->setXtra('user', 'login', 'sysop');

3Request

3.1Request: presentation

Request management is rarely used in application controllers. However, it can be useful in plugins that want to control the execution flow by changing the characteristics of the request.

The request object is available in controllers and plugins by writing:

$this->_request

In other objects managed by the dependency injection icomponent, the request object is accessible by writing:

$this->_loader->request

3.2Request: reading methods

// find out if it's an AJAX request
$ajax = $this->_request->isAjax();

// retrieve path info
$pathInfo = $this->_request->getPathInfo();

// retrieve method (GET, POST...)
$method = $this->_request->getMethod();

// retrieve controller name
$ctrl = $this->_request->getController();

// retrieve action name
$action = $this->_request->getAction();

// retrieve number of parameters
$nbr = $this->_request->getNbrParams();

// retrieve list of parameters
$params = $this->_request->getParams();

// retrieve the first parameter
$p1 = $this->_request->getParam(0);
// retrieve the 2nd parameter, with a default value
$p2 = $this->_request->getParam(1, 'toto');

// retrieve path from site root
$path = $this->_request->getSitePath();

3.3Request: writing methods

// method definition
$this->_request->setMethod('POST');

// controller definition
$this->_request->setController('user');
// recommended: update associated template variable
$this['CONTROlLER'] = 'user';

// action definition
$this->_request->setAction('list');
// recommended: update associated template variable
$this['ACTION'] = 'list';

// define parameter list
$this->_request->setParams(['toto', 'titi']);

// define the second parameter
$this->_request->setParam(1, 'tata');

4Response

4.1Response: presentation

This object is used to retrieve and manipulate the information used to generate the response sent by Temma to the browser.

The response object is available in controllers and plugins by writing:

$this->_response

In other objects managed by the dependency injection component, the response object is accessible by writing:

$this->_loader->response

4.2Response: reading methods

// retrieve redirection URL (or null)
$url = $this->_response->getRedirection();

// retrieve redirection code (301 or 302)
$code = $this->_response->getRedirectionCode();

// retrieve HTTP error code (or null)
$httpError = $this->_response->getHttpError();

// retrieve HTTP return code
$httpCode = $this->_response->getHttpCode();

// retrieve name of defined view
$view = $this->_response->getView();

// retrieve template prefix
$prefix = $this->_response->getTemplatePrefix();

// retrieve defined template (or null)
$tpl = $this->_response->getTemplate();

// retrieve list of HTTP headers
$headers = $this->_response->getHeaders();

// retrieve all defined template variables
$vars = $this->_response->getData();
// retrieve a template variable
$var = $this->_response->getData('var');
// retrieve a template variable with a default value
// (if default value is used, it is added to template variables)
$var = $this->_response->getData('var', 'default');
// retrieve a template variable, using an anonymous function
// to define the default value
$var = $this->_response->getData('var', function() {
    return 'default';
});
// same as above, but providing a parameter to the anonymous function
$var = $this->_response->getData('var', function($param) {
    return $param * 2;
}, $autreValeur);

4.3Response: writing methods

// define a temporary redirection (code 302)
$this->_response->setRedirection($url);
// set permanent redirection (code 301)
$this->_response->setRedirection($url, true);

// set HTTP error code
$this->_response->setHttpError(500);

// set HTTP return code
$this->_response->setHttpCode(404);

// set view name
$this->_response->setView('\Temma\Views\Json');
$this->_response->setView('~Json');
// disable view processing
$this->_response->setView(false);

// add a template variable
$this->_response['key'] = 'value';
// delete a template variable
unset($this->_response['key']);
// redefine all template variables
$this->_response->setData([
    'key1' => 'value1',
    'key2' => 'value2',
]);
// add multiple template variables
$this->_response->addData([
    'key3' => 'value3',
    'key4' => 'value4',
]);
// delete all template variables
$this->_response->clearData();

// set template prefix
$this->_response->setTemplatePrefix($prefix);

// set template path
$this->_response->setTemplate('article/voir.tpl');

// add HTTP header
$this->_response->header('Content-Type: application/pdf');