MVCでのHTMLフォーム処理について疑問に思っています。私は現在コハナを使用していますが、質問は本質的に一般的です。だから私は2つのアプローチについての意見と推奨事項を集めたいと思います:
処理を続行すると、次のフォームが表示されます。
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 } } } }
あなたが好む他のアーキテクチャ?
最初のアプローチは入力と処理が高速ですが、2番目のアプローチはより再利用可能です-フォームを処理するaction_twoは、APPの他の場所のフォームまたはajaxから呼び出すことができます。
どう思いますか ?