【问题标题】:codeigniter redirect to different controllercodeigniter 重定向到不同的控制器
【发布时间】:2012-01-20 16:22:18
【问题描述】:

当用户获得授权时,我试图将用户引导至我的主页。我正在我的 login_check 控制器中进行用户检查并添加 $this->load->view('main'); 页面可以加载但主页中的站点地址仍然显示

http://myprojectname/login_check

但我想让它显示出来

http://myprojectname/main

我必须创建一个新的“主”控制器并加载视图吗?这对我来说听起来是多余的。这是我的代码。

我的login_check.php的一部分

private function _user_check()
{
    $this->load->model('user_query');  //load my model
    $result=$this->user_query->query($this->input->post('username'),$this->input->post('password'))             

    if($result)  //the user is in DB
    {
        $data['view']='main'; 
        $this->load->view('include/template', $data);
        //the address bar shows http://myproject/login_check in main page       

    }else{  //the user is not in DB

        $data['view']='login';
        $this->load->view('include/template', $data);
    }

}

【问题讨论】:

    标签: codeigniter controller


    【解决方案1】:

    首先,您在检查用户是否经过身份验证方面做得很差(仅将用户名/密码传递给模型并不是最好的,应该在发送到模型之前对其进行处理/检查)。

    您可以通过包含帮助程序“URL”轻松进行重定向,并且只需使用:

    redirect('/controller/method');
    

    或者在现实世界中的例子:

    redirect('/main');
    

    Reference Link

    【讨论】:

    • 当您在发送到模型之前说“处理/检查”时,您的意思是 form_validation?如果是这样,我已经在代码的另一部分做了。我需要做 mysql_real_escape_string 吗?我认为 Codeigniter 已经应用了它,因为我使用的是 $this->input->post。谢谢。
    • 不,你很好,如果你使用 Active Queries,CI 会处理所有这些,但是如果你自己编写 SQL 查询,你需要使用 $this->db->escape() 来转义你说“用户名”或“密码”以防万一(多做准备永远不会有坏处)
    • 以这种方式重定向时,Url 在站点域之后添加了一个 index.php,我设法使用 .htaccess 删除了 index.php 后缀,但在使用此重定向方法时它又回来了
    • @ClainDsilva 这可能是因为您没有从配置中删除它......它在那里明确定义,删除它以不附加它。
    • @ClainDsilva 他们留下它的明显原因是因为除非你启用了 apache mod_rewrite,否则你不能只删除 index.php
    【解决方案2】:

    如果你的控制器是Rest控制器,还有另一种方法,但类似于redirect()

    restserver

    您可以在另一个控制器中使用rest客户端来调用服务器控制器上的方法

    restclient

    【讨论】:

      【解决方案3】:
      redirect('controller/function');
      

      例如,您可以拥有 redirect('auth/login'); 路由为

      $route['auth/login'] = 'auth/auth/index';
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2012-07-16
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2012-07-10
        • 2018-09-25
        相关资源
        最近更新 更多