登录后重定向当前控制器


redirect the current controller after login

当我登录到我的系统时,我的当前url如下localhost/storeLTE/login/它工作正常,但在我登录并拥有localhost/storeLTE/home之后。但是,如果我写localhost/storeLTE/,则将我重定向到我的登录,并且我必须再次登录。当我写localhost/storeLTE/时,我如何做到这一点?将我重定向到家中而不是登录。例如,我需要在登录后直接指向当前控制器。

  1. 加载视图登录

  2. 在中登录

  3. 获取访问权限
  4. 加载主视图
  5. 我想在我获得家庭访问权限后编写localhost/storeLTE/,以测试我是否在家访问,即使我请求登录视图重定向到家
  6. 居家视图
  7. 请求日志
  8. 加载视图登录

登录控制器。

class Login extends MY_Controller {
    public function __construct(){
        parent::__construct();
        $this->load->model('User_model');
    }
    public function index(){
        $data['module'] = 'Login';
        $this->load->view('login',$data);
    }
    public function getAccess(){
        $username = $this->security->xss_clean($this->input->post('username'));
        $password = $this->security->xss_clean($this->input->post('password'));
        $array = $this->User_model->login($username,$password);
        if($array[0] == 0){
            echo 0;
        }else{
            $data_session = array(
                'id' => $array[0]['id'],
                'username' => $array[0]['usuario'],
                'password' => $array[0]['password'],
                'name' => $array[0]['nombre'],
                'last_name' => $array[0]['apellido'],
                'type' => $array[0]['id_perfil'],
                'logged_in' => TRUE
            );
            $this->session->set_userdata('log',$data_session);
        }
    } 
    public function logout(){
        $this->session->sess_destroy();
        redirect('login');
    }
}

My_controller

<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
class MY_Controller extends CI_Controller {
    function __construct(){
        parent::__construct();
    }
    protected function isLogged(){
        if(!$this->session->userdata('log'))
            redirect('login');
    }
}
/* End of file MY_Controller.php */
/* Location: ./application/controllers/MY_Controller.php */

家庭控制器

<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
class Home extends MY_Controller {
    public function __construct(){
        parent::__construct();
        $this->isLogged();
    }
    public function index(){
        $data = $this->session->userdata('log');
        $data['module']  = "Home";
        $this->load->view('header',$data);
        $this->load->view('home');
    }
}
/* End of file Welcome.php */
/* Location: ./application/controllers/Welcome.php */

路线配置

<?php
defined('BASEPATH') OR exit('No direct script access allowed');
/*
| -------------------------------------------------------------------------
| URI ROUTING
| -------------------------------------------------------------------------
| This file lets you re-map URI requests to specific controller functions.
|
| Typically there is a one-to-one relationship between a URL string
| and its corresponding controller class/method. The segments in a
| URL normally follow this pattern:
|
|   example.com/class/method/id/
|
| In some instances, however, you may want to remap this relationship
| so that a different class/function is called than the one
| corresponding to the URL.
|
| Please see the user guide for complete details:
|
|   https://codeigniter.com/user_guide/general/routing.html
|
| -------------------------------------------------------------------------
| RESERVED ROUTES
| -------------------------------------------------------------------------
|
| There are three reserved routes:
|
|   $route['default_controller'] = 'welcome';
|
| This route indicates which controller class should be loaded if the
| URI contains no data. In the above example, the "welcome" class
| would be loaded.
|
|   $route['404_override'] = 'errors/page_missing';
|
| This route will tell the Router which controller/method to use if those
| provided in the URL cannot be matched to a valid route.
|
|   $route['translate_uri_dashes'] = FALSE;
|
| This is not exactly a route, but allows you to automatically route
| controller and method names that contain dashes. '-' isn't a valid
| class or method name character, so it requires translation.
| When you set this option to TRUE, it will replace ALL dashes in the
| controller and method URI segments.
|
| Examples: my-controller/index -> my_controller/index
|       my-controller/my-method -> my_controller/my_method
*/
$route['default_controller'] = 'login';
$route['404_override'] = '';
$route['translate_uri_dashes'] = FALSE;

您需要将home设置为默认控制器:

$route['default_controller'] = 'home';