0

I am working on project with Codeigniter. I am stuck in controller problems. Here is my folder schema looks like :

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

I tried to access dashboard_controller.php using redirect() function. Here is my Controller :

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

Here is my Model :

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

But, it doesn't works. Can you guys explain to me? Or Codeigniter routes doesn't support such configuration? Thank you.

Bekti Galan
  • 81
  • 2
  • 8
  • redirect wants a uri, not necessarily the controller name if your routes table remaps www.example.com/test_a/test_a_controller. Is `test_a/test_a_controller/index.php` a valid address? – ourmandave Aug 30 '17 at 10:07
  • Meant to say, is `sitename.com/test_a/test_a_controller` a valid address? =( – ourmandave Aug 30 '17 at 10:31
  • Hi, Few things to check make sure you have properly named the class and file name explained here codeigniter way http://www.codeigniter.com/user_guide/general/styleguide.html#file-naming you also may need to place a htaccess in main directory https://github.com/wolfgang1983/htaccess_for_codeigniter –  Aug 30 '17 at 10:46
  • @ourmandave sitename.com/test_a/test_a_controller.php, test_a_controller.php already a file, not folder – Bekti Galan Aug 30 '17 at 11:06
  • @wolfgang1983 already done. thanks for the advice. :) – Bekti Galan Aug 30 '17 at 11:07
  • @BektiGalan Try and be more clear in your question. show full code where the redirect it. Re edit your question. –  Aug 30 '17 at 11:10
  • @wolfgang1983 Edited. I am sorry I can't show more. I just curious about calling controller inside a folder. Is it allowed or not? Thanks – Bekti Galan Aug 30 '17 at 11:27
  • 1
    I see you have single quote on controller `('$username', '$password')` replace it `($username, $password)` –  Aug 30 '17 at 11:33

4 Answers4

0

You should try with below:

redirect(base_url() . 'test_a/test_a_controller');
Don't Panic
  • 13,965
  • 5
  • 32
  • 51
Mayank Vadiya
  • 1,437
  • 2
  • 19
  • 32
0

Example below

filename: 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');

    }

  }

}

Model

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
}

I think your using 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;
   }
}

When need to create a password use http://php.net/manual/en/function.password-hash.php

0

Well this is how you will need to redirect using base_url:

redirect(base_url('management/dashboard_controller/index'));
Just_Do_It
  • 821
  • 7
  • 20
0

In CodeIgniter the URL is built in the following way:

controller/method/id

I haven't seen anyone place controller files in a sub-directory in 'controller/' before. My guess would be if you're trying to redirect to

management/dashboard_controller/index

where management is the inner directory, dashboard_controller is the controller, and index is the method you could accomplish that by doing:

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

The reason you don't need to explicitly call the method 'index()' is because Codeigniter does that for you automatically. If there is another method within your controller aside from index() and you're trying to redirect to it, then you'd have to explicitly type 'dashboard_controller/foo'.

Husso
  • 262
  • 6
  • 17