1. 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 ?

Discussion in 'XenForo Development Discussions' started by veraderock, Dec 10, 2014.

  1. veraderock

    veraderock Member

    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..
     
  2. veraderock

    veraderock Member

    Btw, I am familiar with the ControllerPublic/Error.php controller, however I need more control! ;) ..
     
  3. Daniel Hood

    Daniel Hood Well-Known Member

    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. veraderock

    veraderock Member

    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?
     
  5. Daniel Hood

    Daniel Hood Well-Known Member

    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
    }
     
    veraderock likes this.
  6. veraderock

    veraderock Member

    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!
     
    Daniel Hood likes this.
  7. veraderock

    veraderock Member

    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
     
  8. veraderock

    veraderock Member

    I've managed to get around it by just extending a few controllers rather then all of them.
     
  9. Daniel Hood

    Daniel Hood Well-Known Member

    can probably do

    PHP:
    if (!class_exists('AddOn_Controller'))
    {
    $extend[] = 'AddOn_Controller';
    }
     
  10. Vladislav Rastrusny

    Vladislav Rastrusny Active Member

    It should be
    PHP:
    if (!class_exists('XFCP_AddOn_Controller'false))
        {
    $extend[] = 'AddOn_Controller';
    }
     

Share This Page