How do I create an add-on that allows a user to click a link and call php code?

Yes.

It does mean that they'd need 1.2 to install it, but I don't think we should be keeping backwards compatibility with 1.1.x. We should be encouraging people to used the latest and greatest.
 
Thanks, Chris. I'll use the Template Modification instead.

So what's the next step in creating this add-on? If I understand correctly I need to define or create a route. Remember I would like to provide a link under the post that when clicked will show a form. The form will have three input text fields and a submit button.
 
Creating a route is usually for new content that not already exists. I would not create a route, you can use the existing route for post as the target of your action is a post item. In that case, you just extend library/ControllerPublic/Post.php

Your function would be actionEditdate. If you have a link <a href="/posts/3343/editdate">Edit Date</a>, this actionEditdate function will be executed. Just insert this code by file edit in library/ControllerPublic/Post.php and enter /posts/3343/editdate in your browser (with a correct post_id).

PHP:
public function actionEditdate()
{
  ZEND_DEBUG::dump("Edit Date is executed");
}
 
Just so we're all on the same page (for @AndyB's benefit):

A route is the part of the URL I've bolded in these examples:

Rich (BB code):
http://xenforo.com/community/posts/636892/report
http://xenforo.com/community/forums/xenforo-development-discussions.34/
http://xenforo.com/community/threads/how-do-i-create-an-add-on-that-allows-a-user-to-click-a-link-and-call-php-code.59856/edit
http://xenforo.com/community/members/chris-deeming.11388/trophies

You can, of course, create new Routes. But generally unless you're creating an entire new application, it usually isn't necessary.

A Route defines a Controller.

The Controllers for the 4 URLs I posted above are (these are the PHP Class names):

XenForo_ControllerPublic_Post
XenForo_ControllerPublic_Forum
XenForo_ControllerPublic_Thread
XenForo_ControllerPublic_Member

You can see the existing code in these controllers in these files:

library/XenForo/ControllerPublic/Post.php
library/XenForo/ControllerPublic/Forum.php
library/XenForo/ControllerPublic/Thread.php
library/XenForo/ControllerPublic/Member.php

(Note, the file path is simply the class name with the _ turned to / prefixed with library/ and suffixed with .php)

Controllers define a number of actions. They are highlighted in the above code block in red. (report, edit, trophies). They will be defined in the controller PHP file as: actionReport(), actionEdit(), actionTrophies().

Notice that the forum link doesn't have an action defined. If no action is defined that presumes it is actionIndex().

When the controller action runs, that's all of the logic required for the page to load. You can fetch data from Models, execute any PHP code you like really. Usually you return some sort of controller response. Often that will be a view which loads a template. Or it could just be a redirect.

One thing you might notice above is the part of the URL in green.

This is the "link data". The Route takes responsibility for building links from the data passed into the link. This can be used in your controller action. If your URL contains these parameters then those parameters are available in the controller action.


Once you've got an understanding of how that works, you then need to look at creating a Code Event Listener which "listens" for when controller classes are instantiated.

The event for that is load_class_controller.

Now you understand the basics, the nitty gritty of extending a controller can be found here:

http://xenforo.com/community/threads/how-to-add-more-actions-to-an-existing-controller.11202/
 
Creating a route is usually for new content that not already exists. I would not create a route, you can use the existing route for post as the target of your action is a post item. In that case, you just extend library/ControllerPublic/Post.php

Your function would be actionEditdate. If you have a link <a href="/posts/3343/editdate">Edit Date</a>, this actionEditdate function will be executed. Just insert this code by file edit in library/ControllerPublic/Post.php and enter /posts/3343/editdate in your browser (with a correct post_id).

PHP:
public function actionEditdate()
{
  ZEND_DEBUG::dump("Edit Date is executed");
}

Hi Marcus,

Thank you for the information. One thing I confused about, you wrote "Just insert this code by file edit in library/ControllerPublic/Post.php", wouldn't that be a hack. What I'm trying to do is create a proper add-on.
 
So if I understand correctly, the URL will be something like:

http://xenforo.com/community/posts/636892/change_date
That's right... Though I'd suggest using change-date... the action part of the URL is inferred from the action name in the Controller.

So eventually you will be extending the Post controller with the following function:

PHP:
public function actionChangeDate
{
    
}

And that will be accessible from this URL:

.../posts/636892/change-date
 
This_Is_My_Class -> /library/This/Is/My/Class.php

Convert underscores to slashes, add .php at the end and place within /library/.
 
So in Kier's example:

pic001.webp

The file should be called Class.php and it should be located in:

/library/Andy/ChangeDate/Class.php
 
So in Kier's example:

View attachment 56868

The file should be called Class.php and it should be located in:

/library/Andy/ChangeDate/Class.php
In Kier's example pictured, his file will be located in:

library/Dev/ControllerPublic/Member.php

Do you understand how the class names are structured in XenForo?
 
In Kier's example pictured, his file will be located in:

library/Dev/ControllerPublic/Member.php

Do you understand how the class names are structured in XenForo?

So for my add-on I should create these two files?

/library/Andy/ChangeDate/ControllerPublic/Post.php

and

/library/Andy/ChangeDate/ControllerPublic/Class.php

I think I understand how the class names are structured in XenForo.
 
You said he couldn't use a template modification. Seems pretty insistent to me.
Can you quote my post for that? I remember that I recommend he using extends ControllerPublic instead using new route :) If that really it true... I'm so sorry about that. :)
 
Why are you creating Class.php?

I have no idea what to do next. As I said, Kier's thread doesn't spell things out step by step.

So what what is the name of the first PHP file I should create and which folder should it go into? What should the contents be?
 
That's right... Though I'd suggest using change-date... the action part of the URL is inferred from the action name in the Controller.

So eventually you will be extending the Post controller with the following function:

PHP:
public function actionChangeDate
{
   
}

And that will be accessible from this URL:

.../posts/636892/change-date

Okay, so what do I do to make this work? I assume I need to make a file called:

/library/Andy/ChangeDate/ControllerPublic/Post.php

Is this correct?
 
Top Bottom