开发者

How to make a PHP script that's pluginable?

I'm creating my own script using the CodeIgniter MVC framework. Now, i want users to easily modify the site functionality and开发者_开发知识库 adding their own without modifying the code which i've already written.

How do i make my site pluginable ?

EDIT: The users would be the site admins. Not the end user. Basically just like drupal or joomla. Want the admin to be able to create/add plugins to extend site functionality.


There may be a better way that's specific to CodeIgniter, but this is what I would do:

First, create functions for various "hook points" in your code. Say, a function named PreArticle that you call in your code, before an article is displayed.

Allow the user to write code like this:

    addHook_PreArticle('funcToCall');

    function funcToCall( &$articleText ) {
       $articleText = str_replace('Hello', 'World', $articleText);
    }

addHook_PreArticle is a function you've defined, which would add the passed string to some internal list. Then when the PreArticle function is called, each of those functions are executed, passing in any appropriate parameters that you define.


Many CMS's Like Joomla and Blogs like Wordpress use variable function names:

$function="phpinfo";
$function();

You could load this into an array to create a list of functions that can be overridden.


That's a perfect case to use the Observer Pattern. http://devzone.zend.com/article/5

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜