redirect method is always executed in codeigniter
I'm facing problems with a function in codeigniter and redirect function of the url helper. What I'm trying to do is verify if the variable $cat is false, if so, redirect to another page using the util library, and if $cat is not false continue executing the method:
function fotos_perfil($id) {
$this->data["s_menu"] = Util::MENU_CAT_ALBUMS;
$cat = $this->catsdao->getById($id);
if (!$cat) {
//here is the problem!!
$this->util->showErrorPage();
return fals开发者_如何学Pythone;
}
$this->data["cat"] = $cat;
$vuser = $this->usersdao->getById($cat->user);
$this->data["vuser"] = $vuser;
$this->load->view("front/cat/profile-photos", $this->data);
}
This is the method inside util:
function showErrorPage() {
$this->CI->load->helper('url');
redirect('/index', 'refresh');
}
The problem is that when the variable $cat is NOT false, the method "$this->util->showErrorPage();" keeps executing and (surpise!) it does not redirect the page, but it does render the page as if it'd redirect to it. How do I know that the page '/index' is rendered? ... because I have ajax methods there, and tracing the 'fotos_perfil' page with firebug I see the ajax calls of '/index' after the page 'fotos_perfil' is loaded.
I do not know how better can I explain this. It should not enter in the if block, because $cat is NOT false, but the 'showErrorPage' is even executed.
And if the $cat function is false, the 'showErrorPage' is executed successfully and redirects to '/index', that's ok
If I change the function 'showErrorPage' to this:
function showErrorPage() {
echo "hello!!";
}
nothing is echoed. So, the problem is with the redirect function. It is magically executed, avoiding the 'if'
Help please, I do not want to have my '/index' page "executed" in the 'background' of my 'fotos_perfil'
SOLVED: My mistake. I had a hidden iframe in the html causing a 404 and redirecting to the index page. That showErrorpage method is used in all methods along my app. Thank you all.
I think you have a double negation going.
if $this->catsdao->getById($id);
returns false
when there's nothing for the ID, then !$cat
is the same as !false
which is true
.
EDIT: Sorry, I'm thinking all backwards this is of course what you want, to enter the if when you have false, please disregard this.
精彩评论