Kohana 中的常量
我过去使用过 codeigniter,但在当前的项目中我正在切换到 Kohana。关于常量的最佳实践是什么?
在codeigniter中有实际的constants.php,但是浏览Kohana的源代码我没有看到类似的东西。
I've used codeigniter in the past but on my current project I'm making the switch to Kohana. What is the best practice on constants?
In codeigniter there is the actual constants.php, but going through Kohana's source I'm not seeing something similar.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
从未使用过 kohana,但经过快速谷歌搜索后,我发现您可以使用 config API 来创建您自己的配置来容纳您需要的常量。
此帖子建议,如果您存储数据库敏感项目,将它们放置在database.php配置中,等等。使它们与它们存储的数据类型相关。
Never used kohana, but after a quick googling I find that you can use the config API to create your own config that will house the constants you need.
This thread suggests that if you are storing database sensitive items, to place them in the database.php config, etc.. making them relative to the type of data they are storing.
我熟悉 Kohana,但不太熟悉 CI,所以我有点猜测你所说的“常量”的含义。我相信与此最接近的确实是 Kohana 的配置 API。因此,如果您想让模板了解某些站点范围的常量(例如您的站点名称),那么使用配置 API 是一件很棒的事情。
为此,您需要在 /config 文件夹下(可能在 /application 目录中)创建一个配置文件。叫什么名字并不重要,但由于它包含站点信息,所以我们将其命名为 site.php。
为了快速开始,您需要在该文件中包含以下内容:
现在,您可以通过执行以下操作将其引入模板:
更好的方法(使用哑模板)是将其分配为控制器中的模板变量。因此,假设您设置了一些默认控制器,代码将是:
然后您的模板将具有如下内容:
Kohana 的配置 API 很有趣,因为它是分层的,这意味着您可以在现有配置之上覆盖和合并新的配置值结构。当您调用
Kohana::config('site.name')
时,引擎会查找所有名为 site.php 的配置文件,运行所有这些配置文件并将结果合并到一个数组中。应用程序级配置文件将覆盖模块,这将覆盖系统等...然后,基于该结果数组,Kohana 将尝试找到“name”键并返回它。I'm familiar with Kohana, but not CI so much, so I'm guessing a bit to what you mean by 'constants.' I believe the closest thing to this is indeed Kohana's config API. So, if you wanted to make templates aware of some site-wide constant like your site name, that's a great thing to use the config API for.
To accomplish this, you'll need to create a config file under your /config folder, probably in the /application directory. What you call it isn't very important, but since it contains site information, let's call it site.php.
To quickly get going, here is what you'll want to have in that file:
Now, you can bring this in to a template by doing something like:
A better way to do this (using dumb templating) would be to assign this as a template variable in your Controller. So, assuming you have some default controller set up, the code would be:
And then your template would have something like this:
Kohana's config API is interesting because it is hierarchical, meaning you can override and merge new configuration values on top of existing config structures. When you call
Kohana::config('site.name')
, the engine looks through all the config files named site.php, runs all of those config files and merges the results in to an array. The application-level config files will overwrite modules, which will overwrite system, etc... Then, based on that result array, Kohana will attempt to find the 'name' key and return it.假设您想要全局常量......以下对我来说效果很好。
application/config/constants.php:
index.php:
MY_COOL_CONSTANT 应该在全局可用。
Assuming you want global constants...the following worked well for me.
application/config/constants.php:
index.php:
MY_COOL_CONSTANT should then be available globally.