tags:

views:

89

answers:

1
function Home()
{
    parent::Public_Controller();
    $this->load->library('survey_form_processing');
    //Load Helpers
    $this->load->helper('form');
    $this->load->library('Paypal_Lib');
    $this->lang->load('userlib');
    $this->load->model('home_model');
    if($foo == true){
        redirect('home');
    }
}

This is my construct of the "Home" controller. If a value == true, how can I redirect to the index function? When I try this I get an error...

Is it possible to redirect to the index function?

P.S. I'm a php/CI noob!

A: 

Based on the information you provided, I'd say that you're stuck in an infinite redirection loop :

  • The user goes to the Home page
  • In the constructor, it checks the subscription and if it fails, it redirects to Home
  • ==> loop !

You may have some solutions :

  • Detect that the method called is index and in that case don't redirect (you can use the function router->fetch_method()
  • Perform the redirection to another Controller
  • Don't perform the redirection in the constructor but in each Function. You just have to put the subscription control in a function and call it at the beginning of each function.

The easiest would probably to change your condition. From

if($foo == true)

To

if($foo == true && router->fetch_method() != 'index')

So that if CodeIgniter is trying to reach the Index it won't be redirected again.

(I don't have a php server here so I can't check the exact value). But the idea is here.

Julien N
Very close. Now that it's almost working, I don't know if this is the best method for me, but you got the question answered!
Kevin Brown
If I was you, I'd do a default Controller, accessible to everyone, the "Home" and all the content that need a subscription should be under another Controller. You add your redirection only on the second one, and you redirect to the first one.
Julien N