在不同的控制器 CakePHP 中使用相同的模型
我刚刚开始阅读 CakePHP,到目前为止一切都进展顺利,尽管我对执行以下操作的最佳方法有疑问:
我有一个“User”模型和一个“UsersController”控制器。
我目前有一个由 home_controller.php 控制的“主页”页面(显然)。主页包含用户的注册。
问题
当从主页发布表单时,我需要访问用户模型(从家庭控制器)。
这项任务的最佳实践是什么?
I've just started reading up on CakePHP and everything is going pretty good so far, though I have a query on the best way to do the following:
I have a "User" model and a "UsersController" controller.
I currently have a "home" page which is controlled by the home_controller.php (obviously). The home page contains a registration for for a user.
The Question
When the form is posted from the home page, I need to access the User model (from the home controller).
What is the best practice for this task?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
如果我正确理解情况,我会将表单发布到用户控制器中的某些功能。然后这个函数会保存数据,或者登录,或者其他什么。最后,例如重定向回主页。
If I understand the situation correctly, I would post the form to some function in users controller. Then this function would save the data, or log in, or whatever. Finally make redirect back to home for example.
您可以轻松地在多个控制器之间共享一个模型,
我使用用户模型和
例如。他们都共享用户模型。
you can easily share one model across many controllers
I do that with User Model and
for example. they all share the User Model.
我目前有一个由 home_controller.php 控制的“主页”页面(显然)
除了索引之外,您在 home_controller 中还有哪些其他方法?在 Cake 约定中,控制器是复数,因此:用户、类别...很可能,您不知道 Cake 中的pages_controller 和路由。无论如何,是的,您可以从任何页面向任何控制器(甚至另一个域)发布帖子,就像任何常规 HTML 页面一样。
echo $this->Form->create('User', array('controller'=>'users','action' => 'register'));
您可以阅读更多信息请参见:http://book.cakephp.org/view/1384/Creating-FormsI currently have a "home" page which is controlled by the home_controller.php (obviously)
What other methods do you have in home_controller, other than index? And in Cake convention, the controller is plural, so: users, categories... Most likely, you are not aware of pages_controller and routing in Cake.Anyway, yes, you can make a post to any controller (or even to another domain) from any page, just like any regular HTML page.
echo $this->Form->create('User', array('controller'=>'users','action' => 'register'));
You can read more here: http://book.cakephp.org/view/1384/Creating-Forms