In MVC, the issue of Message passing (bidirectional) between layers and layers

Source: Internet
Author: User
For example, how can the form data in C be passed to the M layer after the validation is done?
The current use is to assemble the data into the corresponding domain object in action, and then pass the parameter to the model~
However, by doing so, the complex assembly process is undoubtedly given to the action developer, which makes the division of responsibilities between the scoring layers unreasonable, and the C and M layers need to understand the interface of the domain object.

In addition, the model after the business feedback data back to action, especially the return of multiple data, simply return an array is not elegant enough, resulting in a two-layer dependency between the strong, not conducive to development ~

Is there a good solution?

Reply content:

For example, how can the form data in C be passed to the M layer after the validation is done?
The current use is to assemble the data into the corresponding domain object in action, and then pass the parameter to the model~
However, by doing so, the complex assembly process is undoubtedly given to the action developer, which makes the division of responsibilities between the scoring layers unreasonable, and the C and M layers need to understand the interface of the domain object.

In addition, the model after the business feedback data back to action, especially the return of multiple data, simply return an array is not elegant enough, resulting in a two-layer dependency between the strong, not conducive to development ~

Is there a good solution?

On the data structure, my suggestion is that the PHP code rejects the data object. PHP's most bullish, or most advantageous, data structure is an array, so passing an array is not elegant, which is PHP's best practice.
With regard to the transfer between MVC, my suggestion is that each layer will only pass on the data it knows as an array to the other layer, and when it receives the data, make a usability validation and assemble it into its own data structure (preferably an array).

I've struggled with your problems, and at the moment my solution is this:

/** * 创建新主题 * @return string */public function actionNew(){    if (Yii::$app->user->isGuest) {        Yii::$app->user->loginRequired();    }    $form = new TopicForm();    $form->author_id = Yii::$app->user->getId();    $form->ip_address = Yii::$app->request->getUserIP();    if ($form->load($_POST) && $form->validate())    {        Services::getTopics()->create($form->getAttributes(), $form->content);        $this->redirect(['/forum']);    }    return $this->render('new', [        'model' => $form    ]);}

This code is based on YII2, the role is to send a new post in the community.

My approach is to use model (actually my name is form, because Yii2 's form is actually model) to collect the submitted form data and then validate it.

$form = new TopicForm();$form->author_id = Yii::$app->user->getId();$form->ip_address = Yii::$app->request->getUserIP();

After validation, the data is written to the data using the appropriate service

Services::getTopics()->create($form->getAttributes(), $form->content);

The advantage of this is that the form is basically reusable, because he doesn't involve anything else, it just verifies the legitimacy of the data, so I can use it directly, whether it's the Web or the API.

Services in the same way, no matter what else, get the data to write to the database (of course, complex in-house processing, not said here), the same can be reused.

But the problem with this approach is that you need to understand the features of form and Services at the same time, and Yii2 the default practice is that you don't need to know them, a typical Yii2 from the form to the database:

public function actionCreate(){    $model = new User();    if ($model->load($_POST) && $model->save()) {        $this->redirect(['index']);    }    return $this->render('create', ['model' => $model]);}

Did you see it? People are not layered, you do not care about the other, the data load into the good, as to how to verify, how to store, are black box in the operation, the development speed greatly improved, but the code coupling slightly higher, such as the Web registered users with the app registered users of the scene may be different (of course, YII2 provides a scenario mechanism to some extent to avoid this problem)

  • Contact Us

    The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

    If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

    A Free Trial That Lets You Build Big!

    Start building with 50+ products and up to 12 months usage for Elastic Compute Service

    • Sales Support

      1 on 1 presale consultation

    • After-Sales Support

      24/7 Technical Support 6 Free Tickets per Quarter Faster Response

    • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.