在CakePHP中的条件组件加载 [英] Conditional Component Loading in CakePHP

查看:154
本文介绍了在CakePHP中的条件组件加载的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在我的项目中使用DebugKit组件,但是我只想在临时服务器上打开它,并且在从生产服务器运行时不加载。

I'm using the DebugKit component in my project but I want to turn it on only on the staging server and have it not load when running from the production server.

我知道我可以关闭与debug配置值,但我想保持这个值为1为两个服务器。

I know I can turn it off with the debug config value but I want to keep that value at 1 for both servers.

我试着有条件地定义一个contstant'DEBUG_KIT '在bootstrap.php中作为组件名称(即'DebugKit.Toolbar')或null。然后在app_controller文件顶部的var $组件定义中使用该常量。好吧,Cake不喜欢在组件数组和barfs中有一个null。不喜欢一个空字符串。

I tried conditionally defining a contstant 'DEBUG_KIT' in bootstrap.php as either the component name (ie. 'DebugKit.Toolbar') or null. Then using that constant in the var $component definition at the top of the app_controller file. Well, Cake doesn't like having a null in the component array and barfs. Doesn't like an empty string either.

我觉得我错过了一些东西,但不能看到树的森林。提前致谢!

I feel like I'm missing something but can't quite see the forest for the trees. Thanks in advance!

推荐答案

首先,感谢Adam Giles一个伟大的答案。我没有想过看看__construct()回调。这可能是一个比我发现的更好的方法。和丹尼尔·赖特,点了先生。我可能会将我的生产服务器更改为0调试很快,并开始观看错误日志。

First, thanks to Adam Giles for a great answer. I hadn't thought to look at the __construct() callback. That just may be a better way than I found. And to Daniel Wright, point made sir. I'll probably change my production server to 0 debug soon and start watching the error logs.

我在发布此问题后不久发现了我自己的答案。 DebugKit有一个'autoRun'参数,它将打开和关闭。所以,我首先在bootstrap.php中设置一个全局常量,如下所示:

I found my own answer shortly after posting this question. The DebugKit has an 'autoRun' parameter that will turn it on and off. So, I first set a global constant in bootstrap.php like this:

define( 'IS_DEV', ($_SERVER['SERVER_NAME'] == 'staging.example.com') );

然后在app_controller.php中,我使用它在$ components语句中设置'autoRun' 。

Then in app_controller.php, I use it to set the 'autoRun' parameter in the $components statement.

var $components = array( 'DebugKit.Toolbar'=>array('autoRun'=>IS_DEV) );

到目前为止,这看起来效果还不错。

This seems to work pretty well so far.

这篇关于在CakePHP中的条件组件加载的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆