【问题标题】:CodeIgniter routing issue, advice how to do itCodeIgniter 路由问题,建议如何做
【发布时间】:2013-03-20 23:43:09
【问题描述】:

我不是 CI 程序员,只是想学习它。也许这是错误的方法,请指教。

我的控制器(不在子目录中):

class Users extends CI_Controller {

    function __construct() {
        parent::__construct();
    }

public function index($msg = NULL) {

        $this->load->helper(array('form'));

        $data['msg'] = $msg;

        $this->load->view('user/login' , $data);

    }

   public function process_logout() {
        $this->session->sess_destroy();
        redirect(base_url());
    }

}

还有一个登录路径:

$route['user/login'] = 'users/index';

问题是当我想注销时,它会显示 404,因为我的路线中没有它:

$route['user/process_logout'] = 'users/process_logout';

在我看来,我输入了<a href="users/process_logout">logout</a>

当我添加它时,它会起作用,而为所有东西添加路由是很愚蠢的。我做错了什么,请指教。

谢谢

【问题讨论】:

    标签: codeigniter codeigniter-2 codeigniter-url codeigniter-routing


    【解决方案1】:

    不知道你为什么要在 index() 函数中实现登录功能。但是,既然您说您正在学习 CI,我正在讲述有关 _remap() 函数的内容。

    在那之前。您可以尝试以下路由:

    $route['user/:any'] = 'users/$1';
    $route['user/login'] = 'users/index';
    

    如果你想在控制器段之后立即取值,你需要使用 _remap() 函数,这个函数可能会解决你的路由问题,我的意思是你不需要设置路由。让我们使用 _remap() 函数实现您的代码控制器'users'

    class Users extends CI_Controller {
    
        private $sections = array('login', 'logout');
    
        function __construct() {
            parent::__construct();
    
        }
    
       public function _remap($method)
       {
            $section = $this->uri->segment(2);
    
            if(in_array($section, $this->sections))
                call_user_func_array(array($this, '_'.$section), array());
    
            else show_404(); // Showing 404 error
       }
    
       private function _login() 
       {
            $msg = $this->uri->segment(3);
    
            $this->load->helper(array('form'));
            $data['msg'] = $msg;
            $this->load->view('user/login' , $data);
        }
    
       public function _logout() {
            $this->session->sess_destroy();
            redirect(base_url());
        }
    
    }
    

    【讨论】:

      猜你喜欢
      • 2011-09-16
      • 2012-04-27
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多