Building a Joomla Component without Joomla's MVC classes

This project is a work in progress. The basic idea is to build a PHP application that is as decoupled from Joomla as possible, but still able to opt into Joomla services where it seems useful. This means you have all the benefits of a mature CMS (Authentication, Sessions, File Handling, DB layer, Mail wrapper, and the vast extension directory), but you also have a flexible platform on which to build an application you want.

This page is in two parts:

  1. What is the minimal component starting point?
  2. A basic example of a non-MVC component that uses Joomla like a framework

Part 1: Minimal Component

Components can be just frontend, just backend or both. I think it makes sense to take a minimal component to be front end only because I am assuming that it should be visible to someone who isn't an admin.

I am assuming that one would be using discover to install such a thing, and wouldn't be doing upgrades using the Joomla extension installer let along using an upgrade server.

I think you need a router unless:

  1. You only want 1 URI
  2. You want to do everything with query strings
  3. You happy to make lots of menu items and view manifests and link them all up yourself

Given a simple url to segment router is easy to build I think it belongs in this minimal component. But a component needs only a manifest and an entry file to install and run.

Manifest

Location: /components/com_minimal/minimal.xml

<?xml version="1.0" encoding="utf-8"?>
<extension type="component" version="3.7.0" method="upgrade">
    <name>Minimal</name>
    <version>0.0.1</version>
    <description>Testing Only</description>
    <author>Minimal</author>
    <creationDate>Dec 2017</creationDate>

    <files folder="site">
        <filename>minimal.php</filename>
        <filename>router.php</filename>
        <folder>views</folder>
    </files>

    <administration>
        <files folder="admin">
            <filename>index.html</filename>
        </files>
    </administration>

</extension>

Entry File

Location: /components/com_minimal/minimal.php

Description: Catches up to 5 url segments from the router, presumably one would inject whatever you want from Joomla into your own code here. Usually I find that 3 uri segments is fine, 1 maps to a controller, 2 is an id and 3 is a flag for editing, but your requirements may be much different.

<?php
defined('_JEXEC') or die;

$max_segments = 5;

$jinput = JFactory::getApplication()->input;
$segments = [];
for($i=0;$i<$max_segments;$i++){
    $segments[] = $jinput->get('segment'.$i,'','CMD');
}

// load your own custom php application here
echo "<pre>" . print_r($segments,1) . "</pre>";

Router

Location: /components/com_minimal/router.php

Description: splits the url segments into input variables starting from the component base menu item , ie. /menu-item/{segment0}/{segment1} etc. The build method is required to exist but is not used. If you are only displaying content or something through a single menu item then you don't even need a router! If seo isn't important you could just use query strings and pull them from the input if you want.

<?php
Class MinimalRouter extends JComponentRouterBase
{
    public function build(&$query)
    {
        return;
    }

    public function parse(&$segments)
    {
        $vars = [];
        foreach($segments as $i => $s){
            $vars['segment' . $i] = $s;            
        }
        return $vars;
    }
}

Default Menu Item

Location: /components/com_minimal/views/minimal/tmpl/default.xml

Description: Provides a Menu Item Type to point to the component. To be able to have a menu item appear in your menu type options in your menu manager, then you need an xml file in a views folder like in the location above. Even if there is no view class in there, this is just the place Joomla looks for the xml files. You can of course add further settings and more menu item types to your extension if you want.

<?xml version="1.0" encoding="utf-8"?>
<metadata><fields name="params"></fields></metadata>

Ok, that's the whole component. It obviously does nothing and unless your requirements are very small you would need to make more folders and files etc, but this will install and run and capture the uri segments just fine. Anything you echo out will be rendered by the template and any modules assigned to the menu item will display too, if you want to return json or take over the layout then just exit the request early.