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

How to redirect from hook to another controller

如果没有登录,我想在登录页面重定向用户

我的钩子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里面application/controllers

<?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  !^(index\.php|images|css|js|robots\.txt|favicon\.ico)
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ ./index.php/ [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['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';

给出同样的错误

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

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