简体   繁体   English

将控制器中的数据附加到PHP框架中的视图(标准化)

[英]Attaching data from controllers to views in PHP frameworks, standardization

most of the modern PHP frameworks use data attaching method, for example: 大多数现代PHP框架都使用数据附加方法,例如:

View->render('login', $errors); // or
View::make('login')->with(array('errors'=>$errors);

I'm trying to establish a convention, where even if the View designers and the Controller developers do not have a direct communication, they can expect what data to put, and what data to receive, I have thought of two methods, first using a class that takes care of rendering the views including the data, and this class would contain all types of data which will be used among the platform, second method is to have a naming convention to follow, I personally find the first method better, but I have to note that I'm not a skilled developer, I'm not sure what implications can that method have, which method would you choose? 我正在尝试建立一个约定,即使View设计人员和Controller开发人员之间没有直接的交流,他们也可以期望放入哪些数据以及接收哪些数据,我想到了两种方法,首先使用类负责呈现包含数据的视图,并且该类将包含平台之间将使用的所有类型的数据,第二种方法是遵循命名约定,我个人认为第一种方法更好,但是我必须注意,我不是一个熟练的开发人员,我不确定该方法有什么含义,您会选择哪种方法? perhaps another method? 也许是另一种方法?

For direct view rendering, you can use View::composer method. 对于直接视图渲染,可以使用View :: composer方法。 View composers are callbacks or class methods that are called when a view is created. 视图编写器是在创建视图时调用的回调或类方法。 If you have data that you want bound to a given view each time that view is created throughout your application, a view composer can organize that code into a single location. 如果您每次在整个应用程序中创建该视图时都希望将其绑定到给定视图,则视图编辑器可以将该代码组织到一个位置。

It is about auto rendering. 这是关于自动渲染。 You communication convention with designers should be easily done through Separation of concerns pattern. 您可以通过关注点分离模式轻松地与设计人员进行通信约定。

Blade templating syntax is ideal for that, since it separates logic out of your view. 刀片模板语法对此非常理想,因为它将逻辑从您的视图中分离出来。 By logic, I mean all data that comes from you domain layer (model,db...). 从逻辑上讲,我的意思是所有来自您的域层的数据(模型,数据库...)。

Using {{$data}} syntax is pretty straightforward and easy to understand to designers. 使用{{$ data}}语法非常简单,设计人员也很容易理解。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM