[英]MVC form data submission to controller
I am managing to deploy a very simple implementation, containing a registration form and for demonstration purposes, I have chosen to use the MVC pattern. 我正在设法部署一个非常简单的实现,包含注册表单,为了演示目的,我选择使用MVC模式。
My slight issue is that when I press the submit button, I want the submitted data to be handled by the suitable method of the controller. 我的一个小问题是,当我按下提交按钮时,我希望通过适当的控制器方法处理提交的数据。
For instance: 例如:
within the view part, I declare the form like this : 在视图部分中,我声明了这样的形式:
<form action="controller/validate" method="post"/>
I am assuming that this is a routing-related thing, but I am curious whether another way can be suggested. 我假设这是一个与路由相关的事情,但我很好奇是否可以建议另一种方式。
-- -
Regards, 问候,
Theo 西奥
A router can be as simple as a switch statement instead of a full blown router for the small sites: 路由器可以像switch语句一样简单,而不是小型站点的完整路由器:
switch($_SERVER['REQUEST_URI']) {
case 'controller/validate':
$view = new \Views\User\Registration();
$controller = new \Controllers\UserRegistration($view);
$method = 'validate';
break;
default:
$view = new \Views\Error\NotFound();
$controller = new \Controllers\Error($view);
$method = 'notFound';
break;
}
echo $controller->$method();
Also note that instead of doing a relative URL based on the current path you often really want to do a relative URL to the document root: 另请注意,您不必根据当前路径执行相对URL,而是通常希望对文档根执行相对URL:
<form action="/controller/validate" method="post"/>
Note the leading slash. 注意前导斜杠。
The above is just a simple (untested) example of semi pseudocode 以上只是一个简单的(未经测试的)半伪代码示例
Usually, you would make your controller accessible through a route (like POST /register
). 通常,您可以通过路径 (如
POST /register
)访问控制器 。 A controller action is assigned to this route in a bootstrap file. 在引导程序文件中为此路由分配控制器操作。 Some pseudo-code:
一些伪代码:
$framework->route('GET', '/register',
'RegistrationController::action_form');
$framework->route('POST', '/register',
'RegistrationController::action_submit');
Another aproach would be mapping routes directly to controllers (some frameworks do this, I believe Kohana is one of them) like this: 另一个方法是将路由直接映射到控制器(一些框架这样做,我相信Kohana就是其中之一),如下所示:
/register/submit
/register/submit
RegisterContoller::action_submit()
RegisterContoller::action_submit()
GET
, POST
, PUT
, DELETE
in standard HTTP) happens in the action methods. GET
, POST
, PUT
, DELETE
)的管理发生在操作方法中。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.