CodeIgniter Forums
Stop controller at this point, run other method - Printable Version

+- CodeIgniter Forums (https://forum.codeigniter.com)
+-- Forum: Using CodeIgniter (https://forum.codeigniter.com/forumdisplay.php?fid=5)
+--- Forum: General Help (https://forum.codeigniter.com/forumdisplay.php?fid=24)
+--- Thread: Stop controller at this point, run other method (/showthread.php?tid=68230)



Stop controller at this point, run other method - krystian2160 - 06-13-2017

In index of my Main.php controller I have something like this:

PHP Code:
$check $this->Logier->check_address($url);

if( 
$check === )
{
$this->show_page($url);
}
elseif( 
$check === )
{
$this->show_post($url);
}
else
{
show_404();
}

Can't I do something like...

$check = $this->Logier->check_address($url);

[size=small][font=Monaco, Consolas, Courier, monospace]if( $check === 1 )[/font][/size]
[size=small][font=Monaco, Consolas, Courier, monospace]{
[/font][/size][size=small]$this->show_page($url);
break();[/size]
[size=small][font=Monaco, Consolas, Courier, monospace]}[/font][/size]
[size=small][font=Monaco, Consolas, Courier, monospace]if( $check === 2 )[/font][/size]
[size=small][font=Monaco, Consolas, Courier, monospace]{[/font][/size]
[size=small][font=Monaco, Consolas, Courier, monospace]$this->show_post($url);
[/font][/size]break();
[size=small][font=Monaco, Consolas, Courier, monospace]}[/font][/size]
show_404();

I mean, to break at this point I want in just executing this actual method.
So it executes show_post, then output and so on.

Let'
s assume $check === 1
It executes then show_page
($urlmethod.
And 
nothing else in this index method. (So it doesn't check next if and doens't execute last command show_404();) 



RE: Stop controller at this point, run other method - skunkbad - 06-13-2017

That's pretty much exactly the same as what you had, because you used if/else if/else. Break won't do anything for you, since you're not in a loop. If it makes you happy, use exit or die instead.


RE: Stop controller at this point, run other method - Wouter60 - 06-16-2017

In some cases, switch is a better alternative:
PHP Code:
$check $this->Logier->check_address($url);
switch (
$check) {
 case 

 
   $this->show_page($url);
 
   break;
 case 
:
 
   $this->show_post($url);
 
   break;
 default:
 
   show_404();
 
   die();