【问题标题】:codeigniter URL change代码点火器 URL 更改
【发布时间】:2021-10-16 10:40:15
【问题描述】:

我正在 Codeigniter 中创建电子商务。这是用于注册的控制器文件:

class Register extends CI_Controller {

    public function index()
    {   
        
        $this->load->view('common/header');
        $this->load->view('register');
        $this->load->view('common/footer');
        
        

    }

    public function home(){
        $this->load->view('common/header');
        $this->load->view('common/slider');
        $this->load->view('home');
        $this->load->view('common/footer');
    }
    
    public function signup(){
        $username = $this->input->post("username");
        $password = $this->input->post("password");
        $email = $this->input->post("email");
        
        $this->form_validation->set_rules('username','User Name','required|is_unique[user.name]|min_length[4]');
        $this->form_validation->set_rules('password','Password','required|min_length[5]');
        $this->form_validation->set_rules('email','Email address','required|valid_email');
        $this->form_validation->set_error_delimiters('<div class="text-danger">', '</div>'); 

        if($this->form_validation->run() == true){
                $this->load->model('register_model');
                
                if($this->register_model->register_user($username,$password,$email)){
                    $this->home();

                }else{
                    $this->index();
                }
        }else{
                $this->index();
        }
    }
    
}

在表单提交页面重定向到主页,但浏览器上的 URL 是 (http://localhost:8080/codignitor_projects/shophere/register/signup) 但我想将 URL 更改为此 (http: //localhost:8080/codignitor_projects/shophere/)

【问题讨论】:

    标签: php codeigniter


    【解决方案1】:

    我有你的问题。我不知道你的 CI 版本。希望你用的是最新的,我也是按照最新版本的CI-4来回答的。

    请写

    return redirect()->to('index');
    

    return $this->response->redirect(base_url('YourControllerName/index'));
    

    而不是

    $this->index();
    

    为了澄清这个问题,我想给你一个例子。让,我有一个Test控制器,有abc()def()等两个功能

    class Test extends Controller
    {
        function abc()
        {
            echo "Hi ! I am ABC";
        }
    
        function def()
        {
            $this->abc();//URL will be: http://localhost/your_project_name/public/test/def
            //return redirect()->to('abc'); //URL will be: http://localhost/your_project_name/public/test/abc
            //return $this->response->redirect(base_url('Test/abc')); //URL will be:http://localhost/your_project_name/public/Test/abc
    
        }
    }
    

    def() 函数中的三行代码给出了相同的结果,但从第一行到其他行的 URL 会有所不同。

    【讨论】:

      猜你喜欢
      • 2021-02-22
      • 2013-06-05
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-02-09
      • 2011-06-04
      • 2016-01-28
      • 2012-06-30
      相关资源
      最近更新 更多