Jump to content
xisto Community
miCRoSCoPiC^eaRthLinG

Php: Need Ideas On How To Create Api Hooks !

Recommended Posts

Hey guys, I've been working on a AJAX powered CMS for quite a while now. Am done with most of the important aspects - except one, i.e. plug-ins. I've studied how apps like WordPress utilise API Hooks to add in plug-ins (hooks offer various injection points in the main CMS's code, where a plug-in can inject it's own code).What I couldn't figure out is how to create one. Anyone here done this before? Can you gimme some examples or guide me to some tutorials on doing this?Thanks,m^e

Share this post


Link to post
Share on other sites

I have a couple starts of ideas, but I want to look more into them first. But I'll tell you one of them. The first idea I have is to right a function that copies the contents of a file and mixes it with the main code into a temp file, and then call the temp file with an include statement, but I don't really know if that is feasible. But I'll look into this more for you and post back when I find something. (I'm pretty busy this week, so it may not be until the weekend).~Viz

Share this post


Link to post
Share on other sites

As far as I've figured out you wouldn't require any temp file. The code can be directly injected on-the-fly while the main program executes. Code injection here is nothing but a simple require or include statement. There has to be some way you can define some sorta callback mechanism so that the code in inserted at the right point.

Share this post


Link to post
Share on other sites

Well, I can try to explain my understanding of how Joomla and Mambo accomplish this.Joomla has 3 specific types of addons.Components, Modules, and Mambots.A component is it's very own system that relies on the Joomla structure to intereact with the user.Basically, any PHP script can be converted to a Joomla component by following the rules that Joomla requires.A component can be nearly completely stand alone and only interact with Joomla when otuput is displayed. Then, the configuration file of the component tells Joomla what to look for. When the corect URL is requested, the script is executed./index.php?option=com_frontpage&Itemid=1 tells joomla to execute the frontpage component in the main content area of the template! For the most part, Joomla doesn't care what that script does or what it displays!A module on the other hand is a much smaller script usually and can interact with components and mambots if needed. These are loaded in the order which they are arranged by the admin based on the menu item and user level at the time of the request. Again, Joomla really doesn't care what the module does or what it displays.With both Components and Modules, you can nearly completely avoid the use of any Joomla functions or classes but rely on your own method of programming. It is easier in the end to just learn to use the Joomla code to write your scripts since they already have the security and error checking needed for best results. For example, it is much easier to use the Joomla database class to perform your database queries through since it does most of the work for you. Basically, when Joomla comes to a place in the template asking for content like a module, it includes the installed modules for that position in the specifed order. In the end, Joomla just uses require () or include() to insert the content.Mambots on the otherhand require much more specific Joomla code to work since you have to return certain values to Joomla for the mambot to work in most cases. For example, a Joomla searchbot requires the returned search results to be presented to Joomla in a very specific way and not following the guidelines for this will result in errors. This is so that Joomla knows how to use the returned data. BAsically, a Joomla Searchbot simple performs a search of the database for a particular component. Content Mambots are usually more complicated since they interact with the content prior to being passed to the browser. This is usually the last step in the preocess of displaying a web page. These can do things like replace special tags in the content with the correct value. The same concept as is used for Bulletin Board code!I don't suggest that you clone the Joomla method but this method offers several benefits.Instead of using "hooks", which allow for additional code to be added, this method actively seeks the additional code. This makes installations as easy as uploading the required files and updating the database . In scripts like IPB, you have to modify several files for every addon! Then if you have several addons already installed, it can cause confilcts or make it hard to find the "hooks" since the code has changed so much. The other benefit is that usually, Joomla addons are still valide after you upgrade Joomla since they are mostly stand alone! I think this simplicity and seperation of code is what has made Mambo and Joomla so popular. The downside is that it can be a little tricky managing everything since a component may have several modules and mambots that need to be configured independently.I hope that this gives you a few ideas to work with. ;)vujsa

Share this post


Link to post
Share on other sites

essentially what you have here is this.

function addAction($actionName, $functionName){

{

 global $actions;

 $actions[$actionName][] = $functionname;

 return true;

}

function performActions($actionName){

  global $actions;

  foreach($actions[$actionName] as $function)

  {

         // perform $function

   }

}

performActions would go where ever you want the hooks to happen. So if you want something to happen after submitting a form page, but before it goes to the database you would insert a call tot he 2nd function.

-reply by theNetimp

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

×
×
  • Create New...

Important Information

Terms of Use | Privacy Policy | Guidelines | We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.