PHPFixing
  • Privacy Policy
  • TOS
  • Ask Question
  • Contact Us
  • Home
  • PHP
  • Programming
  • SQL Injection
  • Web3.0

Friday, March 4, 2022

[FIXED] Symfony2 Use PHP Class Constant in YAML Config?

 March 04, 2022     php, symfony, yaml     No comments   

Issue

I know this is probably not possible, but is there a clean way to use a PHP class constant within a YAML config/services/etc. file for Symfony2?

For example, if I have this:

namespace My\Bundle\DependencyInjection;

class MyClass
{
    const MY_CONST = 'cookies';
}

Is something like this possible (in a .yml file):

services:
    my_service:
        class: Some\Class
        arguments:
            - %My\Bundle\DependencyInjection\MyClass::MY_CONST%

That'd go a long way in helping maintain consistency between the two.


Solution

In versions before Symfony 3.2, injecting PHP-constants only works with XML:

<parameter key="my_service.my_const" type="constant">My\Bundle\DependencyInjection\MyClass::MY_CONST</parameter>

If you want to keep yor yml files, you could just import the xml-file into your services.yml. Mixing config styles might be a bit ugly, but as far as I know this is the only way to do it.

If this doesn't work for you, my comment to your question applies.



Answered By - dbrumann
  • Share This:  
  •  Facebook
  •  Twitter
  •  Stumble
  •  Digg
Newer Post Older Post Home

0 Comments:

Post a Comment

Note: Only a member of this blog may post a comment.

Total Pageviews

Featured Post

Why Learn PHP Programming

Why Learn PHP Programming A widely-used open source scripting language PHP is one of the most popular programming languages in the world. It...

Subscribe To

Posts
Atom
Posts
Comments
Atom
Comments

Copyright © PHPFixing