从一个控制器重定向到另一个控制器

时间:2018-12-23 17:44:56

标签: codeigniter

我的Codeigniter route.php设置为:

$route['default_controller'] = 'welcome/view';

我正在尝试使用redirect('test')从默认控制器的视图方法重定向到测试控制器,但出现致命错误:Uncaught TypeError:传递给CI_Exceptions :: show_exception()的参数1必须是一个实例异常,给出错误实例。

class Welcome extends CI_Controller {
    public function view()
    {
        redirect('test');
    }
}

-

class Test extends CI_Controller {
    public function index()
    {
        echo 'hi';
    }   
}

当我重定向到测试控制器时,我期望hi作为输出。我不确定该错误的实际含义。有人可以告诉我该错误是什么意思,怎么了?

3 个答案:

答案 0 :(得分:0)

尝试一下

redirect(base_url('Test/index'));

答案 1 :(得分:0)

要使CodeIgniter的URL Helper中的任何功能(例如redirect()base_url()等)起作用,必须配置项$config['base_url']

/application/config/config.php中具有此设置的笔记说

/*
  |--------------------------------------------------------------------------
  | Base Site URL
  |--------------------------------------------------------------------------
  |
  | URL to your CodeIgniter root. Typically this will be your base URL,
  | WITH a trailing slash:
  |
  | http://example.com/
  |
  | WARNING: You MUST set this value!
  |
  | If it is not set, then CodeIgniter will try guess the protocol and path
  | your installation, but due to security concerns the hostname will be set
  | to $_SERVER['SERVER_ADDR'] if available, or localhost otherwise.
  | The auto-detection mechanism exists only for convenience during
  | development and MUST NOT be used in production!
  |
  | If you need to allow multiple domains, remember that this file is still
  | a PHP script and you can easily do that on your own.
  |
 */

请特别注意该行

  

警告:您必须设置此值!

请不要忘记斜杠。

答案 2 :(得分:0)

还在您的config / routes.php文件中定义测试控制器的路由。

$route['test']='Test/index';

并重定向此网址。

 redirect(base_url('test'), 'refresh');