简体   繁体   English

在Cake PHP中的哪里定义全局函数或常量。 定义恒定动态

[英]Where to define Global Function or Constant in Cake PHP. Define Constant Dynamic

I am facing issue to define constant in cake PHP like: 我面临在蛋糕PHP中定义常量的问题,例如:

I want to define these constant from the Database. 我想从数据库中定义这些常量。

define("APP_NAME","Test Application");

How can i access these constant Model, controller and Views? 如何访问这些常量模型,控制器和视图? How can i define global function And can be accessed from the Model, controller and Views? 如何定义全局函数,并且可以从模型,控制器和视图进行访问?

Please let me know. 请告诉我。 I am new in Cake PHP 我是Cake PHP的新手

The best way to define and use the constants is create a separate file and define all constants there. 定义和使用常量的最佳方法是创建一个单独的文件,然后在其中定义所有常量。 like I have created a file "cons_variables.php" and call this file in "app\\Config/bootstrap.php" at the end. 就像我创建了一个文件“ cons_variables.php”,并在最后将其称为“ app \\ Config / bootstrap.php”。

include 'cons_variables.php';

this file contains code like: 该文件包含如下代码:

define('PER_PAGE', '1');
define('ADMIN_PAGING', '10');

To access the constants from database is not a good way.Constants can not be redeclare once they defined. 从数据库访问常量不是一个好方法。常量一旦定义就无法重新声明。 Even also you want to use constants from database than you have to create a component for that and fetch the constants value there. 甚至还想要使用数据库中的常量,而不是必须为此创建一个组件并在其中获取常量值。 This component can be use whenever you want by $components array or your controller's beforeFilter() method: 可以随时通过$ components数组或控制器的beforeFilter()方法使用此组件:

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM