Skip to content
Eric Lamb edited this page Feb 6, 2022 · 12 revisions

About EE Objects Controllers

The Controllers library allows ExpressionEngine developers to compartmentalize their Actions, Template Tags, Extensions, and Control Panel methods, into stand-alone objects instead of containing everything within a single file in a single class.

When you use EE Objects Controllers, you're no longer up against indecipherable scrolling and searching for what you want to work on. Now, everything's compartmentalized and centralized.

Getting Started

To start using the Controllers library, you have to make a couple changes to your ExpressionEngine Addon.

Requirements

  • ExpressionEngine >= 5.5
  • PHP >= 7.1

Installation

Add ee-objects/controllers as a requirement to your composer.json:

$ composer require ee-objects/controllers

Quick Start

Every Controller object can be used by itself or alongside other Controller types.

Update your ExpressionEngine Addon file(s)

Depending on which portion of your Addon you want to setup as Controllers, you have to make a make a change.

Note that every Controller requires the route_namespace property to let EeObjects know where your Routes are stored.

Extensions
use EeObjects\Controllers\Extension;

class Your_addon_ext extends Extension
{
    protected $route_namespace = 'Namespace\For\Your\Controllers';
}
Control Panel
use EeObjects\Controllers\Cp;

class Your_addon_mcp extends Cp
{
    protected $route_namespace = 'Namespace\For\Your\Controllers';
}
Module (Template Tags / Actions)
use EeObjects\Controllers\Module;

class Your_addon extends Module
{
    protected $route_namespace = 'Namespace\For\Your\Controllers';
}

Route Objects

Regardless of the domain of your Controller, every Route requires a single object that follows a couple rules:

  1. Inherits from a base Route object
  2. Implements a process method
  3. It must be available at the namespace you set with the route_namespace property.

A very basic example would be:

namespace EeObjects\Addon\Controllers\Extension\Routes;

use EeObjects\Controllers\Extension\AbstractRoute;

class TemplatePostParse extends AbstractRoute
{
    public function process(string $final_template, bool $is_partial, $site_id, array $currentTemplateInfo): string
    {
        return $final_template;
    }
}

Clone this wiki locally