任何人都知道如何定义在所有 Controller 和应用程序模型上可见的常量。
例如,我可以在其中定义这些常量:
define('APP_CONST_ACTIVE', 1);
define('APP_CONST_INACTIVE', 0);
define('APP_CONST_ENABLE', 1);
define('APP_CONST_DISABLE', 0);
或者我应该在哪个类中定义这些常量:
class WhichClass
{
const APP_CONST_ACTIVE = 1;
const APP_CONST_INACTIVE = 0;
const APP_CONST_ENABLE = 1;
const APP_CONST_DISABLE = 0;
}
我希望这些常量在我的应用程序的所有 Controller 和模型上都可见。
让我澄清一下我的问题。
我只想定义这些常量一次,并且我想在所有 Controller 、模型和 View 中访问它们。
例如在CakePHP中,如果在app/Config/core.php中,我定义了以下内容:
Configure::write('ENABLE', 1);
然后在所有 CakePHP Controller 、模型和 View 中,我可以通过以下方式访问这些常量:
Configure::read('ENABLE');
所以我的问题是:我应该在哪里定义这些在 Phalcon 的整个应用程序中可见的变量?也许在 bootstrap
文件中,但如何做到这一点?
非常感谢。
最佳答案
看:http://docs.phalconphp.com/en/latest/reference/cli.html 我定义了引导常量,并且可以在任何地方使用。 这个对我有用。 抱歉我的英语不好!!
Bootstrap :
define('VERSION', '1.0.0');
[...]
application = new \Phalcon\Mvc\Application($di);
echo $application->handle()->getContent();
Controller :
namespace App\Controllers\xxx;
class IndexController extends ControllerBase
{
public function indexAction()
{
echo VERSION;
}
}
关于model - Phalcon:如何定义在所有 Controller 和模型上可见的常量,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23674995/