重定向到Codeigniter中的另一个控制器

时间:2017-08-30 09:45:12

标签: php codeigniter

我正在与Codeigniter合作开展项目。我陷入了控制器问题。这是我的文件夹架构:

application
 /controller
  /management
    dashboard_controller.php // Dashboard Management
  /administrator
    dashboard_controller.php // Dashboard Administrator

我尝试使用 redirect() 功能访问 dashboard_controller.php 。 这是我的控制器:

...
else{
   $username = $this->input->post('username');
   $password = $this->input->post('password');

   if($this->login_model->get_user_login('$username', '$password')){
      redirect('management/dashboard_controller/index');

这是我的模特:

public function get_user_login($username, $password)
{
    $this->db->select('password');
    $this->db->from('users');
    $this->db->where('username', $username);
    $hash = $this->db->get()->row('password');
    return $this->verify_hash($password, $hash);
}

但是,它不起作用。你们能解释一下吗?或Codeigniter路由不支持这样的配置?谢谢。

解决 我找到了解决这个问题的方法。到目前为止,这solution是最好的。谢谢:))

4 个答案:

答案 0 :(得分:0)

你应该尝试下面的

redirect(base_url()+'test_a/test_a_controller');

答案 1 :(得分:0)

以下示例

文件名:management/Login.php

<?php

class Login extends CI_Controller {

  public function __construct() {
     parent::__construct();
     $this->load->model('login_model');
  }

  public function index() {
    $isValid = $this->login_model->get_user_login($this->input->post('username'), $this->input->post('password'))

    if ($isValid) {
      // You by default will redirect to the index function so not need index at end.
      redirect('management/dashboard_controller');

    }

  }

}

模型

public function get_user_login($username, $password)
{
    $this->db->select('password');
    $this->db->from('users');
    $this->db->where('username', $username);
    $query = $this->db->get();

    $hash = $query->row()->password;

    return $this->verify_hash($password, $hash); Make sure Returns TRUE / FALSE
}

我认为您使用的是password_verify http://php.net/manual/en/function.password-verify.php

public function verify_hash($password, $hash) {
   if (password_verify($password, $hash)) {
      return TRUE;
   } else {
      return FALSE;
   }
}

如果需要创建密码,请使用http://php.net/manual/en/function.password-hash.php

答案 2 :(得分:0)

这就是你需要使用base_url重定向的方式:

redirect(base_url('management/dashboard_controller/index'));

答案 3 :(得分:0)

在CodeIgniter中,URL按以下方式构建:

  

控制器/方法/ ID

我之前没有看到任何人将控制器文件放在'controller /'的子目录中。我的猜测是,如果你想重定向到

  

管理/ dashboard_controller /索引

其中management是内部目录,dashboard_controller是控制器,index是你可以通过以下方式完成的方法:

header("Location:".base_url()."management/dashboard_controller");

您不需要显式调用方法'index()'的原因是Codeigniter会自动为您执行此操作。如果您的控制器中除了index()之外还有另一种方法,并且您尝试重定向到它,那么您必须显式键入'dashboard_controller / foo'。

相关问题