XF 2.2 Call to protected method in controller A from Controller B?

Earl

Well-known member
For an instance, I'm extending the public Thread controller, and I have this new action actionAppendReply in it.

I want to call setupPostEdit and assertViewablePost protected methods in the public Post controller from the thread controller actionAppendReply as opposed to duplicating the code.

Is this possible?
 
Short answer is that you can't call a protected method from another class. You could make new public methods in that class (or an extended class) that wrap the protected methods to effectively make them public.

Saying that, I probably wouldn't implement it the way you're suggesting. setupPostEdit will be explicitly assuming all the input from a post edit. While you would potentially saving code, you're potentially setting it up to be more brittle as other add-ons may be extending the post edit process but this wouldn't apply to your alternative action (or if we change what we expose here in the future, you may start changing things unexpectedly).

assertViewablePost potentially also does some other things you may not want, as the assumption of usage is that it's the "primary" permission check for the page. assertViewableThread would already do that in general thread context. Checking whether the post is viewable (and editable?) is probably all you really need to do, and you can do that directly.
 
Short answer is that you can't call a protected method from another class. You could make new public methods in that class (or an extended class) that wrap the protected methods to effectively make them public.
Well, I've already tried this but the problem is $this->rerouteCotroller() method only accepts actions as its parameter. helper methods don't work with that, right, the method has to be declared with the 'action' prefix? if that's the case, is there any other decent way to call a helper method in a controller class from another controller class?

Saying that, I probably wouldn't implement it the way you're suggesting. setupPostEdit will be explicitly assuming all the input from a post edit. While you would potentially saving code, you're potentially setting it up to be more brittle as other add-ons may be extending the post edit process but this wouldn't apply to your alternative action (or if we change what we expose here in the future, you may start changing things unexpectedly).
Alright, alright... I'm cool with re-adding setupPostEdit, I realized I have to do little modifications to the original post in order to append that to the last post. So I cannibalized some parts of your code into my extended class and named it setupPostEditForAppend.

but assertViewablePost is almost the same! okay okay fine..! I will do the same for that method too 😬 And thank you for the explanation, @Mike 🥰 I really appreciate it.

Then again, still, I'd like to know the answer to call a helper, but this time, it's for a public method from another class. Is there a decent way of doing this? Thanks in advance. And have a good day.
 
Top Bottom