【问题标题】:How can I load a view of the controller constructor in a method in CodeIgniter?如何在 CodeIgniter 的方法中加载控制器构造函数的视图?
【发布时间】:2015-12-26 09:14:00
【问题描述】:

我是 CI 的新手,我正在尝试使用我自己的数据从控制器内部的方法将用户重定向到视图,如下所示:

class Welcome extends CI_Controller {

    public function doLogin() {
        extract($_POST);
        $this->load->library("form_validation");
        $this->form_validation->set_rules('email', 'Email', 'required|trim|valid_email');
        $this->form_validation->set_rules('password', 'Password', 'required|trim');
        if ($this->form_validation->run() == FALSE)
        {
            exit;
        }
        else {
            if ($this->welcome_model->loginuser($email,$password)==true) {
                header("Location: ".base_url()."explore");
            }
            else {
                $data['errors'][] = "Email and password do not match.";
                $this->load->view('header');
                $this->load->view('welcome',$data);
            }
            return true;
        }
    }
}

如您所见,下半部分应该加载 /welcome, 而是加载 /welcome/doLogin

有没有办法像构造函数一样加载视图?所以它只会加载 /welcome 吗?

【问题讨论】:

  • 去掉 return true 然后再试一次。
  • 只是一些提示:如果您碰巧使用 CI 之类的框架,也请使用重定向方法 - 因此,请考虑使用 redirect(),而不是 header()。此外,这里不需要 return 语句(函数退出、重定向或加载视图)。关于您的问题,您的控制器的其他代码是什么?也许把它给pastebin.com
  • 谢谢,不知道 CI 中存在 redirect()。删除 return true 并没有帮助。
  • 删除return true并使用公共函数index()

标签: php codeigniter


【解决方案1】:

您目前正在使用welcome/doLogin 加载视图,并且您想知道如何通过访问welcome 来加载(view) / doLogin

为此,

public function index(){
     $this->doLogin();
}

通过使用此功能。每当您打开欢迎页面时,它都会自动将您重定向到doLogin() 函数。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-12-18
    • 1970-01-01
    相关资源
    最近更新 更多