• This site uses cookies. By continuing to use this site, you are agreeing to our use of cookies. Learn more.

Has anyone been able to extend the Controller.php ?

#1
I've tried extending it using the load_controller event listener, but I assume it's already loaded by the time that happens. Is there anyway to extend the main Controller ?
The reason for this is that I'm attempting to customize the way general errors are handled..
 

Daniel Hood

Well-known member
#3
Use the load_controller with no type hint, include your class. Then every controller class extends your class instead of extending XenForo_Controller(Public|Admin)_Abstract which extends XenForo_Controller.
 
#4
How do you include your class? I've tried the standard method of using load_class_controller to attempt to extend "XenForo_Controller" but that didn't work. Are you saying I should instead extend XenForo_ControllerPublic_Abstract?
 

Daniel Hood

Well-known member
#5
Make your code event listener which listens for load_class_controller, leave the type hint blank. Point it to your add on's listener class/method (example: AddOn_Listener::Controller). In /library/AddOn/Listener.php have

PHP:
<?php
class AddOn_Listener
{
public static function Controller($class, &$extend)
{
 $extend[] = 'AddOn_Controller';
}
}
in /library/AddOn/Controller.php have:

PHP:
<?php
class AddOn_Controller extends XFCP_AddOn_Controller
{
 // Extend whatever functions of XenForo_ControllerPublic_Abstract [or XenForo_Controller since the Abstract class extends XenForo_Controller] here
}
 
#6
Ah! I see, that worked perfectly, thank you :)

In my Listener.php class I was only extending it for the class "XenForo_Controller" or "XenForo_ControllerPublic_Abstract", but by just adding the extension regardless of the currently loading class it now works..

Beautiful. Thank you Daniel!
 
#7
I may have spoke too soon.. It worked for my immediately needs, but as I navigate around the site, I now receive the following error on the homepage:
Code:
Fatal error: Cannot redeclare class XFCP_MyAddon_XenForo_ControllerPublic_Abstract in /home/mysite/public_html/library/XenForo/Application.php(528) : eval()'d code on line 1