Symfony best practice: Where to place these constants?

I am wondering where should I place constants, for example. to display status in symfony. I am used to installing them in the controller, but this is not so, and I prefer the essence, but not quite.

What right?

This is not "what do you think?" - the question is, I really want to know best practices and evaluate explanations or related sources. Both work, for now.

the controller or

namespace my\bundle\Controller;

class MyController {
    const STATUS_NEW     = 1;
    const STATUs_PENDING = 2;
    // ...
}

Object ?

namespace my\bundle\Entity;

class MyEntity {
    const STATUS_NEW     = 1;
    const STATUs_PENDING = 2;
    // ...
}

Example in a branch:

{% set statusNew = constant('my\\bundle\\Controller\\MyController::STATUS_NEW')%} {# or \\Entity\\ #}
{% if data.status == statusNew %}
    Hi, I'm new.
{% endif %}

Thanks in advance!

M.

+4
source share
3 answers

- . twig , :

namespace my\bundle\Entity;

class MyEntity {
    const STATUS_NEW     = 1;
    const STATUs_PENDING = 2;
    // ...

   // For each const status
   public function isNew(){
     return $this->status == self::STATUS_NEW;
   }
}

, :

{% if data.isNew %}{# more contract form: if data.new  #}
    Hi, I'm new.
{% endif %}

( ).

.

+6

, Symfony; - โ€‹โ€‹ , , .

, .

- , Twig. , Post, , Post , :

{{ constant('NUM_ITEMS', post) }}

, - , . , Symfony .

+3

STATUS , (, ), .

+2

Source: https://habr.com/ru/post/1570084/


All Articles