Please upgrade here. These earlier versions are no longer being updated and have security issues.
HackerOne users: Testing against this community violates our program's Terms of Service and will result in your bounty being denied.
Options

Calling a method in one controller from another

judgejjudgej
edited September 2010 in Vanilla 2.0 - 2.8
Hopefully this is a simple question.

I have a plugin X with a handler that looks at comments after they have been saved (PostController_AfterDiscussionSave_Handler). Part of the functionality of that method is to call up a method that is created on the DiscussionController in another plugin (namely DiscussionController_Flag_Create).

In my plugin X I cannot seem to see the controller DiscussionController at all, let alone call up its "Flag" method.

What would be the normal way to handle this? Do I need to do something to tell Vanilla to load up the DiscussionController so I can invoke methods that are added to it by another plugin?

I just want to utilise some functionality built into a plugin to avoid having to duplicate it all in my plugin. I suspect I am overlooking something incredibly simple and obvious.

Thanks.

Comments

  • Options
    I'm wondering whether these helpful methods that could be used by other plugins actually belong somewhere else, i.e. in the model. MVC is pretty new to me, so I'm still coming to grips with how things should be structured, but from what I understand, the purpose of a controller method such as DiscussionController_Flag_Create() i.e. DiscussionController::Flag() should really be just to decide what is going to handle the request. The guts of that function (which in this case updates the database) really belongs in the model, and so I should be calling up the model methods and not a controller method (only the browser should invoke a controller method).

    Is that the way it should work?
  • Options
    Calling another controller is not the right way to go - controllers are loaded based on what particular page you request. You should definitely call a model, or duplicate whatever part of the code in the other controller you need to be executed.
  • Options
    @Cerealek Yes, I think I eventually came to the same conclusion. I have copied the code from the other plugin's controller into my plugin's model for now. This will get me moving and I can see about doing it properly later (for the plugin to be releasable).

    I think it is too easy to put code that belongs in the model, into the controller, so it probably gets done far too often; it gets the job done, but makes reuse of the functionality harder further on.
Sign In or Register to comment.