2011-08-16 16 views
2

ここでリダイレクトが機能しないのはなぜですか?私は未定義の関数redirect()を呼び出しています。Codeigniterのリダイレクトが機能しない

class Login extends CI_Controller { 

    function index() { 

     parent::__construct(); 
     $this->load->helper('form'); 
     $this->load->helper('url'); 
     $this->load->view('login_view');   

    } 

    function authenticate() { 

     $this->load->model('user_model'); 
     $query = $this->user_model->authenticate(); 

     if($query) { 

      $data = array(
       'username' => $this->input->post('username'), 
       'is_logged_in' => true 
      ); 

      $this->session->set_userdata($data); 
      redirect('/site/news_feed'); 

     } 
     else { 

      $this->index(); 

     } 

    } 

} 

答えて

14

変更これまでごauthenticate()方法上記の頂部...

class Login extends CI_Controller { 

    function __construct() 
    { 
     // this is your constructor 
     parent::__construct(); 
     $this->load->helper('form'); 
     $this->load->helper('url'); 
    } 

    function index() 
    { 
     //this is only called when someone does not specify a method... 
     $this->load->view('login_view');   
    } 
... 

私は強く、それらのほとんどmanditory使用の自動ロードするためにこれら二つのヘルパーを動かす推薦する...

2

あなたはauthenticate方法でURLヘルパーをロードしていません。クラスのコンストラクタ(これはやっているように見える)に$this->load->helper('URL')を追加するか、それをauthenticateメソッド自体に追加する必要があります。

indexメソッドは特別なメソッドです。他のメソッドが指定されていない場合に呼び出されます。 URLが<your domain>/login/の場合、インデックスが作成されます。それ以外は無視されます。

0

あなたのロジックあなたは正しいですが、いくつかのヘルパーとモデルをコンストラクタにロードすると、いくつかの欠点があります時々

関数内からヘルパーを読み込もうとします。

+0

これらの「欠点」の例? – jondavidjohn

2

試してみてください。

function __construct() { 
    parent::__construct(); 
    $this->load->helper('form'); 
    $this->load->helper('url'); 
} 

サーバーがWindowsの場合、試してみてください。

redirect('/site/news_feed','refresh'); 
0

をあなたのリダイレクト機能の前に

$this->load->helper('url'); 

を配置する必要があります。

関連する問題