Web-site + plugins - how?

Not for 'how-to' coding questions but PHP theory instead, this forum is here for those of us who wish to learn about design aspects of programming with PHP.

Moderator: General Moderators

Post Reply
malcolmboston
DevNet Resident
Posts: 1826
Joined: Tue Nov 18, 2003 1:09 pm
Location: Middlesbrough, UK

Web-site + plugins - how?

Post by malcolmboston »

Ive often wondered how systems such as Magento, Swift mailer etc offer the use of plugins, how would this be reflected in the actual code, i would like to do this for my ecommerce framework but have no idea how to start.

Any points in the right direction would be great.
User avatar
pickle
Briney Mod
Posts: 6445
Joined: Mon Jan 19, 2004 6:11 pm
Location: 53.01N x 112.48W
Contact:

Re: Web-site + plugins - how?

Post by pickle »

I've often wondered that too. I found out when I looked through the source code for MyBB. I'm sure there are as many solutions as there are plugin-able architectures, but this is how MyBB does it.

Embedded in the application are a bunch of different hooks. Plugins can then register with the main application to be run at these hooks. So say you have an authentication plugin - you want to use LDAP instead of the default database. You could then write a plugin that would be run at the "authenticate" hook.

Application code example:

Code: Select all

//Somewhere in a config file, the plugin is registered
$Plugins->register('authenticate','/path/to/my/plugin','functionName');
 
//somewhere in the application, the hook is called
function authenticate($username,$password)
{
   if($Plugins->registered('authenticate'))
      return $Plugins->runHook('authenticate',$username,$password);
   else
      //default authentication code runs here
}

Plugin code example:

Code: Select all

function functionName($username,$password)
{
   //check LDAP for valid credentials
   return TRUE;
}
Real programmers don't comment their code. If it was hard to write, it should be hard to understand.
User avatar
JNettles
Forum Contributor
Posts: 228
Joined: Mon Oct 05, 2009 4:09 pm

Re: Web-site + plugins - how?

Post by JNettles »

For my own framework I keep track of registered plugins in a database and then do a system of events that way I can drop in/drop out functionality whenever my clients' needs change. Keep in mind you may want to run multiple plugins for a single event such as having multiple authentication layers.

So when processing a user login I do something along the lines of....

Code: Select all

$authorized = Events::raiseEvent('authentication', $arguments);
 
if($authorized)
{
       ...............
A separate class then loads all registered 'authentication' plugins (which are given a specific order number in the database), runs them and returns either true/false.

*Edit*
I think Pickle showed the same thing but I just wanted to clarify on running multiple plugins for the same event. Its also probably a better idea to inject an array into the event rather than explicitly your arguments as different plugins (even for the same event) could require different data.
alex.barylski
DevNet Evangelist
Posts: 6267
Joined: Tue Dec 21, 2004 5:00 pm
Location: Winnipeg

Re: Web-site + plugins - how?

Post by alex.barylski »

Look at Joomla, their extension system is by far the most robust I have ever seen, more than Drupal, CMSMS, Typo, ModX, etc...

They really take the idea of extension to the next level, IMO. With that flexibility comes complexity, so be prepared to learn. :P
Post Reply