通常、検証はモデルで行う必要があります。それが MVC パラダイムのポイントのようなものです。コントローラー ロジックはアクション間でユーザーをバウンスするものであり、ビューは純粋に表示用であり、ビジネス ロジックはモデル内にあります。
特定のフレームワーク (CodeIgniter) は、ロジック (検証など) が接続されていないモデルをフラット オブジェクトにすることで、MVC の意図から大幅に逸脱し、検証ロジックをコントローラー レイヤーに移動する必要がありますが、その時点で「モデル」は実際にはまったくモデル化されていませんが、美化された配列です。
"register" と "process_register" という 2 つのアクションがある限り、投稿と取得の要求に対して異なる応答をする 1 つのアクションを使用する方がはるかにクリーンです。このアクションを「作成」と呼んでRESTfulを維持し、フレームワークが「/ register」を「/ user / create」にマップするルートを定義する場所にルートを定義します
疑似phpの例:
<?php
class User_controller {
// [GET|POST] /users/create
function create() {
$user = new User();
$error = '';
if (postback) {
// form has been submitted.
$user->name = $_POST['name'];
$user->password = $_POST['pasword'];
if (validate_user($user)) {
$user->save();
redirect("user/show/$user->id");
}
// user save failed, fall through to displaying the new user form
// the user's name and password (and other fields) are now populated,
// and will display
$error = 'Account creation failed.';
}
// Render the view with the user and the error message (if any)
render('views/users/create', $user, $error);
}
}
?>