且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

如何从另一个控制器动作中访问控制器动作?

更新时间:2022-05-25 23:52:26

您不要那样做!

如果需要访问另一个控制器,并且无法通过继承从逻辑上解决该问题,则表明您在做错事情,并且您打算违反CakePHP提出的MVC原理。

If you need to access another controller, and this cannot be logically solved via inheritance, then this indicates that you are doing something wrong, and that you're planning to violate the MVC principle that CakePHP proposes.

如果您需要在其他独立控制器之间共享功能,则应考虑将该功能转移到 组件 (代表服务层),服务类(可能是模型层的一部分)或模型。

If you need to share functionality accross otherwise independent controllers, then you should look into moving that functionality into components (which kind of represent a service layer), service classes (which could be part of the model layer), or models.

由于在CakePHP 3.x中,模型,或者更确切地说,模型层的表类是高度数据库中心的,并且您不想使用数据库,因此应该选择两个以前的选项之一,即组件或服务类,但这确实取决于,您的模型层当然可以拥有任何业务功能,甚至没有表类***与数据库进行通信,它的数据源可以是您实现的任何形式。

Since in CakePHP 3.x the models, or let's better say the table classes of the model layer, are highly database centric, and you don't want to use a database, you should maybe choose one of the two former options, ie components or service classes, but it really depends, your model layer can of course hold any business functionality, and not even a table class is forced to communicate with a database, its datasource can be whatever you implement it to be.