我可以使用相同的视图执行不同的操作吗?
我有控制器“产品”,具有“添加”和“编辑”操作。
我创建了一个很长的视图“add.ctp”。想知道我是否可以使用相同的视图来生成显示“编辑”。我需要在控制器的 edit() {} 下放入什么,才能使其输出到“add.ctp”而不是“edit.ctp”?
谢谢。
I have controller, 'products', with the actions 'add' and 'edit'.
I made the view 'add.ctp' which has a long form. Was wondering if I can use that same view for generating display 'edit'. What would I need to put in the controller, under edit() {}, to make it output to 'add.ctp' instead of 'edit.ctp'?
Thanks.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
使用:
您可以在控制器编辑功能的末尾
,但我不推荐它。更好的解决方案是进行一个小的重构,将 add.ctp 的内容放入一个元素中(例如 app/views/elements/productForm.ctp),然后使用 add.ctp 和 edit.ctp 中的该元素意见。
这使您可以灵活地使用特定于操作的元素包装表单,并执行可能特定于该操作的任何设置。例如,在产品表单下,您可能有一组不同的操作,例如“查看产品”,该操作在添加模式下没有意义,但在编辑模式下有意义。
@kaklon 提出了一个非常好的观点,您应该在 ProductForm 元素中添加一些逻辑,以确保编辑模式行为正确:
You could use:
at the end of your controller's edit function, but I wouldn't recommend it.
A better solution would be to do a small refactor to put the contents of your add.ctp into an element (e.g. app/views/elements/productForm.ctp), and then use that element from both the add.ctp and edit.ctp views.
This gives you the flexibility to wrap the form with action specific elements, and do any setup that might be specific to that action. For example, under the product form you might have a different set of actions, such as "View Product" that doesn't make sense in add mode but does in edit mode.
@kaklon made a very good point, you should put a little bit of logic into the productForm element to make sure edit mode behaves correctly:
Pseudocoder 就此写了一篇非常好的文章: http://www.pseudocoder.com/ archives/category/cakephp/page:4
他通过使用路由来完全消除我认为的添加操作,进一步改进了这一点。您可能对他的 CakePHP 技巧和技巧汇编感兴趣。黑客: http://www.pseudocoder.com/free-cakephp-book/
Pseudocoder wrote a very good article on this: http://www.pseudocoder.com/archives/category/cakephp/page:4
He has since improved this even more by using routes to eliminate the add action altogether I think. You may be interested in his compilation of CakePHP tips & hacks: http://www.pseudocoder.com/free-cakephp-book/
是的,你可以,这是一种方法... https:/ /github.com/infinitas/infinitas/blob/dev/app_controller.php#L374
允许您在只有一个可用文件时使用一个文件,或者在需要不同内容时添加/编辑
yes you can, This is one way to do it... https://github.com/infinitas/infinitas/blob/dev/app_controller.php#L374
that allows you to use one file when there is only one available, or add/edit when you need different stuff
您正在寻找元素。视图并不是100%相同,因为编辑表单需要包含要编辑的产品的id,而添加表单没有id。
You are looking for elements. The view is not 100% identical, because the edit form needs to contain the id of the product you want to edit, while the add form does not have an id..