Using the papaya CMS API

From PapayaCMS

Jump to: navigation, search

Here are some hints on using interesting parts of the papaya CMS API. If you have never done any papaya development, you should read the basic module development tutorial first.

Using the Application Registry

As of papaya CMS 5.1, a central application registry has been introduced. This is much more convenient than using lots of different Singletons. Besides, the Application registry can easily be replaced by a custom mock object for unit testing.

Obtaining the Application Instance

To obtain the Singleton instance of the registry, add the following line to your code:

$application = $this->getApplication();

Getting Objects from the Registry

To get a registered object from the registry, use the following code, provided $application is a reference to the Singleton registry instance:

$object = $application->getObject($objectIdentifier);

$objectIdentifier is a string that represents the object you want to obtain. Some typical objects will be introduced later in this section. One example is the 'Surfer' identifier that is used for the current surfer, i.e. the user browsing the website. Use the following code to obtain the surfer object:

$surfer = $application->getObject('Surfer');

In older papaya CMS code, the Singleton approach is used to obtain the current surfer. Consider the following code deprecated and do not use it in any new modules:

include_once(PAPAYA_INCLUDE_PATH.'system/base_surfer.php');
$surfer = &base_surfer::getInstance();

The PapayaApplication class supports a fluent interface approach. This means you can obtain an object in a single step, like so:

$surfer = $this->getApplication()->getObject('Surfer');

PapayaApplication also implements the magic PHP getter and setter methods, __get() and __set(). So if you try to access something that looks like a public attribute, __get() will try to call getObject() with the attribute name as an argument. So

$surfer = $this->getApplication->Surfer;

is synonymous to

$surfer = $this->getApplication->getObject('Surfer');