例如,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机制,一定程度上可以避免这个问题)

PHP remains a powerful and widely used tool in modern programming, especially in the field of web development. 1) PHP is easy to use and seamlessly integrated with databases, and is the first choice for many developers. 2) It supports dynamic content generation and object-oriented programming, suitable for quickly creating and maintaining websites. 3) PHP's performance can be improved by caching and optimizing database queries, and its extensive community and rich ecosystem make it still important in today's technology stack.

In PHP, weak references are implemented through the WeakReference class and will not prevent the garbage collector from reclaiming objects. Weak references are suitable for scenarios such as caching systems and event listeners. It should be noted that it cannot guarantee the survival of objects and that garbage collection may be delayed.

The \_\_invoke method allows objects to be called like functions. 1. Define the \_\_invoke method so that the object can be called. 2. When using the $obj(...) syntax, PHP will execute the \_\_invoke method. 3. Suitable for scenarios such as logging and calculator, improving code flexibility and readability.

Fibers was introduced in PHP8.1, improving concurrent processing capabilities. 1) Fibers is a lightweight concurrency model similar to coroutines. 2) They allow developers to manually control the execution flow of tasks and are suitable for handling I/O-intensive tasks. 3) Using Fibers can write more efficient and responsive code.

The PHP community provides rich resources and support to help developers grow. 1) Resources include official documentation, tutorials, blogs and open source projects such as Laravel and Symfony. 2) Support can be obtained through StackOverflow, Reddit and Slack channels. 3) Development trends can be learned by following RFC. 4) Integration into the community can be achieved through active participation, contribution to code and learning sharing.

PHP and Python each have their own advantages, and the choice should be based on project requirements. 1.PHP is suitable for web development, with simple syntax and high execution efficiency. 2. Python is suitable for data science and machine learning, with concise syntax and rich libraries.

PHP is not dying, but constantly adapting and evolving. 1) PHP has undergone multiple version iterations since 1994 to adapt to new technology trends. 2) It is currently widely used in e-commerce, content management systems and other fields. 3) PHP8 introduces JIT compiler and other functions to improve performance and modernization. 4) Use OPcache and follow PSR-12 standards to optimize performance and code quality.

The future of PHP will be achieved by adapting to new technology trends and introducing innovative features: 1) Adapting to cloud computing, containerization and microservice architectures, supporting Docker and Kubernetes; 2) introducing JIT compilers and enumeration types to improve performance and data processing efficiency; 3) Continuously optimize performance and promote best practices.


Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

AI Hentai Generator
Generate AI Hentai for free.

Hot Article

Hot Tools

SublimeText3 Linux new version
SublimeText3 Linux latest version

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

ZendStudio 13.5.1 Mac
Powerful PHP integrated development environment

SecLists
SecLists is the ultimate security tester's companion. It is a collection of various types of lists that are frequently used during security assessments, all in one place. SecLists helps make security testing more efficient and productive by conveniently providing all the lists a security tester might need. List types include usernames, passwords, URLs, fuzzing payloads, sensitive data patterns, web shells, and more. The tester can simply pull this repository onto a new test machine and he will have access to every type of list he needs.

SublimeText3 Chinese version
Chinese version, very easy to use