How to: Develop a new module for PyroCMS
Posted PyroCMS at May 27, 2009
Creating a new module in PyroCMS is a pretty easy process. I am using Matchbox made by Zacharias Knudsen and he has done a brilliant job of explaning folder structure and basic use in his "Introduction" Wiki page. I strongly suggest you read this if you wish to create a module for PyroCMS.
Now you have your new module structure you can add your controllers, models and views just as you do in CodeIgniter bearing in mind what you have now learn about Matchbox.
However, there are a few bits you will need to know about PyroCMS to get things working nicely.
Public Controllers
In normal CodeIgniter there is only one controller class. In PyroCMS there are three. Controller, Admin_Controller and Public_Controller. To use one of these you can extend them like so:
application/modules/modulename/controllers/modulename.php
<?php
class Modulename extends Public_Controller {
function __construct() {
parent::Public_Controller();
}
function index()
{
echo "Hello world";
}
}
?>
This page will be available to anyone wether logged in or not and will use the frontend design. That means it will use the current active theme and show any login data and navigation, etc and can be viewed via "http://example.com/modulename".
Admin Controllers
Admin controllers have a few different properties to them. It will automatically check that a user has permission to be there, and redirect them to a login page if not. This means they either need to have a user role of "admin" or be allowed specific permissions on the controller or module.
application/modules/modulename/controllers/admin.php
<?php
class Admin extends Admin_Controller {
function __construct() {
parent::Admin_Controller();
}
function index()
{
echo "Hello admin";
}
}
?>
This page can be accessed via "http://example.com/admin/modulename".
details.xml
This is the only dirty part of working with modules. Modules used to be entirely drag 'n' drop but now they require an .xml file that will let the admin panel know they exist, wether they are available to the backend and/or frontend, their readable name and a description.
Soon this will all be moved to the database but for now, we have this nasty solution. Take a look at the contact details.xml to get an idea.
<?xml version="1.0" encoding="UTF-8"?>
<module version="0.5" type="content">
<name>Contact</name>
<description>Adds a form to your site that allows visitors to send email's to you without discolosing an email address to them.</description>
<is_frontend>1</is_frontend>
<is_backend>0</is_backend>
</module>
Summary
That should be everything you need to add a basic new module. If you get stuck, just take a look at the other modules or tweet, Skype or e-mail for a little support. Don't forget, if you make something worth sharing, I would love to include it into PyroCMS.
Comments