Welcome Guest, Not a member yet? Register   Sign In
Stop Processing at end of constructor
#1

[eluser]theprodigy[/eluser]
I have my CI setup using MY_Controller. In MY_Controller, I have a function called show_page(). Each controller sets it's respective variables (including the view to call), and then calls $this->show_page();

That is setup just fine, and works. I've recently run into a situation where I need to check something in the constructor of a controller, and if it returns false, I need to display a particular view and not allow the user to continue.

I wrote out the code to do the check, it returns false (for my test), it sets the variables properly, displays the view properly, and then continues on to the method originally called (as would normally happen with a constructor). I've tried using break and exit() and neither one of these are working for what I need.

How can I stop processing after the constructor, if the method returns false?

Thanks for your help
#2

[eluser]bretticus[/eluser]
The whole purpose of the constructor is to run on instantiation of a class. In other words, the constructor will always run before you can call any method. Calling exit() from the constructor should stop subsequent execution. That means calling the method that returns boolean in the constructor though. And that also means calling it before calling any subsequent code.
#3

[eluser]JoostV[/eluser]
Have you tried
Code:
if($this->checkModel->doCheck() == false) {
   $this->load->library('session');
   $this->session->set_flashdata('message', 'Sorry, could not continue');
   redirect ('some/controller/that/displays/error/message', 'refresh');
}

Alternatively, (but less elegant) you can use
Code:
showerror('Sorry, could not continue');
#4

[eluser]TheFuzzy0ne[/eluser]
That should be "show_error". Smile




Theme © iAndrew 2016 - Forum software by © MyBB