Migration

For users of the first version of Temma who wish to upgrade to v2, here are a few points to bear in mind.


1Controllers: creation and naming

Controllers must inherit from the \Temma\Web\Controller object and not \Temma\Controller.

The names of controller objects no longer need to have the Controller suffix.
The names of action methods no longer need to have the exec prefix.

The controller initialization method must be called __wakeup() and not init().
The method for finalizing controllers must be called __sleep() and not finalize().

The root action must be called __invoke() and no longer index().
The proxy action must be called __proxy() and no longer proxy().

The name of the template file corresponding to the root action therefore becomes __invoke.tpl, and no longer index.tpl.

Controller utility methods now take an underscore at the beginning of their name, to avoid any ambiguity with the names of actions:

  • $this->template() becomes $this->_template()
  • $this->redirect() becomes $this->_redirect()
  • $this->redirect301() becomes $this->_redirect301()
  • $this->httpError() becomes $this->_httpError()
  • $this->httpCode() becomes $this->_httpCode()
  • $this->getHttpError() becomes $this->_getHttpError()
  • $this->getHttpCode() becomes $this->_getHttpCode()
  • $this->view() becomes $this->_view()
  • $this->templatePrefix() becomes $this->_templatePrefix()
  • $this->subProcess() becomes $this->_subProcess()

2Views: naming

The names of the objects managing the views no longer have the "View" suffix.

  • \Temma\Views\SmartyView becomes \Temma\Views\Smarty
  • \Temma\Views\JsonView becomes \Temma\Views\Json
  • \Temma\Views\CsvView becomes \Temma\Views\Csv
  • \Temma\Views\RssView becomes \Temma\Views\Rss
  • \Temma\Views\IniView becomes \Temma\Views\Ini
  • \Temma\Views\ICalView becomes \Temma\Views\ICal

3Plugins: creation

Plugins must inherit from the \Temma\Web\Plugin object and not \Temma\Controller.

Plugins may still have:

  • Either a plugin() method, which will be systematically called when the object is used (as pre-plugin as well as post-plugin);
  • or a preplugin() method and/or a postplugin() method, which will be called depending on whether the object is used as pre-plugin and/or post-plugin.

Note that the \Temma\Web\Plugin object inherits from \Temma\Web\Controller, so plugins are always controllers with additional capabilities. So a plugin can have actions in addition to its pre-/post-plugin methods.


4Controllers and plugins: Template variables

To write or read in a template variable, we wrote:

$this->set('variable', $value);
$value = $this->get('variable');

Now you have to write:

$this['variable'] = $value;
$value = $this['variable'];

There are similar changes for sessions and cache management.


5Controllers: Syntactic sugar

Previously, when we wanted to make a redirection and then stop all processing (plugins and controller), we wrote:

$this->redirect($url);
return self::EXEC_HALT;

Now we can write:

return $this->_redirect($url);

6Dependency injection component

An object centralizes the instances of the objects handled. It is accessible using the _loader attribute of the controllers.

For example, to access the object containing the configuration:

$this->_loader->config

You can make sure that your own objects are managed by the component (see the documentation). This then allows you to use the objects without worrying about their instantiation.
For example:

$this->_loader->UserGateway->deleteUser($userId);

7SQL: quote() and quoteNull()

If you write your SQL queries yourself, you escape the parameters with the quote() method. For example:

$sql = "SELECT *
        FROM users
        WHERE email = " . $db->quote($email) . "
        LIMIT 1";

The quote() method always returns a string surrounded by apostrophes. So it will convert the string A student's paper to A student\s paper. And it will convert an empty string (as well as the null value) to ''.

Previously, the quote() method did not add apostrophes at the beginning and end of the processed string. It was therefore necessary to add them in the SQL query. It is not necessary any more.

The quoteNull() method does the same thing, except that if we give it an empty string (or null) as a parameter, it returns the NULL string (without apostrophes).

This can be useful when a field accepts null values, and we want to manage that.

In this example, the text field will be set to NULL if the $content variable is empty:

$sql = "INSERT INTO ARTICLE
        SET title = " . $db->quote($title) . ",
            text = " . $db->quoteNull($content);

8SQL: Prepared requests

It is now possible to use prepared queries (see documentation).