Reputation: 3353
I have been writing some code that creates a generic blog.
Its features are simple, create a post, edit and delete the post and allow comments.
I am trying to write it so that it is very easy to write plugins for, however Im not sure on the best approach.
Some of my ideas:
Have the plugin author write a short script called e.g "config" in which they have an array that has the application(e.g frontend, admin etc), the module(e.g blog, profile etc) and the action(s)(e.g create, edit etc) that thier plugin affects, then include the plugin files when the correct action is run.
//example array in config.php:
array(
'application' => 'admin',
'module' => 'blog',
'action' => array('create','edit')
);
add strings into the views code such as "{form-extras}" and have the plugin author say which string there code will replace. Then use str_replace to replace the {xxx} with the plugin code.
#example code in blog_form.php
<input type="text" name="blog_title" />
<input type="text" name="blog_text" />
{form-extras}
#example code in plugins config.php
array(
'replace' => array('form-extras')
);
Both of these ideas are pretty rubbish and very limited in their use but I am struggling to come up with an better ideas.
I'm not sure how much info about my code people need but the basic dir structure is simple, below is an example:
apps //applications
frontend //app name
modules
blog
views
index.php //list blogs
new.php //create new blog post
actions.class.php
admin
modules
blog
views
index.php //list blogs
new.php //create new blog post
actions.class.php
lib //library of classes like database class
plugins //where plugins will hopefully be installed
web //where the site runs e.g index.php, all the css and js
The Question
Does anyone know of any tutorials/articles on making code easy to write plugins for, or does anyone have any tested methods that I could apply?
Regards
Luke
Upvotes: 1
Views: 1386
Reputation: 19445
Before you start thinking about how to make the plugins system, you have to define what exactly a plugin is for you application, what the plugins can do and what data the plugins will be able to access (for example, posts table but not users table).
Then, take a look at Drupal, i guess it's module system based on hooks is really powerfull and 'simple' to use for developers.
Basically, the idea is that when a module or plugin is installed, everythings you do on your 'core' code, search if there is some module/plugins hooked to that action.
Example:
//your code
$modules_enabled = array('foo', 'bar');
//example action, lets say insert a new blog post. Obviously, nothings prevent you
//to do that in OOP style (i'd never really understood why drupal is almost all procedural).
function create_new_post($modules_enabled, $other_args){
//looks if some modules need to be called before create the blog post
foreach($modules_enables AS $module){
if(function_exists("after_create_new_post_$module")){
$func = "before_create_new_post_$module";
$func($params);
}
}
//do your stuff here
//looks if some modules need to be called after the post is been created
foreach($modules_enables AS $module){
if(function_exists("after_create_new_post_$module")){
$func = "after_create_new_post_$module";
$func($params);
}
}
}
//the module file should look like $hooks_name . $module_name:
function after_create_new_post_foo($args){
//do your job
}
This is a very very sintetic example (and doesnt work!), but should give you the idea.
The only problem here comes with the args that you pass to each hook_function, that have to be documented really good, however the documentation is important whatever path you'll choose.
Some reference: Drupal hooks, hook_insert
Upvotes: 4
Reputation: 17309
What you are looking for is an example and/or information about a plugin architecture. Using this term and Google will reveal many resources.
Examine how this is achieved in existing established applications such as WordPress, Drupal or Joomla.
Consider also this existing question: Plugin Architecture in PHP
Upvotes: 2