Stop execution after call $this->load->view()

后端 未结 5 985
野性不改
野性不改 2021-01-18 01:40

In Codeigniter, How could we stop the execution after load a view?

I have tried this

function index() {
    $this->load->view(\'myView\');
             


        
相关标签:
5条回答
  • 2021-01-18 02:01

    https://www.codeigniter.com/user_guide/general/views.html

    There is a third optional parameter lets you change the behavior of the function so that it returns data as a string rather than sending it to your browser. This can be useful if you want to process the data in some way.

    echo $this->load->view('myView', '', TRUE);
        die();
    
    0 讨论(0)
  • 2021-01-18 02:01

    AFAIK, You can't, because Codeigniter have to run other script after you load a view, such as CI need to call display() method from Output class to render your view. In your case(if i'm not misunderstanding), I suggest you to just load your view at the end of your controller.

    0 讨论(0)
  • 2021-01-18 02:07

    This is a great question. After lots of googling, I am currently throwing my error pages this way:

    helpers/X.php

    function load_404_page_then_die($obj)
    {
        $obj->output->set_status_header(404);
        load_page_with_great_races_sidebar($obj, 'errors/html/error_404_custom');
        send_view_then_die($obj);
    }
    
    function send_view_then_die($obj)
    {
        // Force the CI engine to render the content generated until now    
        $obj->CI =& get_instance();
        $obj->CI->output->_display();
    
        die();
    }
    

    controllers/X.php

    $data['volunteer'] = $this->volunteer_model->get_volunteer_by_id($volunteer_id_and_text) OR load_404_page_then_die($this);
    

    The other way to do it is to use "return" while in a public method of the controller. However, this results in code that doesn't fit on one line, and you can't use this technique while in a private method of the controller or while in a helper file.

    controllers/X.php

    $data['volunteer'] = $this->volunteer_model->get_volunteer_by_id($volunteer_id_and_text);
    if ( empty($data['volunteer']) )
    {
        load_404_page($this);
        return;
    }
    
    0 讨论(0)
  • 2021-01-18 02:10

    I know I'm late but I came across this whilst searching to find out if loading a view stops execution but apparently it does not.

    It might still be useful to someone so... To stop execution after loading a view, you can just return:

    $this->load->view('myView');
    return;
    

    This will not stop PHP (unlike die() or exit()), CI will load the view as it normally would and any following code will not run.

    0 讨论(0)
  • 2021-01-18 02:12

    You should call load->view at the end of your controller method. If you stop execution then other parts of the theme will not be loaded. Your view file contents will only be rendered. You can use this method for ajax calls where you do not want any thing else should be rendered except what you want to render as out put of ajax call.

    0 讨论(0)
提交回复
热议问题