Composr Tutorial: Writing mini-modules
Written by Chris Graham (ocProducts)The Composr mini-module feature lets you create new Composr pages very easily. You don't need to code to any particular structure or API, just write plain PHP and output as normal from it. You have full access to Composr's APIs for when you need to interface with the rest of Composr.
This very practical tutorial shows you how via a number of simple examples.
- PHP programmers with no experience with Composr to hit the ground running
- experienced Composr developers to develop simple pages without any coding overhead
- easier porting of third-party PHP scripts into Composr (you may need to change some links around, and remove HTML header tags – but it's a lot easier than doing a rewrite)
We will present 3 examples. To try each out, simply save the code into a site/pages/mini-modules_custom/example_page.php file, then call it up via http://yourbaseurl/site/index.php?page=example_page.
As you can see, site/pages/mini-modules_custom/example_page.php corresponds to a page called example_page, in the site zone.
As mini-modules are just a kind of Composr page, you can control access to them using normal Composr page permissions (i.e. set from Admin Zone > Security > Permissions tree editor).
Example 1The standard introductory example, Hello World.
echo 'Hello World';
Example 2Now let's do some simple Composr API calls.
echo '<p>Hello, ' . htmlentities($username) . '.</p>';
echo '<p>It is ' . htmlentities($time) . '.</p>';
Example 3Need to output a simple spreadsheet? You are allowed to set headers and exit(); within your page, so that Composr doesn't continue doing anything more after your spreadsheet has output and your code has run.
header('Content-disposition: attachment; filename="example.csv"');
// Some arbitrary data to output
$example_data = array(
'country' => 'UK',
'capital' => 'London',
'country' => 'France',
'capital' => 'Paris',
foreach ($example_data as $i => $row)
// If first row, show show headings
if ($i == 0)
foreach (array_keys($row) as $heading)
echo csv_escape($heading) . ',';
// Show values
foreach ($row as $value)
echo csv_escape($value) . ',';
return str_replace('"', '""', $value);
IntegrationsYou may wish to integrate with another database (i.e. not Composr's main database), third-party web-services, or other programming languages.
These complexities can be dealt with in the same way as any PHP programmer would deal with them, while making use of Composr's APIs only as is convenient.
For example, you can use:
- standard PHP database functionality to open a new connection (MySQL, SQL Server, Oracle, …)
- PHP's file_get_contents function to download from REST web services
- PHP's file_get_contents function to download from scripts written in another language, bridging their output over into the Composr page
- PHP's SOAP support to integrate with more complex REST services (assuming the PHP SOAP extension is installed)
- PHP's COM support to integrate to ASP code (assuming the PHP COM extension is installed)
Example 4This simple example shows how to bridge one web system, to another. The example embeds Google into your page. Naturally, this is a silly example, but the same technique can be used to bridge your own scripts, should you be more comfortable programming in a language other than PHP.
echo '<div style="position: relative; overflow: hidden">';
Note I had to add a little CSS to stop Google trying to render parts of its layout on-top of the Composr website.
- Making an addon (part 1)
- Introduction to programming
- Composr Code Book
- A PHP/Composr coding primer
- PHP website
Please rate this tutorial:
Have a suggestion? Report an issue on the tracker.