Redirection inside controller |
Hi there,
A short fragment of my code in controller: Code: public function index($jack) { My question: is it correct to use return in a controller function? Otherwise, the view 'mypost' will be loaded in any case. Thank you.
Hey,
Absolutely. Haven't tried 4.0 yet, so that system might work differently, but 3.x you echo output or load views yourself, and can return out of controller method at any point when controller code is finished, but you want to let CI finish properly, instead of abruptly calling exit or die.
I'd like to add that you do not need to return a value. So instead of
PHP Code: return null; PHP Code: return; (10-01-2018, 11:11 PM)Pertti Wrote: Hey, Thank you, Pertti.
You must need to put if and else condition for example
public function index ($jack) { if($jack === 'John') { $this->error404(); }else{ $this->load->view('mypost'); } } OR public function index ($jack) { if($jack === 'John') { $this->error404(); exit; } $this->load->view('mypost'); }
@ Gurutechnolabs
There is nothing wrong with his code so your point is pointless. What did you Try? What did you Get? What did you Expect?
Joined CodeIgniter Community 2009. ( Skype: insitfx )
|
Welcome Guest, Not a member yet? Register Sign In |