Modes

Modes are a way to separate an action's logic throughout multiple files.

Modes in Appzio


Deprecated: Function create_function() is deprecated in /var/www/docs.appzio.com/public/wp-content/plugins/wp-gfm/markdown.php on line 301

Deprecated: Function create_function() is deprecated in /var/www/docs.appzio.com/public/wp-content/plugins/wp-gfm/markdown.php on line 302

When you start using Appzio you will quickly see that controllers tend to get quite bloaty – most of them passing 1000 lines of code. This is because both the logic and the layouts are defined inside the controllers in order to give PHP developers control over everything. The platform provides you will the ability to create modes to avoid this problem.

Some of the actions in the platform are responsible for multiple things that are tightly connected. So you will often see yourself in a situation in which you’re using the same action in multiple places in your app. In order to get better separation here, you can define different modes for your action. In every action you will find a form folder. In that folder there will be a file named like your action. For example – the Mobile Matching action has a file named Mobilematching.php in this folder.

This is one of the configuration files of your action and inside of it you can define the fields that appear in your admin panel. In the $mode array you can define the modes for this action.

$mode = array(
    'matching' => '{%match%}',
    'my_matches' => '{%my_matches%}',
    'my_matches_new_messages' => '{%my_matches_with_new_messages%}',
    'my_invites' => '{%my_invites%}',
    'admin' => '{%admin%}'
);

After declaring your modes, they will be available as a dropdown list when editing your action in  the admin panel.

Inside of it you can define a $mode array which will define the modes. It is an associative array accepting key, value pairs. The key should be the identifier of the action, the value is the name that will show up in the admin panel. Once the mode has been configured you will be able to access it like this:

$this->getConfigParam('mode');

If you don’t have too much logic you can use a conditional statement to execute different methods depending on the chosen mode. This is a common case and you will often see it used in the built-in methods.

switch ($this->getConfigParam('mode')) {
...
}

However there is a built-in way to get better code separation by using different classes for different modes. When you set a mode for your action, Appzio will look for another class before executing the main one. Similarly to themes, the mode class should be named  [Mode][Actionname][Controller | View].php. For example – Mobileproperties is the name of the action and it has a ‘tenants’ mode defined for it. The controller that the platform will look for should be named TenantsMobilepropertiesController.

Example


Deprecated: Function create_function() is deprecated in /var/www/docs.appzio.com/public/wp-content/plugins/wp-gfm/markdown.php on line 301

Deprecated: Function create_function() is deprecated in /var/www/docs.appzio.com/public/wp-content/plugins/wp-gfm/markdown.php on line 302
class TenantsMobilepropertiesController extends MobilepropertiesController
{
    public function tab1()
    {
        ...
    }
}

Themes and Modes


Deprecated: Function create_function() is deprecated in /var/www/docs.appzio.com/public/wp-content/plugins/wp-gfm/markdown.php on line 301

Deprecated: Function create_function() is deprecated in /var/www/docs.appzio.com/public/wp-content/plugins/wp-gfm/markdown.php on line 302

Some actions are separated into modes which you will likely want to extend or modify in a certain way. Like in normal controllers here you can use themes to achieve the same result. The convention you need to follow is the following – [Mode][Actionname][Theme][Controller | View]. For example if we have a mode on the Mobile User Profile action called Status and we wan’t to use an “appzio” theme to extend it, we need to name our file StatusAppzioMobileuserprofile and class accordingly. Class names tend to get quite lengthy but they provide clarity on exactly what is their purpose and what are they overriding.

Example


Deprecated: Function create_function() is deprecated in /var/www/docs.appzio.com/public/wp-content/plugins/wp-gfm/markdown.php on line 301

Deprecated: Function create_function() is deprecated in /var/www/docs.appzio.com/public/wp-content/plugins/wp-gfm/markdown.php on line 302
class StatusAppzioMobileuserprofile extends StatusMobileuserprofile
{
    public function tab1()
    {
        ...
    }
}