我想知道 MVC 中的 HTML 表单处理。我目前使用 Kohana,但这个问题本质上是通用的。所以我想收集关于两种方法的意见和建议:
继续进行,显示表单:
class Controler_Sample { public function action_one { $view = View::factory('form'); if($_POST) { $model = new Model_SomeModel; //validate try($model->values($_POST)->save(); { //on success go to action with success logic using post redirect get pattern $this->request->redirect('Sample/sucess') } catch(Exception $e) { //on fail attach error message to form view $view->set('errors',$e->errors); } } echo $view; } }
在其他操作中保持表单处理,而不是显示它
class Controler_Sample { public function action_one { //display form, with errors if there are anny passed in GET echo View::factory('form') set->('errors',$this->request->get('errors',FALSE); } public function action_two { if($_POST) { $model = new Model_SomeModel; //validate try($model->values($_POST)->save(); { //on success go to action two using post redirect get pattern $this->request->redirect('Sample/success') } catch(Exception $e) { //on fail create new hmvc call to action_one with errors in GET //im don't remember the syntax, let's assume it's here ok :D } } } }
您喜欢的其他架构?
我看到第一种方法键入和处理速度更快,但第二种方法更可重用 - action_two,处理表单可以从 APP 中其他地方的表单或 ajax 调用。
你怎么看 ?