如何通过MVC传递POST变量? [英] How to pass POST variables with MVC?
问题描述
我正在遵循 MVC指南了解使用MVC模式创建Web应用程序的基础.
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
address.co.uk/controller/method/params
我了解这是如何工作的,但是在发布数据时这将如何工作?例如一个登录表单,我尝试从控制器访问POST变量,但是怎么都行不通,我猜这是由于.htaccess文件将所有内容发送到index.php吗?
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?
更新:
所以我已经创建了这种基本形式;
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>
将数据提交给此控制器方法newuser;
That submits data to this controller methods 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>
有什么理由不起作用吗?
Is there any reason why this doesn't work?
推荐答案
$_POST
是一个超全局变量,意味着它随处可见.当然,除非您执行实际的POST,否则不会填满它.
$_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
仅会重写URL,但不会触摸$_POST
,$_SESSION
或$_COOKIE
.只有$_GET
可能会稍作更改,但这超出了您的问题范围.
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.
修改
您的问题绝对与$_POST
无关.将$data
作为参数传递给loadView
是将它作为$data
添加到模板,还是将它作为两个单独的变量添加:user和pass?在这种情况下,请尝试loadView('..', array('data' => $data))
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))
这篇关于如何通过MVC传递POST变量?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!