php Codeigniter 重定向不起作用

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/7082795/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-26 01:58:27  来源:igfitidea点击:

Codeigniter redirect not working

phpcodeigniter

提问by el_pup_le

Why isn't redirect working here. I'm getting call to undefined function 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();

        }

    }

}

回答by jondavidjohn

Change the top portion above your authenticate()method to this...

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

I would strongly recommend moving these two helpers to be autoloaded because of their almost manditory use...

我强烈建议将这两个助手移动到自动加载,因为它们几乎是强制性的使用......

回答by Alfonso Rubalcava

Try:

尝试:

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

If your server is windows, try:

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

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

回答by cwallenpoole

You're not loading the URLhelper in the authenticatemethod. You'll have to either add $this->load->helper('URL')to the class constructor (which it looks like you were trying to do), or you'll have to add that to the authenticate method itself.

您没有URLauthenticate方法中加载助手。您要么必须添加$this->load->helper('URL')到类构造函数(看起来您正在尝试这样做),要么必须将其添加到身份验证方法本身。

Just a reminder, the indexmethod is a special method -- it is called when no other method is specified. With the URL <your domain>/login/, index will be fired. Other than that, it will be ignored.

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

回答by Herr

Your logic is inda right but, there are a couple of shortcomings when you load some helpers and models in the constructor sometimes.

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

Try to load the helper from within the function.

尝试从函数内部加载助手。

回答by Prateek Joshi

You need to place

你需要放置

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

before your redirect function.

在您的重定向功能之前。

回答by Abdul Manan

redirect() belong to url helper.

You can load it like this each time you need it.

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

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

or better to load it globally so you don't need to load it again and again

或者更好地全局加载它,这样你就不需要一次又一次地加载它

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