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

How Can I Modify a Line in a ControllerPublic File

Discussion in 'XenForo Development Discussions' started by Tilkißey, Mar 24, 2013.

  1. Tilkißey

    Tilkißey Well-Known Member

    Hi All,

    I want to modify a line in a ControllerPublic_X file using via listener
    How can i do that?

    for example (that's not mine problem, it is just for example)

    original line
    $latestUsers $userModel->getLatestUsers($criteria, array('limit' => 8));
    i want to modify like that
    $latestUsers $userModel->getLatestUsers($criteria, array('limit' => 100));
    that is just example

  2. Jake Bunce

    Jake Bunce XenForo Moderator Staff Member

    With a listener you can extend the entire actionIndex(). That allows you to execute code before or after the original is called. For example, your extended function could call the parent and then afterwards replace the 'latestUsers' param in the response, querying again the latest members.

    Create a listener for load_class_controller to extend the class. Here is a code example of an extended controller:


    You can run the parent function, capture the response, do your own stuff, then return the original response (or modified response).
    Tilkißey and Chris D like this.
  3. Chris D

    Chris D XenForo Developer Staff Member

    You are basically trying to do what one of my add-ons does.

    There are much better ways of doing it, but I basically modified the parameter and requeried with my desired criteria. I should have also extended the view instead of the controller too...

    But you can see how I did it in my add-on. It's one way but not the best or only way:


    EDIT: Nice ninja :)
    Tilkißey and Jake Bunce like this.
  4. Tilkißey

    Tilkißey Well-Known Member

    thanks for your answer Jake and Chris =)
    but, it doesnt work for me
    maybe i couldnt explain myself clearly, sorry =(

    i need give an another example
    line 490 (original file)
    i need
    if i use response, $writer will read the original code and and of course will save it
    and will read my modified code after saving (i tried and it works like that)

    i want to modify line 490 before using dw save function
  5. Chris D

    Chris D XenForo Developer Staff Member

    You would probably need to actually make this change in the DataWriter, rather than the Controller.

    The DataWriter has a function called preSave that you can extend.

    In there you'd be able to do something like:

    Tilkißey likes this.
  6. Jake Bunce

    Jake Bunce XenForo Moderator Staff Member

    The system functions based on extended classes, not directly changing existing code. You could make it so your extended function does not even call the parent, thereby allowing you to copy/paste the entire function into your extended class with the small code change.
    Tilkißey likes this.
  7. Tilkißey

    Tilkißey Well-Known Member

    my first try was copy/paste function with small code change and it worked
    but i have a question for about extend a functions all codes

    there is 1 line changing in my extended (so edited) class
    the 1 line is using $writer

    and u wrote another add-on
    your add-on wants extend the same file (calling parent)

    will there any crash between your add-on and my add-on

    ps: i am sorry due to my English, i need practice but i have to time and teacher :(
  8. Jake Bunce

    Jake Bunce XenForo Moderator Staff Member

    Yes. So you should set the execution order of your listener to a very low number (0 is the lowest). That should avoid any problems.
    Tilkißey likes this.
  9. Tilkißey

    Tilkißey Well-Known Member

    thank u jake (y)

Share This Page