📜 ⬆️ ⬇️

Programmer's notes on the new features of MODx Revolution

In this article I want to consider some new features of MODx Revolution, which were not in the younger branch - MODx Evolution. I started working with this CMF even when the version of modx-2.0.0-beta-2 was available, i.e. about a year and a half ago. I can say that since the summer of 2009, when the system was only in beta, before the release in summer 2010, MODx went a long way in its development, added new features, and the old functionality started working faster and better. I believe that at present MODx Revolution is a really powerful platform for building a wide variety of web applications, from the simplest sites to large portals. So, let's begin.

I will try to describe the following innovations MODx Revolution:
  1. xPDO
  2. Namespaces
  3. Contexts
  4. New caching system
  5. Internationalization

Now about everything in order.


xPDO

First, an extract from the xPDO description from the official site :
')
xPDO is a lightweight ORB library that runs on PHP 4 and 5, providing the benefits of a new database access standard in PHP 5.1 and higher - PDO. It implements a very simple but effective Active Record data access pattern.

xPDO is the MODx community-developed ORB library on which all the work of the system is based, even the main MODx class is inherited from the xPDO class. In MODx Revolution, all database operations are performed using xPDO, component developers are also advised to use this library, however the old mechanism of working with a database from MODx Evolution is still left for backward compatibility.
xPDO allows you to use a single interface to communicate with different types of databases. At the moment, connectors for MySQL and SQLite are available, further expansion of the list of supported databases is planned.
All data manipulations are performed using specialized methods. For sampling, creating, updating and deleting data, the use of direct queries is not required, thus virtually eliminating SQL injections.

The following example selects from the database all resources in the context of a web whose parent resource is set to 3 and prints their headers.

//
$resources = $modx->getCollection(“modResource”, array(
“parent” => 3,
“context_key” => “web”
));
//
foreach ($resources as $resource) {
echo($resource->get(“pagetitle”).”|”);
}


Similarly, create, edit, and delete objects:

//
$resource = $modx->newObject(“modResource”);
//
$resource->set(“pagetitle”, “ ”);
$resource->set(“content”, “ ”);
// .
//! !
$resource->save();

// 1
$resource = $modx->getObject(“modResource”, 1);
//
$resource->set(“pagetitle”, “ ”);
//
$resource->save();

// 5
$resource = $modx->getObject(“modResource”, 5);
//
$resource->remove();


Namespaces

Namespaces are used to identify various components that are not part of the system’s core. In fact, the namespace is a folder in core / components that contains the model files for xPDO, dictionaries, and other resources required by the components.
Namespaces are also used when creating additional pages and menu items in the administration system.
Each external component must have its own namespace, which can be created in the administration system.

Contexts

Context - Resource Storage in MODx. Each resource must relate to one of the contexts. The context has a separate cache of the resource tree belonging to it. Accordingly, each time a resource is added or removed from the context, the system clears the cache. When you re-access the context, the resource tree is generated and written to the cache, which can cause some slowdowns in the site if you frequently add or delete resources. Therefore, if there is a frequently updated site with a large number of documents, it may be wise to put some sections into separate contexts.

Context access policies are implemented using a set of rights that are set for a specific group of users in a certain context. Therefore, you can prevent certain users from editing or even viewing resources in a specific context.
By default, after the installation, there are 2 contexts: the web (the context of the user part of the site) and mgr (the context of the administrative panel).

In the context of the web, anonymous users have only rights to view resources, and users belonging to the Administrator group have full rights to all actions with resources. Accordingly, in the context of mgr, only users from the Administrator group can perform any actions with resources.
Contexts can be used to create multiple sites in a single MODx Revolution installation, as well as to create subdomains in which site users can post their materials. However, to implement this behavior, we will need a small revision of the index.php file, because it initializes the web context by default, and in some cases we will need to initialize other contexts.

New caching system

MODx Revolution provides a new caching system with which the programmer can store any data in the cache and fully manage their life cycle. An example of writing and retrieving data from the cache (taken from the official MODx documentation ):

// -
$colors = array('red','blue','green');
$modx->cacheManager->set('colors',$colors); /* core/cache/colors.cache.php */

//
$colors = $modx->cacheManager->get('colors');
foreach ($colors as $color) {
echo $color.'-';
} /* 'red-blue-green' */


Also saved data can be deleted. Removal example:


$modx->cacheManager->delete('colors');


Internationalization

MODx Revolution allows you to create internationalized versions of sites using dictionaries that are stored in files or a database. Dictionaries are divided into the following levels: languages, topics and strings. In the file system, languages ​​are folders that contain files with themes, which, in turn, contain strings.

Consider an example of organizing the storage of MODx system dictionaries in the file system.
The root folder of the dictionaries contains subfolders whose names match the language notation adopted by IANA (for example, en or ru). Further, each language subfolder contains files with names in the format_name.inc.php format. And each theme file contains lines in the format

$_lang['_'] = '_';

On the site page the value of the string from the dictionary is inserted with the following tag:

[[%_? &topic=`_` &namespace=`modx` &language=``]]

The namespace parameter is used to specify the namespace for which the lexicon should be loaded. The language parameter is optional; in its absence, the language set in the system default settings will be selected.

I tried to consider only some of the new features of MODx Revolution. This system is fraught with much more than I can imagine. I am pleased to continue to study this wonderful, in my opinion, CMF, and have never been disappointed in it.

Resources

  1. MODx official website
  2. MODx Documentation
  3. XPDO website
  4. MODx Russia
  5. Unofficial Russian-speaking community MODx

Source: https://habr.com/ru/post/111448/


All Articles