简体   繁体   English

如何从Nette的neon文件中获取值?

[英]How to get values from neon file in Nette?

I have small application based on Nette framework. 我有基于Nette框架的小应用程序。

I've created constants.neon file and add it to container. 我已经创建了constants.neon文件并将其添加到容器中。 There will be some data which should be available from presenters, models, forms etc. 将有一些数据可以从演示者,模型,表格等获得。

How can I access to values in constants.neon ? 如何访问constants.neon值?

I know that there is a method (new \\Nette\\Neon\\Neon())->decode([NEON_FILE_PATH]) but I don't think that this is the right way . 我知道有一种方法(new \\Nette\\Neon\\Neon())->decode([NEON_FILE_PATH])但我不认为这是right way I suspect that after using addConfig(...) in bootstrap.php all data from those config files should be available all over the system. 我怀疑在bootstrap.php使用addConfig(...)后,这些配置文件中的所有数据都应该可以在整个系统中使用。

<?php
// bootstrap.php
require __DIR__ . '/../vendor/autoload.php';

$configurator = new Nette\Configurator;

$configurator->setDebugMode(true); // enable for your remote IP
$configurator->enableDebugger(__DIR__ . '/../log');

$configurator->setTempDirectory(__DIR__ . '/../temp');

$configurator->createRobotLoader()
    ->addDirectory(__DIR__)
    ->addDirectory(__DIR__ . '/../vendor/phpoffice/phpexcel')
    ->register();

$configurator->addConfig(__DIR__ . '/config/config.neon');
$configurator->addConfig(__DIR__ . '/config/config.local.neon');
$configurator->addConfig(__DIR__ . '/config/constants.neon');

$container = $configurator->createContainer();

return $container;

My constants.neon file: 我的constants.neon文件:

constants:
  DP_OPT = 'DP'
  PP_OPT = 'PP'
  DV_OPT = 'DV'
  ZM_OPT = 'ZM'
  TP_OPT = 'TP'

Thanks 谢谢

UPDATE #1 更新#1

Figured out that I've used wrong format of .neon file. 弄清楚我使用了错误的.neon文件格式。

constants:
  DP_OPT: DP
  PP_OPT: PP
  DV_OPT: DV
  ZM_OPT: ZM
  TP_OPT: TP

To complete Jan's answer, here's how you pass your config parameters to a model. 要完成Jan的答案,以下是将配置参数传递给模型的方法。

Make your model class expect it as a constructor parameter: 使您的模型类期望它作为构造函数参数:

namespace App\XXX;
class MyModel
{
  /** @var array */
  private $constants;

  public function __construct(array $constants)
  {
    $this->constants = $constants;
  }

Then register your model as a service in config (Neon): 然后在config(Neon)中将模型注册为服务:

services:
    - App\XXX\MyModel(%constants%)

When you inject that model into your presenter: 将该模型注入演示者时:

class DefaultPresenter extends BasePresenter
{
  /** @var App\XXX\MyModel @inject */
  public $myModel;

it will automatically receive your 'constants' when instantialized. 它会在实时化时自动收到你的'常数'。

If you store the constants inside parameters array in the neon file, you will be able to access it from presenter's context like this: 如果将常量存储在neon文件中的parameters数组中,您将能够从演示者的上下文中访问它,如下所示:

// $this is instance of Nette\Application\UI\Presenter
$this->context->parameters['constants']

Neon file: 霓虹文件:

parameters:
    constants:
        DP_OPT: DP
        PP_OPT: PP
        DV_OPT: DV
        ZM_OPT: ZM
        TP_OPT: TP

Please note that this might not be recommended approach. 请注意,这可能不是推荐的方法。 For more information see how to use presenter as a service . 有关更多信息,请参阅如何将演示者用作服务

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

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