繁体   English   中英

通过URL在MVC中使用URL路由获取变量是否存在问题

[英]Is there a problem to get a variable by an URL with an url routing in MVC

我无法在GET或REQUEST中获取URL中的变量。 如果我的密码<6,则返回错误。 我需要在我的文件中说明URL中的变量是什么错误。 但我很蠢,我不明白为什么我不能这样做......

型号/ user.php的:

public function register() {
    // Sanitize POST
    $post = filter_input_array(INPUT_POST, FILTER_SANITIZE_STRING);


    if ($post['submit']) {
        $password = $_POST['password'];
        if (strlen($password) < 6) {
            header('Location: '.ROOT_URL. 'users/register?name=12');
            exit(0);
        }


        // Insert into MySQL
        $this->query('INSERT INTO users (name, prenom, sexe, birthday, 
        email, password) VALUES(:name, :prenom, :sexe, :birthday, :email, 
        :password)');
        $this->bind(':name', $post['name']);
        $this->bind(':prenom', $post['prenom']);
        $this->bind(':sexe', $post['sexe']);
        $this->bind(':birthday', $post['birthday']);
        $this->bind(':email', $post['email']);
        $this->bind(':password', $post['password']);
        $this->execute();

        //Verify
        if($this->lastInsertId()) {
            // Redirect
            header('Location: '.ROOT_URL. 'users/login');
        }           
    }
    return;
 }

查看/ register.php:

<?php
$timeStamp = date("Y-m-d");

if (isset($_GET['name'])) {
  $model = $_GET['name'];
  echo $model;
} else {
 echo 'not found';
}
?>

    <form method="post" action="<?php $_SERVER['PHP_SELF']; ?>">
        <div class="row register-form">
           <div class="col-md-6">
                <div class="form-group">
                  <label>Nom</label>
                  <input type="text" class="form-control" name="name" 
                  placeholder="Nom *" required />
                </div>
                <div class="form-group">
                  <label>Prénom</label>
                  <input type="text" class="form-control" name="prenom" 
                placeholder="Prénom *" required />
                </div>
                <div class="form-group">
                  <label>Sexe : </label> 
                  <label class="radio-inline">
                    <input type="radio" name="sexe" value="Homme">Homme
                  </label>
                  <label class="radio-inline">
                    <input type="radio" name="sexe" value="Femme">Femme
                  </label>
              </div>                    
            </div>
            <div class="col-md-6">
              <div class="form-group">
                <div class="form-group">  
                  <label>E-mail</label>
                  <input type="email" name="email" class="form-control" 
                placeholder="E-mail *" required>
                </div>
                <div class="form-group">
                  <label>Mot de Passe</label>
                  <input type="password" name="password" class="form- 
                control" placeholder="Mot de Passe *" required/>
                </div>
                <div class="form-group">
                  <label>Date de naissance : </label>
                  <input type="date" name="birthday" min="1900-00-00" 
                max=" 
                <?php echo $timeStamp ?>">
                </div>

              </div>
              <input type="submit" class="btnRegister" name="submit"  
               value="Inscription"/>
            </div>
        </div>
      </form>

它的返回页面中找不到。

我需要在我的页面中显示错误面板引导程序,以便用户了解表单未提交的原因。 目前我只需要在URL中传递一个变量进行测试。 一切都很好,密码的形式,上下文都很好,只是在URL中使用params重定向并不好。 为什么?

这就是为什么你没有“找不到”:

当您传递参数name您将始终进入

if (isset($_GET['name'])) {
    $model = $_GET['name'];
    echo $model;
  }

因为isset($_GET['name'])永远是真的......

为了获得else语句,您应该更改视图的逻辑,例如:

if (isset($_GET['name']) && ($_GET['name'] != "error")) {
    //you would get an error bellow if you were using "$GET" intead of "$_GET"...
    $model = $_GET['name'];
    echo $model;
  } else {
    echo 'not found';
}

然后将您的模型更改为:

if ($_POST['submit']) {
    //you would get an error bellow if you were using "password" instead of "$password"
    $password = $_POST['password'];
    if (strlen($password) < 6) {
        header('Location: '.ROOT_URL. 'users/register?name=error');
        exit(0);
    }
}

只是为了记录,如果你使用MVC框架,肯定有更好的方法来处理这些错误......

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM