search
HomeBackend DevelopmentPHP TutorialMVC中,层与层之间消息传递(双向)的问题

例如,C中的表单数据在做完验证后,如何传递给M层?
目前采用的是在Action中就把数据组装成对应的领域对象,然后通过传参的方式给制定的model~
但是这样做的话无疑把复杂的组装过程交给了action的开发人员,这样做会使得分层之间的职责划分不够合理,c和m层的人员都需要去了解领域对象的相关接口。

另外,model做完业务后的反馈数据如何回传给action,尤其是多条数据返回,简单的返回一个数组总觉得不够优雅,导致两层之间的依赖过强,不利于开发~

有没有好的解决方案?

回复内容:

例如,C中的表单数据在做完验证后,如何传递给M层?
目前采用的是在Action中就把数据组装成对应的领域对象,然后通过传参的方式给制定的model~
但是这样做的话无疑把复杂的组装过程交给了action的开发人员,这样做会使得分层之间的职责划分不够合理,c和m层的人员都需要去了解领域对象的相关接口。

另外,model做完业务后的反馈数据如何回传给action,尤其是多条数据返回,简单的返回一个数组总觉得不够优雅,导致两层之间的依赖过强,不利于开发~

有没有好的解决方案?

关于数据结构,我的建议是,PHP代码,拒绝数据对象。PHP最牛的,或者说最具有优势的数据结构就是数组,所以传递数组不是不优雅,这就是PHP的最佳实践。
关于MVC之间的传递,我的建议是各层只把自己了解的数据以数组形式传递给其它层,接收数据的时候,做一次可用性验证,拼装成自己需要的数据结构(最好也是数组)。

你的问题我也纠结过,目前我的解决办法是这样的:

<code>/**
 * 创建新主题
 * @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
    ]);
}
</code>

这段代码是基于Yii2的,作用是在社区发一个新贴。

我的做法是用model(实际上我写的名字是form,因为Yii2的form其实就是model)来收集提交上来的表单数据,然后进行验证

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

验证通过后会使用相应的服务将数据写入数据

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

已知这样做的好处是form基本可以重用,因为他不涉及其他东西,只是验证数据合法性,所以不管是web、api,我都能直接使用他。

Services同理,不管别的,拿到数据就往数据库写(当然复杂的还要在内部处理,这里不说),同样也能复用。

不过这种做法有个问题,就是你需要同时了解 Form 跟 Services 的功能,而Yii2默认的做法是你不需要了解他们,一个典型的Yii2从表单到数据库的代码是这样的:

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

    return $this->render('create', ['model' => $model]);
}
</code>

看出来了吗?人家压根就不分层,你根本不用关心其他的,把数据load进去就好了,至于怎么验证,怎么存储,都是黑箱在操作,开发速度大大提升,但是代码耦合略高,比如web注册用户跟app上注册用户的情景可能是不同的(当然,Yii2提供了scenario机制,一定程度上可以避免这个问题)

Statement
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
PHP Performance Tuning for High Traffic WebsitesPHP Performance Tuning for High Traffic WebsitesMay 14, 2025 am 12:13 AM

ThesecrettokeepingaPHP-poweredwebsiterunningsmoothlyunderheavyloadinvolvesseveralkeystrategies:1)ImplementopcodecachingwithOPcachetoreducescriptexecutiontime,2)UsedatabasequerycachingwithRedistolessendatabaseload,3)LeverageCDNslikeCloudflareforservin

Dependency Injection in PHP: Code Examples for BeginnersDependency Injection in PHP: Code Examples for BeginnersMay 14, 2025 am 12:08 AM

You should care about DependencyInjection(DI) because it makes your code clearer and easier to maintain. 1) DI makes it more modular by decoupling classes, 2) improves the convenience of testing and code flexibility, 3) Use DI containers to manage complex dependencies, but pay attention to performance impact and circular dependencies, 4) The best practice is to rely on abstract interfaces to achieve loose coupling.

PHP Performance: is it possible to optimize the application?PHP Performance: is it possible to optimize the application?May 14, 2025 am 12:04 AM

Yes,optimizingaPHPapplicationispossibleandessential.1)ImplementcachingusingAPCutoreducedatabaseload.2)Optimizedatabaseswithindexing,efficientqueries,andconnectionpooling.3)Enhancecodewithbuilt-infunctions,avoidingglobalvariables,andusingopcodecaching

PHP Performance Optimization: The Ultimate GuidePHP Performance Optimization: The Ultimate GuideMay 14, 2025 am 12:02 AM

ThekeystrategiestosignificantlyboostPHPapplicationperformanceare:1)UseopcodecachinglikeOPcachetoreduceexecutiontime,2)Optimizedatabaseinteractionswithpreparedstatementsandproperindexing,3)ConfigurewebserverslikeNginxwithPHP-FPMforbetterperformance,4)

PHP Dependency Injection Container: A Quick StartPHP Dependency Injection Container: A Quick StartMay 13, 2025 am 12:11 AM

APHPDependencyInjectionContainerisatoolthatmanagesclassdependencies,enhancingcodemodularity,testability,andmaintainability.Itactsasacentralhubforcreatingandinjectingdependencies,thusreducingtightcouplingandeasingunittesting.

Dependency Injection vs. Service Locator in PHPDependency Injection vs. Service Locator in PHPMay 13, 2025 am 12:10 AM

Select DependencyInjection (DI) for large applications, ServiceLocator is suitable for small projects or prototypes. 1) DI improves the testability and modularity of the code through constructor injection. 2) ServiceLocator obtains services through center registration, which is convenient but may lead to an increase in code coupling.

PHP performance optimization strategies.PHP performance optimization strategies.May 13, 2025 am 12:06 AM

PHPapplicationscanbeoptimizedforspeedandefficiencyby:1)enablingopcacheinphp.ini,2)usingpreparedstatementswithPDOfordatabasequeries,3)replacingloopswitharray_filterandarray_mapfordataprocessing,4)configuringNginxasareverseproxy,5)implementingcachingwi

PHP Email Validation: Ensuring Emails Are Sent CorrectlyPHP Email Validation: Ensuring Emails Are Sent CorrectlyMay 13, 2025 am 12:06 AM

PHPemailvalidationinvolvesthreesteps:1)Formatvalidationusingregularexpressionstochecktheemailformat;2)DNSvalidationtoensurethedomainhasavalidMXrecord;3)SMTPvalidation,themostthoroughmethod,whichchecksifthemailboxexistsbyconnectingtotheSMTPserver.Impl

See all articles

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

SublimeText3 English version

SublimeText3 English version

Recommended: Win version, supports code prompts!

DVWA

DVWA

Damn Vulnerable Web App (DVWA) is a PHP/MySQL web application that is very vulnerable. Its main goals are to be an aid for security professionals to test their skills and tools in a legal environment, to help web developers better understand the process of securing web applications, and to help teachers/students teach/learn in a classroom environment Web application security. The goal of DVWA is to practice some of the most common web vulnerabilities through a simple and straightforward interface, with varying degrees of difficulty. Please note that this software

Dreamweaver Mac version

Dreamweaver Mac version

Visual web development tools

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools