where to define constants in cakephp

杀马特。学长 韩版系。学妹 提交于 2020-01-01 01:53:10

问题


In which file should I define application-wide constants that are specific to my cakephp app?


回答1:


I define them in app/config/bootstrap.php

Bootstrapping CakePHP

If you have any additional configuration needs, use CakePHP’s bootstrap file, found in app/Config/bootstrap.php. This file is executed just after CakePHP’s core bootstrapping.

This file is ideal for a number of common bootstrapping tasks:

  1. Defining convenience functions.
  2. Registering global constants.
  3. Defining additional model, view, and controller paths.
  4. Creating cache configurations.
  5. Configuring inflections.
  6. Loading configuration files.

Complete manual goes here




回答2:


@travis:

i think its better to use the configs file (and configure class) for such dev/staging stuff

the bootstrap is more like the "general" more hardcoded stuff - all app configs together the configs are the "variant" config arrays.

just my 5 cents.




回答3:


There is a another way to set constants in external file except doing it in long files like bootstrap (When it's doesn't effect on Behaviors like editable Pdf constants).

It could be retrieved from view, controller or model:

Configure::load('PDF_CONSTANT_FILE'); 
echo THE_CONSTANT;

// PDF_CONSTANT_FILE set in  app/config/PDF_CONSTANT_FILE.php

Adding $config=array(); to PDF_CONSTANT_FILE.php file at start is necessary.

Just for general benefit.



来源:https://stackoverflow.com/questions/3858232/where-to-define-constants-in-cakephp

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!