Getting started with custom module development for PyroCMS

Written by: Jerel

To help make the development of PyroCMS modules easier I decided to put together a simple module that could be used for a template.

While you will not use all of these folders in the average module the following is the supported folder structure for a module.

sample_1

And at the bare minimum you will have the details.php file and the controllers and views folders.

Download the code (links below), rename the zip and the folder within it to "sample", and upload it to your installation of PyroCMS to see how it functions. It is hosted at http://github.com/pyrocms/sample if you happen to be a git user. I intend for the code to be a template to get you started so do what you want with it. Just don't redistribute it as a tutorial under your name. Also it's not a finished module (some things like Edit/Delete aren't finished).

Leave your thoughts in the comments below and if you have questions once your development is under way refer to PyroCMS' documentation and forums.

 

Update: As of June 21, 2011 This module has been mostly rewritten (I wrote the first version in a big hurry and never finished it to my liking). Create, Edit, and Delete all function properly and the front-end uses Tags only - no php.

Update: As this post is nearing 2 years old I removed the old download link and replaced it with the links below. Download the appropriate version for your installation.
PyroCMS v2.1.x
PyroCMS v2.0.x
PyroCMS v1.3.x

Comments

Gravatar

Dak76

February 01, 2011

With all the pontential emerging in pyro lines of code (and CI), it's also sometimes frustrating for newcomers, after diving in reading and code lines, start off with something.

I'm sure it will be very helpfull to have your template as a guideline.

tyvm for sharing
Again, tyvm

Gravatar

February 18, 2011

This looks to be very helpful from someone that just want's to see code and dissect how to connect the dots between CI development and Pyro Module development.

Thanks!

Gravatar

February 18, 2011


Glad you guys are finding it useful!

If you end up creating a module you might consider submitting it at PyroCMS' store. The more addons available to the community the better.

Gravatar

April 12, 2011

excellent example of an module and how you can develop your own. It helped me get started immediately.
good job and many thanks.!

Gravatar

April 19, 2011

Thank you for this. Given that incomplete documentation of PyroCMS, this is a great help. I am still not totally clear on when to use a widget and module. When would you use a widget, and when a module?

Gravatar

April 19, 2011

@boaz A plugin returns only the data and you are responsible with wrapping it in html when you insert the tag.
A widget would be a small chunk of html that is inserted into content. It may optionally fetch and display data from a module. The advantage of a widget is that it's fairly foolproof to insert. A plugin is more flexible for someone who understands html.
A module is part of the actual application and can be very powerful and complex. (Pages, Blog, Galleries, are all modules) It is accessed by visiting site.com/module-name. To display data from the module elsewhere on the site (like inside Pages) you will need to use a widget or a plugin.

Gravatar

May 11, 2011


i've download the codes, and i placed them to the /addon/modules/. i go to the control panel, and installed the Sample module. then i go to the Sample page, and create an item. the problem occure when i click the 'view' links in the manage column of the item list. it return error message like this: [code removed for readability - the issue is fixed] what i've done wrong? thanks before

Gravatar

Pyronewbie

May 13, 2011

Thanks for this. I want to use PyroCMS, but still very reluctant due to lack of documentation and very low activity within the forums. Hopefully, your example will shed some light in what I want to do. Thanks again.

Gravatar

May 13, 2011

We are planning to start reworking documentation as soon as we release v1.2 so that should improve greatly in the next month or two!

As far as forums activity goes I answer a number of posts per day so there seems to be a bit of activity :)

Hope this module gets you started down the right path. See you around

Gravatar

Nige

June 20, 2011


Hi Jerel Thanks for the sample module. Unfortunately I'm getting the same issue as Uchenk (I think). My steps are....

1.install the sample module

2. add a name and slug in the backend

3.when it comes to viewing the default controller at example.com/sample i get.

[code removed for readability - the issue is fixed] Please note that im not trying to edit, delete, etc. simply create items in the admin panel and then view them by going to example.com/sample

Gravatar

June 21, 2011

Thanks for pointing that out Nige. I spent a little time today and rewrote most of this module to bring it up to date. If you re-download it you should be fine.

Gravatar

James

August 24, 2011

Thanks for the sample. Very helpful for people just starting off.

Gravatar

September 07, 2011

Thanks for this sample, it is a very helpful to start a PyroModule from scratch. There are still some few things I don't well understand for example how to use MY_Model with my own models etc.

Gravatar

November 15, 2011

Thank you Jerel.
I write now a showcase module and your sample was really fast start!

Gravatar

December 21, 2011

Thanks for the code Jerel. It is a great help. I am having trouble understanding what this line of code does: From: \modules\sample\views\admin\items.php

<div class="inner">&lt;?php $this->load->view('admin/partials/pagination'); ?&gt;</div>

Where does the code pick 'admin/partials/pagination', I am unable to figure out where the partial is defined?

Pls help!

Gravatar

December 21, 2011

That loads the admin/partials/pagination view which is found in the admin theme. system/cms/themes/pyrocms/views/admin/partials/pagination.php

Gravatar

January 10, 2012

I use this template for all of my new modules...very handy.

I discovered something this weekend that could make it problematic for new (or even experienced) users.

The events.php file has a function run() that's bound to the run event. This will run EVERY page load. No big deal...except the function calls the get_all() method of the module's default model.

This resulted out of memory errors for me that had no apparent cause...it was several hours of WTF? before I looked in the events.php file and found the function call that was requesting all 19million records in my db table...every time any page ran.

May want to comment that out or something for future users...

Gravatar

January 17, 2012

Hey John, thanks for letting me know about this. I've made a change so this won't happen to anyone else

Gravatar

March 30, 2012

I recently started using PyroCMS and loving it. the interface is clean and intuitive.

Gravatar

Burke

April 27, 2012

Thanks for this 'sample' it will be very helpful.

Gravatar

August 06, 2012

Ups, not working in latest pyro realease. it gives a Fatal error: Call to undefined function js() at line 38.

// We'll set the partials and metadata here since they're used everywhere $this->template->append_metadata(js('admin.js', 'sample')) ->append_metadata(css('admin.css', 'sample'));

Gravatar

August 06, 2012

@Imatt I've updated the links in the post. The updated module is available, it just wasn't the one that the download link pointed to.

Gravatar

September 18, 2012

Excellent start point. I was not clear of where to start.

Doubts: - I see that the code uses keywords as 'TRUE', where the coding guidelines states that "Keywords such as true, false, null, as, etc should be all lower case, as upper case is reserved for constants.". Are guidelines now more flexible on this topic?

  • I use Hungarian Notation for variables. Should I replace all of them according to guidelines in order to contribute modules for PyroCMS?
Gravatar

September 18, 2012

@Pavel This was written a while ago, follow the current coding guidelines.

For contributing code everything needs to match our coding guidelines so that the code all looks as if one person wrote it even tho hundreds were involved.

Gravatar

Le-yo

July 09, 2013

really helpful, thanks

Post a comment