【问题标题】:Form Submission and URI rewriting表单提交和 URI 重写
【发布时间】:2026-02-10 06:20:05
【问题描述】:

我有一个相当标准的 CI3 站点正在运行。我创建了自己的基本控制器,称为MY_Controller,我所有的页面控制器都扩展了它。

MY_Controller.php

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

/**
 * Display the view. This function wraps up all the teplates,
 * sets the page title, adds all the requested Javascript and CSS, 
 * and passes along any data.
 * @param string $view The name of the content view to display
 * @param array $data (Optional) ÏAn array of any data to pass along
 */
protected function showView($view, $data = null) {

    if ($data === null) {
        $data = array();
    }

    // call all the template pieces
    $this->load->view('header', $data);
    $this->load->view('mainNav', $data);
    $this->load->view($view, $data);
    $this->load->view('footer', $data);
}

每个页面控制器在准备好显示结果或其他内容时调用$this->showView($viewName, $data);

我的登录控制器上有一个表单,Login.php

Login.php 有一个名为“submit”的方法。

public function submit() {

    $cfg = array(
        array(
            'field' => 'username',
            'label' => 'Username',
            'rules' => 'required|trim|alpha_numeric|xss_clean|min_length[3]|max_length[50]'
        ),
        array(
            'field' => 'password',
            'label' => 'Password',
            'rules' => 'required|trim|alpha_numeric|xss_clean|min_length[3]|max_length[50]'
        )
    );

    if ($this->form_validation->set_rules($cfg) === false) {
        $this->showView("login");
    } else {
        $data = array(
            'password' => $this->input->post('password')
        );

        if (filter_var($this->input->post('username'), FILTER_VALIDATE_EMAIL) === false) {
            $data['username'] = $this->input->post('username');
        } else {
            $data['email'] = $this->input->post('username');
        }

        $user = $this->User->getUserFromLogin($data);

        if ($user !== false) {

            $sessionData = array(
                'userName'  => $user->userName,
                'email'     => $user->email,
                'authToken' => $user->authToken,
                'lastSeen'  => date("Y-m-d")
            );
            // Add user data to session
            $this->session->set_userdata('userLoggedIn', true);
            $this->session->set_userdata('userData', $sessionData);
            $this->showView("home");

        } else {
            $data = array(
                'error_message' => 'User could not be loaded.',
            );
            $this->showView("login", $data);
        }
    }
}

我的登录视图,login.php

<div class="wrapper style1">
  <article id="work">
    <header>
      <h2>Login!</h2>
      <?=validation_errors();?>
    </header>
    <div class="container 50%">
      <section>
        <form method="post" action="login/submit">
          <div>
            <div class="row">
              <div class="6u">
                <input type="text" name="username" id="username" placeholder="username or email" value="<?=set_value('username');?>" />
              </div>
              <div class="6u">
                <input type="password" name="password" id="password" placeholder="password" />
              </div>
            </div>
            <div class="row">
              <div class="12u">
                <ul class="actions">
                  <li>
                    <input type="submit" value="Sign in!" />
                  </li>
                </ul>
              </div>
            </div>
          </div>
        </form>
        <footer>
          <div>...or sign in with Facebook!</div>
          <fb:login-button scope="public_profile,email" onlogin="checkLoginState();"></fb:login-button>
        </footer>
      </section>
    </div>
  </article>
</div>

成功提交表单后,我希望它会将我重定向到 home,但是,URI 是 localhost/login/submit 而不是 localhost/home

与我的注销控制器类似,在注销时,它会导航到 URI localhost/logout/logout,这会生成 404。

我不明白为什么它没有重定向到我在showView() 方法中指定的控制器。

我没有使用任何自定义路由技巧。

【问题讨论】:

    标签: php forms codeigniter-3


    【解决方案1】:

    方法 showView 只需加载您想要的额外模板的值以及您发送的数据。我相信当您需要离开页面时,您希望进行重定向,而不仅仅是按照您的方式重新呈现页面。

    redirect($uri = '', $method = 'auto', $code = NULL)
    

    如果这不能满足您的需求,请包括您的路由和您的注销控制器,以查看在您提到的其他场景中发生了什么。

    【讨论】:

    • 使用redirect 而不是调用我的showView 似乎可以满足我的要求,但是,我仍然觉得我缺少一些东西。
    • 如果它不起作用发布我提到的块给我新的数据来帮助你。
    • 顺便说一句,我的路由文件只是默认的。我觉得我可能缺少路由规则,并且理解为什么我必须说路由规则来简单地提交表单。
    【解决方案2】:

    认证成功时,而不是

    $this->showView("home");
    

    你应该把它改成redirect()

    redirect("controller/method/parameters if any");
    

    【讨论】:

      【解决方案3】:

      有方法调用showview()。要加载视图,您必须使用

      $this->load->view("login");
      

      以及重定向应该是

      redirect('controller_name/method_name');
      

      【讨论】: