如何使用控制器操作方法动态添加帮助程序或组件
我不想按如下方式添加它,因为我在某些操作方法中只需要它们一次
(所以不要无用地加载内存) <代码>
class UsersController extends AppController {
var $name = 'Users';
var $helpers = array('Html', 'Session');
var $components = array('Session', 'Email');
i don't want to add it as below cause i needed them only once in certain action method
(so do not useless load the memory)
class UsersController extends AppController { var $name = 'Users'; var $helpers = array('Html', 'Session'); var $components = array('Session', 'Email');
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
有关此内容的更多信息,请文档。
希望有帮助。
More on this in the documentation.
Hope that helps.
您可以使用上面提到的 Rob 加载帮助程序
,但这对于控制器不起作用,因为它们有需要调用才能工作的初始化和启动方法。
我在网上遇到了一些用于在控制器操作内加载组件的代码: ComponentLoaderComponent
是的,它是一个组件,但它不是很大,因此将它包含在控制器中应该不成问题。
或者您可以研究它以了解组件加载的工作原理,然后编写您自己的控制器操作来执行相同的操作。
You can load helpers using
as Rob mentioned above, but this won't work for controllers because they have their initialize and startup methods that need to be called in order for them to work.
I've come across a bit of code on the web for loading components inside of a controller action: ComponentLoaderComponent
Yes, it is a component but it isn't very big so it shouldn't be a problem to include it in your controllers.
Either that or you can just study it to see how the component loading works and then write your own controller action to do the same.
我使用一个组件来动态添加助手和组件:
等
完整的代码可以在我刚刚打开的 cakephp 增强票中看到:
http://cakephp.lighthouseapp.com/projects/42648-cakephp/tickets/1277
或者使用 php 标记:
http://www.dereuromark.de/2010/ 11/10/loading-classes-on-the-fly/
它还修复了 mtnorthrop 发布的解决方案中的一些小问题。
现在可以使用插件和传递选项。玩得开心。
I use a component for adding helpers and components on the fly:
etc
The complete code to this can be seen in the cakephp enhancement ticket I just opened:
http://cakephp.lighthouseapp.com/projects/42648-cakephp/tickets/1277
Or with php markup:
http://www.dereuromark.de/2010/11/10/loading-classes-on-the-fly/
It also fixes some minor problems with the solution posted by mtnorthrop.
Plugins as well as passed options are now possible. Have fun.