如何在 Cakephp 中使用其他控制器的方法

发布于 2024-12-12 04:11:18 字数 226 浏览 0 评论 0原文

在我的应用程序中,我使用 InvoicesController 来处理可以源自多个其他模型的发票的创建。在另一个方法中调用某个方法导致生成新发票后,我想调用 InvoicesController 中的 create() 方法,但还需要相关参数。我想将其集中化,因为 create() 方法还包含通知客户新发票已准备好付款的逻辑(使用 EmailComponent,因为我无法在发票模型中使用它)。

有什么想法或最佳实践吗?

In my app I use a InvoicesController which handles the creation of invoices that can originate from several other Models. After some method is called in another method that leads to the generation of a new invoice, I want to call the create() method in the InvoicesController but also need the relevant parameters. I want to centralize this, because the create() method also contains the logic of informing the customer that a new invoice is ready for payment (with EmailComponent, since I cannot use this in Invoice Model).

Any ideas or best practices for this?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

扎心 2024-12-19 04:11:18

通常你会在模型中(我的意思是创建发票)和控制器中的之前和之后(获取参数并给出答案)中执行该方法,这样你就可以轻松地重复使用发票的创建......无论如何你总是可以use

App::import('Controller', 'Posts');
$post = new PostsController();
$post->myFunction();

用于使用另一个控制器功能。

您也可以创建一个包含一些您想要重用的函数的库,但在您的情况下,我将使用模型来完成它

希望这对您有帮助

normally you would do that method in the model (i mean creation of invoice) and in the controller the before and after (getting params and giving answer) and that way you can easily reuse the creation con the invoice... anyway you can always use

App::import('Controller', 'Posts');
$post = new PostsController();
$post->myFunction();

To get to use another controller function.

Also you may do a Lib with some functions you want to reuse, but in your case i will do it with the model

Hope this helps you

故笙诉离歌 2024-12-19 04:11:18

如果您在其他控制器中经常使用它,请考虑将该函数放入 app_controller 中,并且您始终可以在任何其他控制器中使用 $this->Function 访问它。

If you use it that much in other controllers, consider putting that function in the app_controller and you`ll always be able to access it with $this->Function in any other controller.

暗地喜欢 2024-12-19 04:11:18

您应该将可重用代码放入模型方法中!

顺便一提:
您可以在模型中使用 EmailComponent。我这样做已经很多年了。有了 2.0,这一切变得更加容易。
但在 1.3 中只需使用

$Email = new EmailComponent(new Controller()));

You should put your reusable code in a model method!

by the way:
you can use EmailComponent in a model. I do that for many years now. With 2.0 it gets even easier to do so.
But in 1.3 simply use

$Email = new EmailComponent(new Controller()));
叫思念不要吵 2024-12-19 04:11:18

根据 http://book.cakephp.org /2.0/en/core-utility-libraries/app.html#loading-classes
它适用于 cakephp2.0

App::import('PostsController', 'Controller');

According to http://book.cakephp.org/2.0/en/core-utility-libraries/app.html#loading-classes
it works in cakephp2.0 with

App::import('PostsController', 'Controller');
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文