Search code examples
phpcodeigniteroutput-buffering

Why Code igniter holds the output of views in a buffer


I have a very basic code below i am new to code igniter so pardon if my question sound amateur

    public function view($num = 0) {

    echo "View was called<br>";

    $this->load->view("header");
    $this->load->view('xyz');
    $this->load->view('footer');

    echo "View function ended";
    }

Ok so i want to know why code igniter doesn't output result the way it is return

The ouptut of the code is as following

  1. View was called
  2. output of the result by calling random_func()
  3. Renders view

What is expected from the output is as following

  1. View was called
  2. Renders view
  3. output of the result by calling random_func()

Is there any code igniter specific behavior i am missing on?why code igniter holding the views in buffer ?


Solution

  • If you follow Codeigniters lifecycle in /system/core/Codeigniter.php you will see that the final output gets called near the very end of the file.

    So views will always get output to the browser last, regardless of how they are called.

    When a view is called, it gets stored in the buffer so it can be manipulated before it gets sent for final render.

    Manipulating the buffer could be anything from storing one view in another, parsing/injecting variable data/tags etc.

    Codeingiter Lifecycle(in Order)

    1. Defines some Global Constants(CI_VERSION, CI_CORE) version control
    2. Loads the core/Common.php file which includes some GLOBAL helper functions
    3. Checks which ENVIRONMENT the framework is running under(local/remote)
    4. Sets a Custom Error handler for handling Errors/Exceptions
    5. Loads the config file to check if there is a custom subclass prefix eg: (MY_)
    6. Set a liberal script execution time limit
    7. Loads the Benchmark class so anything under this line can be benchmarked!
    8. Loads the hOOk class and checks if their are any 'pre_system' hooks defined in application/hooks
    9. Loads the main config file and assigns everything inside the file to the config array
    10. loads the UTF-8 and URI classes
    11. Loads the router class and sets the default route in /application/config/routes.php => default_controller
    12. loads the output class and checks if there is anything in the cache that can be output straightaway
    13. loads security/Input/lang classes
    14. Loads the Controller class(entry point) and sub classes that extend it in application/controllers
    15. checks to see if a Controller method begins with and _ => this is private
    16. Does some other security checks and looks for an index method in the controller and searches for requested controller
    17. looks for a pre_controller Hook
    18. Instantiates the requested controller from step 16
    19. looks for a post_controller_construct Hook
    20. looks for a controller _remap override function
    21. looks for a post_controller Hook, this happens after the controller __construct method has been called
    22. Outputs the final Render(buffer) to the browser
    23. looks for a post_system Hook
    24. Closes any DB Connections that are open
    25. Back to step 1

    As you can see now from the cycle, If you wanted to intercept it at any point, you would created a HOOK, and those hooks will get injected at the point the hooks are called.

    Hope this helps