Avoiding deep-nesting of if/else statements in controllers? - Printable Version +- CodeIgniter Forums (https://forum.codeigniter.com) +-- Forum: Archived Discussions (https://forum.codeigniter.com/forumdisplay.php?fid=20) +--- Forum: Archived Development & Programming (https://forum.codeigniter.com/forumdisplay.php?fid=23) +--- Thread: Avoiding deep-nesting of if/else statements in controllers? (/showthread.php?tid=32003) |
Avoiding deep-nesting of if/else statements in controllers? - El Forum - 07-09-2010 [eluser]sorenchr[/eluser] You know the deal.. halfway through working on your controller that handles logins your code looks something like this: Code: if($this->form_validation->run() == TRUE) Is there a way to avoid this? Possibly by 'stopping' the controller if a single statement is true? Like: Code: if(statement) Avoiding deep-nesting of if/else statements in controllers? - El Forum - 07-09-2010 [eluser]skunkbad[/eluser] That's not a very deeply nested example if you ask me. Avoiding deep-nesting of if/else statements in controllers? - El Forum - 07-09-2010 [eluser]stuffradio[/eluser] That looks about right in accordance to the way I made my authorization stuff. Avoiding deep-nesting of if/else statements in controllers? - El Forum - 07-10-2010 [eluser]Buso[/eluser] I only do the validation in the controller, the other stuff in the model, so I don't have to see it every time. Also, if the TRUE or FALSE block is short enough, you can avoid using 'if' like this: Code: $this->form_validation->run() OR $this->login() // if validation fails, show the login again In this case, the FALSE block is only a function call. Then I can do that. If it was the oposite case, you could change the condition to: Code: ! $this->form_validation->run() OR redirect('success') // if validation passed, show the success page or Code: $this->form_validation->run() AND redirect('success') // if validation passed, show the success page or whatever fits the case Avoiding deep-nesting of if/else statements in controllers? - El Forum - 07-10-2010 [eluser]sorenchr[/eluser] Thanks for the replies. @Buso thanks for the suggestions |