如何使用 MVC 传递 POST 变量?

发布于 2024-12-24 19:02:03 字数 1107 浏览 2 评论 0原文

我正在遵循这个 MVC 指南了解使用 MVC 模式创建 Web 应用程序的基础知识。

我已经全部设置完毕并且工作正常,目前系统可以使用这种形式的 url;

address.co.uk/controller/method/params

我了解这是如何工作的,但是在发布数据时这将如何工作?例如登录表单,我尝试从控制器访问 POST 变量,但这不起作用,我猜这是由于 .htaccess 文件将所有内容发送到 index.php?

更新:

所以我创建了这个基本表单;

    <form name="input" action="register/newuser" method="post">
    Username: <input type="text" name="user" />
    password: <input type="text" name="pass" />
    <input type="submit" value="Submit" />
    </form> 

向该控制器方法 newuser 提交数据;

function newuser()
{
    $data['user'] = $_POST["user"];
    $data['pass'] = $_POST["pass"];
    $this->loadView('view_register_result',$data);
}

最后是结果页面;

<body>
    <h1>Result</h1>

    im not sure? <br />
    user: <?php echo $data['user']; ?>
    pass: <?php echo $data['pass']; ?>
</body>

有什么原因导致这不起作用吗?

I am following this MVC guide to learn the basics of creating web apps with the MVC pattern.

I have set it all up and it works fine, currently the system works with urls of this form;

address.co.uk/controller/method/params

I understand how this can work, however how would this work when posting data? for example a login form, i have tried accessing the POST variables from the controller, how ever this doesn't work, im guessing this is due to the .htaccess file sends everything to the index.php?

UPDATE:

so i have created this basic form;

    <form name="input" action="register/newuser" method="post">
    Username: <input type="text" name="user" />
    password: <input type="text" name="pass" />
    <input type="submit" value="Submit" />
    </form> 

That submits data to this controller methods newuser;

function newuser()
{
    $data['user'] = $_POST["user"];
    $data['pass'] = $_POST["pass"];
    $this->loadView('view_register_result',$data);
}

Then finally the result page;

<body>
    <h1>Result</h1>

    im not sure? <br />
    user: <?php echo $data['user']; ?>
    pass: <?php echo $data['pass']; ?>
</body>

Is there any reason why this doesn't work?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

不气馁 2024-12-31 19:02:03

$_POST 是一个超全局变量,这意味着它在任何地方都可用。当然,除非您进行实际的 POST,否则它不会被填充。

.htaccess 中的 mod_rewrite 只会重写 URL,但不会触及 $_POST$_SESSION$_COOKIE。只有 $_GET 可能会略有变化,但这超出了您的问题的范围。

编辑
您的问题绝对与 $_POST 无关。是否将 $data 作为参数传递给 loadView 将其作为 $data 添加到模板中,或者将其添加为两个单独的变量: user并通过?在这种情况下,请尝试 loadView('..', array('data' => $data))

$_POST is a superglobal, meaning that it's available everywhere. Of course, it won't be filled unless you do an actual POST.

mod_rewrite in the .htaccess will only rewrite the URL, but it won't touch the $_POST, $_SESSION or $_COOKIE. Only the $_GET might be changed slightly, but that's beyond the scope of your question.

Edit
Your issue is definitely unrelated to the $_POST. Does passing $data as an argument to loadView add it to the template as $data, or does it add it as two separate variables: user and pass? In that case, try loadView('..', array('data' => $data))

浮光之海 2024-12-31 19:02:03

提交表单后,您仍在调用 MVC 模式中的控制器。这与任何页面的工作方式相同。您可以在控制器中使用 $_POST 变量来获取该信息。您还可以使用 $_REQUEST 变量,因为它同时包含 Post 和 Get。

When your form is submitted you are still calling a controller in the MVC pattern. This works the same as any page. You can use the $_POST variable in your controller to caputer that information. You can also use the $_REQUEST variable as it contains both Post and Get.

月亮是我掰弯的 2024-12-31 19:02:03

您的 POST 变量应该始终可以从 $_POST 超级全局数组访问。如果您无法从那里检索值,则可能是因为应用程序引导程序手动清空它(即强制使用专用库),或者没有随请求传递 POST 变量。

不管怎样,我鼓励您使用现有的 MVC 解决方案。

(不过,重新发明轮子对于玩耍和学习来说很酷。)

Your POST variables should always be accessible from the $_POST superglobal-array. If you fail to retrieve the values from there, it's either because the application bootstrapper is manually emptying it (i.e. to enforce usage of a dedicated library) or no POST variables were passed through with the request.

Either way, I encourage you to use an existing solution for MVC.

(Reinventing the wheel is cool for playing around and learning though.)

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文