11

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

I have tried this

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

    //do not execute next code
}

but it resulting blank screen.

Wildan Muhlis
  • 1,553
  • 2
  • 22
  • 43

6 Answers6

17

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();
Stack Programmer
  • 679
  • 6
  • 18
Sefus
  • 256
  • 1
  • 5
16

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.

junkystu
  • 1,427
  • 1
  • 13
  • 15
  • Yes this is very useful. I've tried @Sefus solution but it did not work. This worked like a charm :) Thank you – zachu Jan 21 '16 at 00:38
  • This works for me.Where in latest version `die()` Isn't work . – TarangP Dec 05 '19 at 13:14
  • `return` works, but it's not enough if one is using a custom method in the controller method to send the output. E.g., I have a `response()` method that I'm calling in the controller method. I output a JSON which varies depending on the situation. So I have multiple calls to the `response()`. Execution should stop in the `response()`, otherwise I'll have to `return` after each `response()` call. This would not be practical. I manually echo the output in the `response()` and then `exit`. Now, I can freely use the `response()` in any controller method without the use of `return`. – akinuri Jan 21 '21 at 09:53
2

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;
}
RedDragonWebDesign
  • 1,797
  • 2
  • 18
  • 24
1

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

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.

egig
  • 4,370
  • 5
  • 29
  • 50
0

I know am very late. But here is the solution. it has to be outputted to _display. Just incase if any newbie faces the same issue then this would be ideal i feel.

$this->load->view('myView');
$this->output->_display();
die;
Jeeva
  • 632
  • 1
  • 12
  • 21