Codeigniter重定向无法正常工作

时间:2011-08-16 18:02:06

标签: php codeigniter

为什么不重定向在这里工作。我正在调用未定义的函数redirect()。

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();

        }

    }

}

6 个答案:

答案 0 :(得分:16)

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');        
    }
...

我强烈建议将这两个帮手自动加载,因为它们几乎可以使用......

答案 1 :(得分:3)

尝试:

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

如果您的服务器是Windows,请尝试:

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

答案 2 :(得分:2)

您没有在URL方法中加载authenticate帮助器。您必须将$this->load->helper('URL')添加到类构造函数(它看起来像您尝试这样做),或者您必须将它添加到authenticate方法本身。

提醒一下,index方法是一种特殊方法 - 在没有指定其他方法时调用它。使用URL <your domain>/login/,将触发索引。除此之外,它将被忽略。

答案 3 :(得分:0)

你的逻辑是正确的,但是在构造函数有时中加载一些帮助器和模型时会有一些缺点。

尝试从函数中加载帮助程序。

答案 4 :(得分:0)

你需要放置

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

在重定向功能之前。

答案 5 :(得分:0)

redirect() belong to url helper.

每次需要时都可以像这样加载它。

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

或更好地将其全局加载,这样您就不必一次又一次地加载

//application/config/autoload.php
$autoload['helper'] = array("url");