草庐IT

php - CakePHP 应该将 number_format(与 View 相关的逻辑)放在哪里?

coder 2024-01-03 原文

在与同事讨论后,我们认为与 View 相关的逻辑应该放在哪里。

例如,假设我们有一个数字要显示在我们的 View 中。我认为 number_format(或 CakeNumber::format,因为我们使用的是 CakePHP)应该放在 View 中,因为它与我们显示的内容相关。我的同事认为它应该放在 Controller 中,因为那是所有逻辑的所在。

在这种情况下,我们有两个 View ,一个用于网站和最终用户,另一个用于返回 json 响应的 API View 。如果我将代码放在 Controller 中,我必须检查我使用的是哪个 View ,因为当它实际上是一个整数时不在 json 响应中给出一个字符串。出于这个原因,我非常支持 View 中的代码。

问题是,谁“更”对,数字格式应该放在哪里?

除了我关于将代码放在 View 中的论点之外,他很乐意在 View 中使用 htmlentities 但我认为如果我不允许使用 number_format,他就不能使用 htmlentities 并且它应该在 Controller 中完成。

最佳答案

For example lets say we have a number that we want to show in our view. I think the number_format (or CakeNumber::format as we're using CakePHP) should go in the view as it's related to what we show. My colleague thinks it should go in the controller because that's where all logic goes.

你的 friend 错了。你应该使用 NumberHelper事实上,它是静态 CakeNumber 的包装器。您通常希望在您的应用程序中避免静态和单例,也可以使用别名帮助程序,因此在 View 中使用 NumberHelper。 View 是正确的地方。

In this case we have two views, one for the website and end users and an API view which returns a json response. If I were to put the code in the controller I'd have to check which view I'm using as not to give a string in the json response when really it is an integer. For this reason I'm heavily supporting the code in the view.

Controller 无需更改一行即可支持 json 和其他输出。那么,您可以使用 View 变量的内置序列化到 json/xml 或构建自定义 json View 。 Read this chapter.但是,没有必要创建两个方法或用实际上错误的业务逻辑填充 Controller 。

The question is, who is "more" right, where should that number formatting go?

绝对是 View 。

Controller 应该是瘦的并且不包含任何业务逻辑:

Further to my argument of putting code in the view, he is happy to use htmlentities in the view but I argue that if I'm not allowed number_format, he can't have htmlentities and that it should be done in the controller.

使用h()这是 htmlspecialchars() 的快捷方式。并确保你将它用于 一切 你回显的地方。如果您通过它们传递某些内容,则框架附带的所有核心帮助程序都会在内部为您执行此操作,但要注意可能不会使用它的第三方帮助程序!

关于php - CakePHP 应该将 number_format(与 View 相关的逻辑)放在哪里?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31141937/

有关php - CakePHP 应该将 number_format(与 View 相关的逻辑)放在哪里?的更多相关文章

  1. ruby-on-rails - Rails - 一个 View 中的多个模型 - 2

    我需要从一个View访问多个模型。以前,我的links_controller仅用于提供以不同方式排序的链接资源。现在我想包括一个部分(我假设)显示按分数排序的顶级用户(@users=User.all.sort_by(&:score))我知道我可以将此代码插入每个链接操作并从View访问它,但这似乎不是“ruby方式”,我将需要在不久的将来访问更多模型。这可能会变得很脏,是否有针对这种情况的任何技术?注意事项:我认为我的应用程序正朝着单一格式和动态页面内容的方向发展,本质上是一个典型的网络应用程序。我知道before_filter但考虑到我希望应用程序进入的方向,这似乎很麻烦。最终从任何

  2. ruby-on-rails - 渲染另一个 Controller 的 View - 2

    我想要做的是有2个不同的Controller,client和test_client。客户端Controller已经构建,我想创建一个test_clientController,我可以使用它来玩弄客户端的UI并根据需要进行调整。我主要是想绕过我在客户端中内置的验证及其对加载数据的管理Controller的依赖。所以我希望test_clientController加载示例数据集,然后呈现客户端Controller的索引View,以便我可以调整客户端UI。就是这样。我在test_clients索引方法中试过这个:classTestClientdefindexrender:template=>

  3. ruby-on-rails - 如何从 format.xml 中删除 <hash></hash> - 2

    我有一个对象has_many应呈现为xml的子对象。这不是问题。我的问题是我创建了一个Hash包含此数据,就像解析器需要它一样。但是rails自动将整个文件包含在.........我需要摆脱type="array"和我该如何处理?我没有在文档中找到任何内容。 最佳答案 我遇到了同样的问题;这是我的XML:我在用这个:entries.to_xml将散列数据转换为XML,但这会将条目的数据包装到中所以我修改了:entries.to_xml(root:"Contacts")但这仍然将转换后的XML包装在“联系人”中,将我的XML代码修改为

  4. ruby - 检查 "command"的输出应该包含 NilClass 的意外崩溃 - 2

    为了将Cucumber用于命令行脚本,我按照提供的说明安装了arubagem。它在我的Gemfile中,我可以验证是否安装了正确的版本并且我已经包含了require'aruba/cucumber'在'features/env.rb'中为了确保它能正常工作,我写了以下场景:@announceScenario:Testingcucumber/arubaGivenablankslateThentheoutputfrom"ls-la"shouldcontain"drw"假设事情应该失败。它确实失败了,但失败的原因是错误的:@announceScenario:Testingcucumber/ar

  5. ruby-on-rails - 如何在我的 Rails 应用程序 View 中打印 ruby​​ 变量的内容? - 2

    我是一个Rails初学者,但我想从我的RailsView(html.haml文件)中查看Ruby变量的内容。我试图在ruby​​中打印出变量(认为它会在终端中出现),但没有得到任何结果。有什么建议吗?我知道Rails调试器,但更喜欢使用inspect来打印我的变量。 最佳答案 您可以在View中使用puts方法将信息输出到服务器控制台。您应该能够在View中的任何位置使用Haml执行以下操作:-puts@my_variable.inspect 关于ruby-on-rails-如何在我的R

  6. ruby-on-rails - 相关表上的范围为 "WHERE ... LIKE" - 2

    我正在尝试从Postgresql表(table1)中获取数据,该表由另一个相关表(property)的字段(table2)过滤。在纯SQL中,我会这样编写查询:SELECT*FROMtable1JOINtable2USING(table2_id)WHEREtable2.propertyLIKE'query%'这工作正常:scope:my_scope,->(query){includes(:table2).where("table2.property":query)}但我真正需要的是使用LIKE运算符进行过滤,而不是严格相等。然而,这是行不通的:scope:my_scope,->(que

  7. ruby-on-rails - 如何在 Rails View 上显示错误消息? - 2

    我是rails的新手,想在form字段上应用验证。myviewsnew.html.erb.....模拟.rbclassSimulation{:in=>1..25,:message=>'Therowmustbebetween1and25'}end模拟Controller.rbclassSimulationsController我想检查模型类中row字段的整数范围,如果不在范围内则返回错误信息。我可以检查上面代码的范围,但无法返回错误消息提前致谢 最佳答案 关键是您使用的是模型表单,一种显示ActiveRecord模型实例属性的表单。c

  8. java - 我的模型类或其他类中应该有逻辑吗 - 2

    我只想对我一直在思考的这个问题有其他意见,例如我有classuser_controller和classuserclassUserattr_accessor:name,:usernameendclassUserController//dosomethingaboutanythingaboutusersend问题是我的User类中是否应该有逻辑user=User.newuser.do_something(user1)oritshouldbeuser_controller=UserController.newuser_controller.do_something(user1,user2)我

  9. ruby-on-rails - 复数 for fields_for has_many 关联未显示在 View 中 - 2

    目前,Itembelongs_toCompany和has_manyItemVariants。我正在尝试使用嵌套的fields_for通过Item表单添加ItemVariant字段,但是使用:item_variants不显示该表单。只有当我使用单数时才会显示。我检查了我的关联,它们似乎是正确的,这可能与嵌套在公司下的项目有关,还是我遗漏了其他东西?提前致谢。注意:下面的代码片段中省略了不相关的代码。编辑:不知道这是否相关,但我正在使用CanCan进行身份验证。routes.rbresources:companiesdoresources:itemsenditem.rbclassItemi

  10. ruby-on-rails - 带有 Zeus 的 RSpec 3.1,我应该在 spec_helper 中要求 'rspec/rails' 吗? - 2

    使用rspec-rails3.0+,测试设置分为spec_helper和rails_helper我注意到生成的spec_helper不需要'rspec/rails'。这会导致zeus崩溃:spec_helper.rb:5:in`':undefinedmethod`configure'forRSpec:Module(NoMethodError)对thisissue最常见的回应是需要'rspec/rails'。但这是否会破坏仅使用spec_helper拆分rails规范和PORO规范的全部目的?或者这无关紧要,因为Zeus无论如何都会预加载Rails?我应该在我的spec_helper中做

随机推荐