An Introduction to Joomla’s Session Registry

Note: This post is a bit advanced when it comes to Joomla – and is mainly targeted at developers who are developing an integrated solution for their clients using Joomla.

So what is Joomla’s registry?

Joomla’s registry is a temporary storage for variables that can be passed back and forth between different extensions on the same page. These variables are stored and retrieved using Joomla’s JSession class – which by default stores its information in the table jos_session in the database (substitute jos with the prefix of your Joomla’s website). Registry variables are available at the page level – which means that if you move to another page then these variables are destroyed.

When should Joomla’s registry be used?

Joomla’s registry is very convenient if you want your different extensions to talk to each other. For example, you might want one of your modules to display differently based on some data from your component. A real life example would be a generic paging module. A paging module, in order to display properly, needs the following information:

  • The current URL: The current URL is returned by many Joomla and non-Joomla functions. You can get the URL using the Joomla function JURI::getInstance();.
  • The number of entries per page: This is the maximum number of entries on each listing page. This value is necessary for the module to know how many pages (in total) there are. This value is usually stored in a custom settings table in Joomla’s database.

  • The maximum number of pages: The maximum number of pages specifies the maximum number of links to other pages we can have in our paging. For example, if we’re currently at Page #1, then we will have links to Pages 1 through 10 (if that maximum is 10). Again, this value is typically stored in the settings table from Joomla’s database.

  • The number of entries in the current view: This is a value that is known by the view itself – and this where we need to use the registry – the number of entries in the current view needs to be passed by the view of the component to the module itself – and the most efficient and elegant way to do this in Joomla is by storing the number of entries in the registry in the component and then retrieving it from the registry in the module.

But, how do you store a variable in the registry?

The code to store a variable in Joomla’s registry is quite easy (it’s actually a two-line code):

$app =& JFactory::getApplication();
$app->setUserState("registryVar", 5);

In the above code, we are storing a variable called registryVar that has a value of 5 in the registry.

…and how do you retrieve it?

Again, it’s a two line code:

$app =& JFactory::getApplication();
$localVar = $app->getUserState("registryVar", 0);

In the code above, we are assigning the value of the local variable $localVar to the value of the registry variable registryVar. If the system can’t find the registryVar in the registry, then it will automatically assign the value of $localVar to the value of the second parameter of the method getUserState (which is 0 in our example above).

Why not use session variables instead?

There are two reasons why you shouldn’t use session variables in the case where you want two extensions to talk to each other on the same page: 1) session variables are not available on the same page they are assigned in and 2) session variables will be available globally to the application, which is not what you need if you only need them on a certain page.

Is Joomla’s registry stable?

Yes it is – we’ve used it a lot and it’s quite stable. However, we noticed that the registry will misbehave (e.g. will not correctly store variables and/or will not correctly retrieve them) if you change the URL of your website (which can happen if you move from a development environment to a production environment). This can be solved by emptying the jos_session table directly in phpMyAdmin or by running the following PHP script in one of your Joomla files that will contain an instance of the Joomla application (make sure you remove this code after you run it once):

$db =& JFactory::getDBO();
$sql = "TRUNCATE TABLE #__session";

If you want Joomla experts to help you build your application by using Joomla’s framework, then all you need to is to contact us. We do know Joomla (a lot about it, in fact), we are nice people to work with, our rates are affordable, and we’re always looking for new challenges! What are you waiting for?

No comments yet.

Leave a comment