如何从钩子重定向到另一个控制器

时间:2015-08-21 17:34:36

标签: php .htaccess codeigniter codeigniter-3

如果未记录,我想在登录页面中重定向用户

我的钩子HK_Login.php在application / hooks中

<?php

    class HK_Login
    {
        private $CI;
        public function checkLogin()
        {
            $this->CI =& get_instance();
            if($this->CI->session->userdata('ID') == '' && $this->CI->uri->segment(3) != 'login' )
            {
                redirect($this->CI->uri->segment(1) . '/secure/login');
            }

        }
    }

我的控制器Secure.php在应用程序/控制器中

<?php
class Secure extends CI_Controller
{
    public function __construct()
    {
        parent::__construct();
    }

    public function login()
    {
        $this->load->view('login/index');
    }
}

结果是:

Not Found

The requested URL /web/secure/login was not found on this server.

我的.htaccess

DirectoryIndex index.php
RewriteEngine on
RewriteCond $1 !^(index\.php|images|css|js|robots\.txt|favicon\.ico)
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ ./index.php/$1 [L,QSA]

我的config.php的一部分

$config['index_page'] = '';
$config['uri_protocol'] = 'REQUEST_URI';
$config['enable_hooks'] = TRUE;

自动加载

$autoload['helper'] = array('url', 'custom');
$autoload['libraries'] = array('database', 'session');

hook config

$hook['post_controller_constructor'][] = array(
    'class' => 'HK_Login',
    'function' => 'checkLogin',
    'filename' => 'HK_Login.php',
    'filepath' => 'hooks'
);

这是我的routes.php

$route['default_controller'] = 'dashboard';
$route['404_override'] = '';
$route['translate_uri_dashes'] = FALSE;

为什么要给我这个错误?

在routes.php中添加

$route['/secure/login'] = 'secure/login';

给出相同的错误

1 个答案:

答案 0 :(得分:0)

您收到错误,因为找不到路线。将此添加到您的路线文件

$route['secure/login'] = 'secure/login';