返回redirect()没有将我带到所需的方法

我正在CodeIgniter 3中建立博客。我想在用户通过身份验证但redirect()无法正常工作时从一个控制器重定向到另一个控制器。


我已经尝试了Stack Overflow中提供的所有解决方案,但是没有人适合我。检查我的代码,并告诉我问题出在哪里。提前致谢。


MY_Controller是我从扩展的核心控制器CI_Controller。


Login_c.php

class Login_c extends MY_controller {


    public function index() {

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

        $this->load->view('public/admin_login_v');

    }


    public function admin_login() {

        $this->load->library('form_validation');

        $this->form_validation->set_rules('username', 'User name', 'required|trim|alpha');

        $this->form_validation->set_rules('password', 'Password' , 'required');


        if($this->form_validation->run()) {

            $username = $this->input->post('username');

            $password = $this->input->post('password');

            $this->load->model('login_model');

            $login_id = $this->login_model->login_valid($username,$password);


            if( $login_id ){

                $this->session->set_userdata('user_id', $login_id);

                return redirect('admin_c/dashboard');


            } else {

                echo "user not authenticated";

            }

        } else {

            $this->load->view('public/admin_login_v');

            // echo validation_errors();

        }

    }

}

admin_c.php

<?php

// ob_start();


class Admin_c extends MY_Controller {

    public function dashboard() {

        $this->load->view('public/admin_dashboard');

    }

}

config.php

$config['base_url'] = "http://localhost/ci_blog";

错误

HTTP 500内部服务器错误


PIPIONE
浏览 167回答 3
3回答

小怪兽爱吃肉

你必须用&nbsp;&nbsp;redirect('admin/dashboard.php','refresh');

喵喔喔

在构造函数中加载会话库。您的代码就像class Login_c extends MY_controller {public function index() {&nbsp; &nbsp; $this->load->helper('form');&nbsp; &nbsp; $this->load->view('public/admin_login_v');}public function __construct(){&nbsp; &nbsp; parent::__construct();&nbsp; &nbsp; $this->load->library('session');}public function admin_login() {&nbsp; &nbsp; $this->load->library('form_validation');&nbsp; &nbsp; $this->form_validation->set_rules('username', 'User name', 'required|trim|alpha');&nbsp; &nbsp; $this->form_validation->set_rules('password', 'Password' , 'required');&nbsp; &nbsp; if($this->form_validation->run()) {&nbsp; &nbsp; &nbsp; &nbsp; $username = $this->input->post('username');&nbsp; &nbsp; &nbsp; &nbsp; $password = $this->input->post('password');&nbsp; &nbsp; &nbsp; &nbsp; $this->load->model('login_model');&nbsp; &nbsp; &nbsp; &nbsp; $login_id = $this->login_model->login_valid($username,$password);&nbsp; &nbsp; &nbsp; &nbsp; if( $login_id ){&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; $this->session->set_userdata('user_id', $login_id);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; return redirect('admin_c/dashboard');&nbsp; &nbsp; &nbsp; &nbsp; } else {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; echo "user not authenticated";&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; } else {&nbsp; &nbsp; &nbsp; &nbsp; $this->load->view('public/admin_login_v');&nbsp; &nbsp; &nbsp; &nbsp; // echo validation_errors();&nbsp; &nbsp; }}}**请勿在使用redirect()之前进行任何打印**
打开App,查看更多内容
随时随地看视频慕课网APP